Question

Okay so I have a simple hero class, which i passed into my MainShop class. The problem is i want to know how to get these virtual functions to work. I declared a pure virtual function in my "Base" class which is MainShop, and all of my derived classes inherit from it, but every time i override the virtual function in more than one derived class i get a linker error saying i have multiple symbols defined.

Base Class:

//MainShop.h
#ifndef __SampleClass1__MainShop__
#define __SampleClass1__MainShop__

#include "Hero.h"
class MainShop
{
   public:
    void mainShop(Hero& hero);
    virtual ~MainShop(){}  // Doesn't do anything, but you need it anyway 
    virtual void EnterShop(Hero &hero)=0;//abstract
};
#endif /* defined(__SampleClass1__MainShop__) */


//MainShop.cpp
#include "MainShop.h"

void MainShop::mainShop(Hero& hero)
{
    //Some code here
    EnterShop(hero);//virtual function
}

Derived Classes:

//SwordShop.h
#ifndef __SampleClass1__SwordShop__
#define __SampleClass1__SwordShop__

#include "MainShop.h"
class SwordShop: public MainShop
{
    public:
    void soldierShop(Hero& hero);
};

#endif /* defined(__SampleClass1__SwordShop__) */


//SwordShop.cpp
#include "SwordShop.h"
void soldierShop(hero)
{
    //Some code here
}

void EnterShop(Hero& hero)
{
    //I WANT TO BE ABLE TO CALL soldierShop FROM WITHIN THIS EnterShop() FUNCTION
    cout << "Inside swordshop";//Debug Purposes
    //SwordShop Sword;
    //Sword.soldierShop(hero);
    //MainShop *swordShop = &Sword;
    // calling soldierShop
    //swordShop->EnterShop(hero);
}


//SpellBookShop.h
#ifndef __SampleClass1__SpellBookShop__
#define __SampleClass1__SpellBookShop__

#include "MainShop.h"

class SpellBookShop: public MainShop
{  
   public:
    void MageShop(Hero& hero);
};

#endif /* defined(__SampleClass1__SpellBookShop__) */


//SpellBookShop.cpp
#include "SpellBookShop.h"

void SpellBookShop::MageShop(Hero& hero)
{
    //Some Code Here
}

void EnterShop(Hero& hero)//trying to override virtual function
{
    //I WANT TO BE ABLE TO CALL MageShop FROM WITHIN THIS EnterShop() FUNCTION
    cout << "Inside Spell Shop\n";//Debug Purposes
}

Aside from SwordShop and SpellBookShop, i have two more classes which are BowShop and PotionShop each separated into their .h and .cpp files, but i didn't include them because they're exactly the same as these two classes so it's redundant.

When i was debugging i tried commenting out every virtual function i tried to override and i found out that i can only override one. If i try to override another one it gives me that error. For example: in SwordShop.cpp i can do:

void EnterShop(Hero& hero)
{
    cout << "Inside swordshop";//Debug Purposes
}

but if i try to do the same in, lets say, BowShop or SpellBookShop also, it gives me that error.

If you guys want to see the error, here it is:

Duplicate symbol __Z9EnterShopR4Hero in:
    /Users/Damian/Library/Developer/Xcode/DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/SampleClass1.build/Debug/SampleClass1.build/Objects-normal/x86_64/PotionShop.o
    /Users/Damian/Library/Developer/Xcode/DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/SampleClass1.build/Debug/SampleClass1.build/Objects-normal/x86_64/SpellBookShop.o
duplicate symbol __Z9EnterShopR4Hero in:
    /Users/Damian/Library/Developer/Xcode/DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/SampleClass1.build/Debug/SampleClass1.build/Objects-normal/x86_64/PotionShop.o
    /Users/Damian/Library/Developer/Xcode/DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/SampleClass1.build/Debug/SampleClass1.build/Objects-normal/x86_64/SwordShop.o
duplicate symbol __Z9EnterShopR4Hero in:
    /Users/Damian/Library/Developer/Xcode/DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/SampleClass1.build/Debug/SampleClass1.build/Objects-normal/x86_64/PotionShop.o
    /Users/Damian/Library/Developer/Xcode/DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/SampleClass1.build/Debug/SampleClass1.build/Objects-normal/x86_64/BowShop.o
ld: 3 duplicate symbols for architecture x86_64

Any kind of help is appreciated! I've been trying to solve this error for days now and to no avail! I even tried searching online, but they all showed examples of people overriding their virtual functions just fine. Am i doing anything wrong? I don't see how its different from others. Thanks!

Était-ce utile?

La solution

You need to tell the compiler which EnterShop function you're implementing. You can do that by prefixing it with the class name, as in:

void SwordShop::EnterShop(Hero& hero)
{
    cout << "Inside swordshop";//Debug Purposes
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top