i am making a program regarding bunnies and linked lists. I have five files: 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
I have no idea where the error lies, so i did some research and found out these errors mean that i have defined something more than once, but i cant find out where. I tried moving all #includes to one file but that didn't help either.
I have scanned my code but cant find anywhere that fits this error, so now i'm stumped.
NB! Before you get mad at me for posting a giant wall of text, i don't know where the error lies, so i thought it was best to include it all
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
and last 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 and bunny.h is included in stdafx.h, this was one solution that didnt help, but they are there)