我正在製作一個關於兔子和鏈表的程序。我有五個文件: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中,這是一個解決方案,沒有幫助,但他們在那裏)
也是我認爲你不應該在頭文件stdafx.h中。編譯器忽略該行之前的所有內容,這會導致問題。 – 2015-04-01 22:37:36