无论我做什么,我都会得到 LNK1169 和 LNK2005
I get LNK1169 and LNK2005 no matter what i do
我正在制作一个关于兔子和链表的程序。我有五个文件:link.h, link.cpp, bunny.h, bunny.cpp, main.cpp
When i try to compile my program (Using MVS2013) i get two errors;
LNK1169: Error 3 error LNK1169: one or more multiply defined symbols found
and
LNK2005: Error 2 error LNK2005: "struct node * curr" (?curr@@3PAUnode@@A) already defined in 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
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 中,这是一个没有帮助的解决方案,但它们在那里)
您正在 list.h
中定义变量
nodePtr curr = NULL;
在多个翻译单元中插入 .h
时,编译器报告翻译单元中定义了相同的符号。
如果您需要在多个翻译单元中使用变量,请移动 .cpp
的变量,使用 extern
(链接 post 以获取有关 extern 的更多信息)。
阅读代码,没有必要将 curr
声明为自由变量,它仅在 displayAllBunnies
中用作迭代变量(可以是局部变量)。
将 displayAllBunnies 更改为:
void bunnyList::displayAllBunnies(void) {
nodePtr curr = head;
while (curr != NULL) {
cout << curr->data << endl;
curr = curr->next;
}
}
并从 list.h
中删除行:nodePtr curr = NULL;
我正在制作一个关于兔子和链表的程序。我有五个文件:link.h, link.cpp, bunny.h, bunny.cpp, main.cpp
When i try to compile my program (Using MVS2013) i get two errors; LNK1169: Error 3 error LNK1169: one or more multiply defined symbols found and LNK2005: Error 2 error LNK2005: "struct node * curr" (?curr@@3PAUnode@@A) already defined in 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
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 中,这是一个没有帮助的解决方案,但它们在那里)
您正在 list.h
nodePtr curr = NULL;
在多个翻译单元中插入 .h
时,编译器报告翻译单元中定义了相同的符号。
如果您需要在多个翻译单元中使用变量,请移动 .cpp
的变量,使用 extern
(链接 post 以获取有关 extern 的更多信息)。
阅读代码,没有必要将 curr
声明为自由变量,它仅在 displayAllBunnies
中用作迭代变量(可以是局部变量)。
将 displayAllBunnies 更改为:
void bunnyList::displayAllBunnies(void) {
nodePtr curr = head;
while (curr != NULL) {
cout << curr->data << endl;
curr = curr->next;
}
}
并从 list.h
nodePtr curr = NULL;