首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >致命错误LNK1169:找到一个或多个多重定义的符号

致命错误LNK1169:找到一个或多个多重定义的符号
EN

Stack Overflow用户
提问于 2012-10-04 17:00:16
回答 2查看 15.6K关注 0票数 1

我得到了这个错误:

代码语言:javascript
运行
复制
fatal error LNK1169: one or more multiply defined symbols found

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

文件1- main.cpp

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
# 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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-04 17:01:19

# include "linklist.cpp"之后我就不再读了。不要在其他实现文件.中包含实现文件(除非您正在进行批量构建,我对此表示怀疑)。在头文件中分隔声明,并包含这些声明,并将定义保留在实现文件中。

票数 9
EN

Stack Overflow用户

发布于 2012-10-04 17:13:37

你有两种方法来解决你的问题:

首先是对Luchian Grigore的回答。创建单独的头文件,并将其包含在主文件中。

第二个是使用项目选项从构建中排除文件linklist.cpp。换句话说,该文件将被构建两次:在他自己构建期间和在主文件构建期间。

然而,第二种方式并不是好的编程风格。最好是创建头文件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12723766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档