2015-04-01 75 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

相關問題