Hey guys i need help! I spent hours and hours looking online but none had the same problem as me. Basically, I have a base class called MainShop and it has 3 derived classes which are SwordShop, SpellBookShop and BowShop. I want the base class to be able to call a function from one of the derived classes but no matter what i do, it doesn't seem to work!
Here is my code:
#include "MainShop.h"
//BaseClass cpp
void MainShop::EnterShop(Hero& hero)
{
//Display Choices
switch (choice)
{
//Swords
case 1: SwordShop::soldierShop(hero);//DOES NOT WORK!!
break;
case 2: SpellBookShop::MageShop(hero);//Staffs
break;
case 3: BowShop::ArcherShop(hero);//Bows
break;
default: cout << "Error!, Please try again.";
MainShop::EnterShop(hero);
}
}
I have two other derived classes, but its basically the same concept. I have a function in one of the derived classes and i would like to call it from the base class. This is one my derived classes:
#include <iostream>
usingnamespace std;
struct Base
{
virtual ~Base() { } // Doesn't do anything, but you need it anyway
void hello() const // Base class function that calls derived stuff
{
do_hello();
}
virtualvoid do_hello() const
{
cout << "Hello from Base!\n";
}
};
struct Derived: public Base
{
virtualvoid do_hello() const
{
cout << "Hello from Derived!\n";
}
};
27 28 29 30 31 32 33 34 35 36 37 38 39
// Version using pointers to Base
int main()
{
Base* base = new Base;
Base* derived = new Derived;
base->hello();
derived->hello();
delete derived;
delete base;
}
27 28 29 30 31 32 33 34 35 36
// Version using local Base object
int main()
{
Base base;
Derived derived;
base.hello();
derived.hello();
}
2) You can declare the function as a static, so you can call it like you did above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class SwordShop {
public:
staticvoid soldierShop(Hero& hero);
// This func can only access data members declared as static
};
void MainShop::enterShop(Hero& hero) {
switch (choice) {
case 1:
SwordShop::soldierShop(hero);
break;
// etc
}
}
3) You can make each of the shops inherit from MainShop, and have MainShop have a virtual function for each of the shops that can be called from the shop, similar to @Duoas' example above, and all you do is have a seperate, non-member function that is passed an instance of a "shop" and call its "doShop" function (or whatever you call it).
Ld /Users/Damian/Library/Developer
/Xcode/DerivedData/SampleClass1
-dmwbviaemubkqddiigadavsgeknm/Build/
Products/Debug/SampleClass1
normal x86_64
cd /Users/Damian/Documents/Xcode/SampleClass1
setenv MACOSX_DEPLOYMENT_TARGET 10.9
/Applications
/Xcode.app/Contents/Developer/Toolchains/
XcodeDefault.xctoolchain/usr/bin/clang++ -arch
x86_64 -isysroot /Applications/Xcode.app
/Contents/Developer/Platforms/MacOSX.platform/
Developer/SDKs/MacOSX10.9.sdk -L/Users/Damian
/Library
/Developer
/Xcode/DerivedData
/SampleClass1-dmwbviaemubkqddiigadavsgeknm
/Build/Products/
Debug -F/Users/Damian
/Library/Developer/Xcode/DerivedData/
SampleClass1-dmwbviaem
ubkqddiigadavsgeknm/Build/Products/Debug -filelist
/Users/Damian
/Library/Developer/Xcode/DerivedData
/SampleClass1-dmwbviaemubkqddiigadavsgeknm/
Build/Intermediates/SampleClass1.build/
Debug/SampleClass1.build/Objects-normal/x86_64/
SampleClass1.LinkFileList
-mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker
-dependency_info
-Xlinker /Users/Damian/Library/Developer/Xcode/
DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/
SampleClass1.build/
Debug/SampleClass1.build/Objects-normal/x86_64/SampleClass1_dependency_info.dat -o
/Users
/Damian/Library/Developer/Xcode/DerivedData/
SampleClass1-dmwbviaemubkqddiigadavsgeknm/
Build/
Products/Debug/
SampleClass1
Undefined symbols for architecture x86_64:
"vtable for PotionShop", referenced from:
PotionShop::PotionShop() in PotionShop.o
NOTE: a missing vtable usually means the first non-inlinevirtual member function has no definition.
"vtable for SpellBookShop", referenced from:
SpellBookShop::SpellBookShop() in SpellBookShop.o
NOTE: a missing vtable usually means the first non-inlinevirtual member function has no definition.
"vtable for BowShop", referenced from:
BowShop::BowShop() in BowShop.o
NOTE: a missing vtable usually means the first non-inlinevirtual member function has no definition.
"vtable for SwordShop", referenced from:
SwordShop::SwordShop() in SwordShop.o
NOTE: a missing vtable usually means the first non-inlinevirtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
(use -v to see invocation)