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

Posted

技术标签:

【中文标题】致命错误 LNK1169:找到一个或多个多重定义符号 (C++)【英文标题】:fatal error LNK1169: one or more multiply defined symbols found (C++) 【发布时间】:2015-03-01 16:21:58 【问题描述】:

我正在尝试编译此代码,但收到致命错误 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.

【问题讨论】:

对我来说是编译好的。我想没有错。检查您是否编译了您认为已编译的代码。通常编译器会告诉你哪个符号是重复的。 如我的回答中所述,我无法重现您的链接器错误。请指定使用哪个链接器命令行,以及逐字显示错误消息。 【参考方案1】:

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

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

【讨论】:

天才。我在一个项目中有多个具有主要功能的文件。非常感谢!

以上是关于致命错误 LNK1169:找到一个或多个多重定义符号 (C++)的主要内容,如果未能解决你的问题,请参考以下文章

从 AfxGetAppModuleState 更改为 AfxGetStaticModuleState 会导致链接器错误:LNK1169:找到一个或多个多重定义的符号

全局变量重复定义,fatal error LNK1169: 找到一个或多个多重定义的符号

error LNK1169: 找到一个或多个多重定义的符号”的解决方法(转载)

fatal error LNK1169: 找到一个或多个多重定义的符号

fatal error LNK1169: 找到一个或多个多重定义的符号

出现“error LNK1169: 找到一个或多个多重定义的符号”的原因