c++ - fatal error LNK1169 : one or more multiply defined symbols found (C++)

标签 c++ visual-studio compiler-errors

我正在尝试编译此代码,但出现 fatal error LNK1169:找到一个或多个多重定义的符号。该代码只是一个测试,看看 C++ 指针是如何工作的。请告诉我代码有什么问题。谢谢。

#include <iostream>
using namespace std;

int main()
{
    //PROBLEM 8.8

    //8.8 (a) Declare an array of type unsigned int called values with five elements, and initialize
    //the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE
    //has been defined as 5.

    unsigned const int SIZE = 5;

    unsigned int values[SIZE] = { 2, 4, 6, 8, 10 };

    //8.8 (b) Declare a pointer vPtr that points to an object of type unsigned int.
    unsigned int *vPtr;

    //8.8 (c) Use a for statement to print the elements of array values using array subscript notation.
    for (int i = 0; i < SIZE; i++){
        cout << values[i] << endl;
    }

    //8.8 (d) Write two separate statements that assign the starting address of array values to pointer
    //variable vPtr.
    vPtr = values;
    vPtr = &values[0];

    //8.8 (e) Use a for statement to print the elements of array values using pointer/offset notation.
    for (int i = 0; i < SIZE; i++){
        cout << *(vPtr + i) << endl;
    }

    //8.8 (f) Use a for statement to print the elements of array values using pointer/offset notation
    //with the array name as the pointer.
    for (int i = 0; i < SIZE; i++){
        cout << *(values + i) << endl;
    }

    //8.8 (g) Use a for statement to print the elements of array values by subscripting the pointer to
    //the array.
    for (int i = 0; i < SIZE; i++){
        cout << (vPtr[i]) << endl;
    }

    //8.8 (h) Refer to the fifth element of values using array subscript notation, pointer/offset notation
    //with the array name as the pointer, pointer subscript notation and pointer/offset
    //notation.
    cout << values[4] << endl;
    cout << *(vPtr + 4) << endl;
    cout << *(values + 4) << endl;
    cout << vPtr[4] << endl;

    //8.8 (i) What address is referenced by vPtr + 3? What value is stored at that location?
    //Address: 1002506; 8 is stored;
    cout << (vPtr + 3) << endl;
    cout << *(vPtr + 3) << endl;


    //8.8 (j) Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4?
    //What value is stored at that location?

    //Address: 1002500; 2 is stored;

    unsigned int *temp = vPtr;
    cout << (vPtr -= 4) << endl;
    cout << *(temp -= 4) << endl;




    //PROBLEM 8.9

    //8.9 (a) Declare the variable longPtr to be a pointer to an object of type long.
    long *longPtr;

    //8.9 (b) Assign the address of variable value1 to pointer variable longPtr.
    long value1 = 200000;
    long value2;
    longPtr = &value1;

    //8.9 (c) Print the value of the object pointed to by longPtr.
    cout << *longPtr << endl;

    //8.9 (d) Assign the value of the object pointed to by longPtr to variable value2.
    value2 = *longPtr;

    //8.9 (e) Print the value of value2.
    cout << value2 << endl;

    //8.9 (f) Print the address of value1.
    cout << &value1 << endl;

    //8.9 (g) Print the address stored in longPtr. Is the value printed the same as value1’s address?
    cout << longPtr << endl; //Yes, it's the same.
}

最佳答案

您在问题中发布的代码对我来说很好(我无法重现任何链接器错误 here )。

您可能还有其他 .cpp同一项目中使用相同符号的文件(包括 main() )。

关于c++ - fatal error LNK1169 : one or more multiply defined symbols found (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795912/

相关文章:

C#、Visual Studio,如何获取错误和警告的 ID?

c - 如何用Visual Studio 2010开发C语言?

compiler-errors - 无法解决错误,VHDL,语法错误,但我看不到

c++ - 没有匹配的函数可以调用合并排序中的函数

c++ - 如何使用getc编写getLine函数

c++ - 模板的多态使用

c++ - 使指针指向数组c++的开头

visual-studio - 将 VS2015 升级到 asp.net 5 beta7 我得到无法加载文件或程序集 'dnx.clr.managed' 或其依赖项之一

objective-c - Obj-C 中的乘法问题

module - 打开 Module_name 给出编译器错误