2015-04-01 71 views
0

我正在制作一个关于兔子和链表的程序。我有五个文件:link.h, link.cpp, bunny.h, bunny.cpp, main.cpp无论我做什么,我都会得到LNK1169和LNK2005

当我尝试编译我的程序(使用MVS2013)我得到两个错误; LNK1169:错误3错误LNK1169:一个或一个以上乘法定义的符号发现 和 LNK2005:错误2错误LNK2005: “结构节点* CURR”(CURR @@ 3PAUnode @@ A 2)在list.obj已经定义

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

我扫描了我的代码,但找不到适合这个错误的地方,所以现在我很难过。

注意!我发脾气张贴文字的巨墙之前,我不知道是哪里的错误所在,所以我认为这是最好的,包括它所有

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 

兔子。 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中,这是一个解决方案,没有帮助,但他们在那里)

+0

也是我认为你不应该在头文件stdafx.h中。编译器忽略该行之前的所有内容,这会导致问题。 – 2015-04-01 22:37:36

回答

1

你正在定义在list.h

nodePtr curr = NULL;

变量当在多个TRA插入.h nslation单位,编译器报告在翻译单位定义相同的符号。

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

读取代码时,没有必要声明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

+0

将其移动到列表中。CPP来解决这个问题,谢谢。我在最后一句中看到你的意思 – Voluptious 2015-04-01 20:58:21

相关问题