0

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)

chwarr
  • 6,777
  • 1
  • 30
  • 57
Voluptious
  • 13
  • 4
  • also I think you are not supposed to have stdafx.h in the header file. The compiler ignores everything before that line, which will cause problems. – M.M Apr 01 '15 at 22:37

1 Answers1

1

You are defining variables in the list.h

nodePtr curr = NULL;

When inserting the .h in multiple translation units, the compiler report the same symbol define in the translation units.

Move the variable for the .cpp if you need to use the variable in multiple translation units, use extern (linked a post with more info about extern).

Reading the code, there's no necessity of declaring curr as a free variable, it's only used in displayAllBunnies as an iteration variable (that could be local).

Change displayAllBunnies to:

void bunnyList::displayAllBunnies(void) {
    nodePtr curr = head;

    while (curr != NULL) {
        cout << curr->data << endl;
        curr = curr->next;
    }
}

And remove line: nodePtr curr = NULL; from list.h

Community
  • 1
  • 1
NetVipeC
  • 4,402
  • 1
  • 17
  • 19