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

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

本文关键字:定义 定义符 符号 C++ LNK1169 致命错误 一个      更新时间:2023-10-16

我正试图编译此代码,但我得到了致命错误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.
}

问题中发布的代码对我来说很好(我无法在此处复制任何链接器错误)。

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