无论我做什么,我都会得到LNK1169和LNK2005

时间:2015-04-01 20:49:14

标签: c++ visual-studio-2013 linker-errors

我正在制作关于兔子和链表的节目。我有五个文件:link.h, link.cpp, bunny.h, bunny.cpp, main.cpp

  

当我尝试编译我的程序时(使用MVS2013),我得到两个错误;   LNK1169:错误3错误LNK1169:找到一个或多个多重定义的符号   和   LNK2005:错误2错误LNK2005:" struct node * curr" (?curr @@ 3PAUnode @@ A)已在list.obj中定义

我不知道错误在哪里,所以我做了一些研究,发现这些错误意味着我已经定义了不止一次,但我无法找到答案。我尝试将所有#includes移动到一个文件中,但这也没有帮助。

我扫描了我的代码,但无法找到符合此错误的任何地方,所以现在我感到难过。

NB!在你因为张贴巨大的文字墙而生我的生气之前,我不知道错误在哪里,所以我认为最好把它全部包括在内

bunny.h

#ifndef BUNNY_H
#define BUNNY_H

#include "stdafx.h"

#include <string>

using namespace std;

class bunny
{
    private:

        int m_age;
        char m_gender;
        bool m_radioactiveVampire;
        string m_color;
        string m_name;

    public: 
        bunny();
        void setRadioactiveVampire(bool RV) {m_radioactiveVampire = RV; }

        string getName(void) { return m_name; }
        string getColor(void) { return m_color; }
        bool getRadioactiveVampire(void) { return m_radioactiveVampire; }
        char getGender(void) { return m_gender; }
        int getAge(void) { return m_age; }
};
#endif

bunny.cpp

#include "stdafx.h"

#include <iostream>

using namespace std;

/* 16 bunnies */ string maleBunnies[] = { "Abel", "Abraham", "Ace", "Acer", "Achilles", "Acker", "Adagio", "Admiral", "Aesop", "Ajax", "Aladdin", "Alaska", "Albert", "Alchemy", "Alex", "Mark" };
/* 16 bunnies */ string femaleBunnies[] = { "Abba", "Aberdeen", "Abigail", "Acura", "Adele", "Adoni", "Aki", "Alibi", "Alice", "Amaretto", "Amaya", "Ambrosia", "Amore", "Amorette", "Anabell", "Anastasia" };
/* 8  colors  */ string bunnyColors[] = { "Red", "Black", "Yellow", "Gray", "Green", "White", "Blue", "Purple" };

bunny::bunny()
{
    m_age = 0;
    m_color = bunnyColors[rand() % 8 + 1];

    if (rand() % 2 + 1 == 1) //calculates what gender and name respectively
    {
        m_gender = 'f';
        m_name = femaleBunnies[rand() % 16 + 1]; 
    }
    else if (rand() % 2 + 1 == 2)
    {
        m_gender = 'm';
        m_name = maleBunnies[rand() % 16 + 1]; 
    }
    else
    {
        cerr << "Uh oh, something went wrong with gender generation";
    }

    if (rand() % 100 + 1 < 2) //calculates 2% chance on radioactive vampire
    {
        m_radioactiveVampire = true; 
    }
    else if (rand() % 100 + 1 > 2)
    {
        m_radioactiveVampire = false; 
    }
    else
    {
        cerr << "Uh oh, something went wrong with RV calculation ";
    }
}

的main.cpp

#include "stdafx.h"
#include <ctime>

using namespace std; 

int main()
{
    srand(time(NULL));
    bunnyList list;
    bunny *b;

    for (int i = 0; i < 5; i++)
    {
        b = new bunny();

        list.addBunny(b);
    }

    return 0;
}

list.h

#ifndef LIST_H
#define LIST_H

#include "bunny.h"
#include "stdafx.h"

typedef struct node
{
    node *next;
    bunny *data;
} *nodePtr;

nodePtr curr = NULL;

class bunnyList
{
    private:

    public:
        node *head; 

        bunnyList();
        void displayAllBunnies(void);
        void addBunny(bunny *b); // linked list stuff
        void removeBunny(int bunnyNumber); // linked list stuff
};

#endif      

和最后一个list.cpp

#include "stdafx.h"
#include <iostream>

using namespace std;

bunnyList::bunnyList()
{
}

void bunnyList::addBunny(bunny *b)
{
    node *newNode = new node; //creates a node and holds its pointer
    newNode->data = b;
    newNode->next = head;
    head = newNode;
}


void bunnyList::displayAllBunnies(void)
{
    curr = head;

    while (curr != NULL)
    {
        cout << curr->data << endl;
        curr = curr->next;
    }
}

(list.h和bunny.h包含在stdafx.h中,这是一个没有帮助的解决方案,但它们都在那里)

1 个答案:

答案 0 :(得分:1)

您正在list.h

中定义变量

nodePtr curr = NULL;

在多个翻译单元中插入.h时,编译器会在翻译单元中报告相同的符号定义。

如果您需要在多个翻译单元中使用该变量,请移动.cpp的变量,使用extern(链接一个帖子以及有关extern的更多信息)。

阅读代码时,没有必要将curr声明为自由变量,它只在displayAllBunnies中用作迭代变量(可能是本地的)。

将displayAllBunnies更改为:

void bunnyList::displayAllBunnies(void) {
    nodePtr curr = head;

    while (curr != NULL) {
        cout << curr->data << endl;
        curr = curr->next;
    }
}

nodePtr curr = NULL;

删除行list.h