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

标签 c++ fatal-error symbols

我收到这个错误:

fatal error LNK1169: one or more multiply defined symbols found

下面是两个包含代码的文件。在文件 1 中,我有 main() 函数,我正在调用第二个名为 linklist.cpp 的文件中编写的函数。 感谢您提前提供帮助。

文件 1 - main.cpp

#include "stdafx.h"
# include "linklist.cpp"


int main(int argc, _TCHAR* argv[])
{
    node *link_list2;
    link_list2 = createList(31);
    addFront(link_list2,33);
    printList(link_list2);
    printf("Hello There Omer Obaid khan\n");
    return 0;
}

文件 2 - linklist.cpp

# include "stdafx.h"
# include <stdlib.h>
struct node{
    node * next;
    int nodeValue;

};

node* initNode(int number);
node* createList (int value);
void addFront (node *head, int num );
void deleteFront(node*num);
void destroyList(node *list);
int getValue(node *list);

node*createList (int value)  /*Creates a Linked-List*/
{
    node *dummy_node = (node*) malloc(sizeof (node));
    dummy_node->next=NULL;
    dummy_node->nodeValue = value;
    return dummy_node;
}


void addFront (node *head, int num ) /*Adds node to the front of Linked-List*/
{
    node*newNode = initNode(num);   
    newNode->next = NULL;
    head->next=newNode;
    newNode->nodeValue=num;
}

void deleteFront(node*num)   /*Deletes the value of the node from the front*/
{
    node*temp1=num->next;

    if (temp1== NULL) 
    {
        printf("List is EMPTY!!!!");
    }
    else
    {
        num->next=temp1->next;
        free(temp1);
    }

}

void destroyList(node *list)    /*Frees the linked list*/
{
    node*temp;
    while (list->next!= NULL) 
    {
        temp=list;
        list=temp->next;
        free(temp);
    }
    free(list);
}

int getValue(node *list)    /*Returns the value of the list*/
{
    return((list->next)->nodeValue);
}


void printList(node *list)   /*Prints the Linked-List*/
{

    node*currentPosition;
    for (currentPosition=list->next; currentPosition->next!=NULL; currentPosition=currentPosition->next)  
    {
        printf("%d \n",currentPosition->nodeValue);
    }   
    printf("%d \n",currentPosition->nodeValue);

}

node*initNode(int number) /*Creates a node*/
{
    node*newNode=(node*) malloc(sizeof (node));
    newNode->nodeValue=number;
    newNode->next=NULL;
    return(newNode);
}

最佳答案

我在 # include "linklist.cpp" 后停止阅读。 不要在其他实现文件中包含实现文件。(除非您正在进行批量构建,我对此表示怀疑)。在 header 中单独声明并包含这些声明,并将定义保留在实现文件中。

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

相关文章:

c++ - DirectX - 创建以顶点数组作为参数的缓冲区

PHP call_user_func 替代

php - fatal error : Call to a member function get_error_code() on a non-object

python - 如何在 tkinter 按钮中使用符号字体? Python

c++ - 未经初始化打印随机符号

Java 8 不呈现货币符号

c++ - gdb和ffmpeg编译

c++ - 在消息中显示每个字符的十六进制?

c++ - Qt5:告诉 QPlainTextEdit 忽略语法高亮更改

php - Magento 1.9 fatal error Mage_Configurableswatches_Helper_Productimg' 找不到