致命错误 LNK1169:找到一个或多个多重定义的符号 (C++)

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

我正在尝试编译此代码,但出现致命错误 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())。