I have a base class:
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
And a few derived classes, example given:
class Bar : public Foo {
public:
Bar();
private:
const char* getClassName()
{
return "Bar";
}
};
The above code gives "undefined reference to Foo::getClassName()" which Im assuming because the code wants to call Foo::getClassName(), but how do I get it to call the function like a virtual call normally? I.E. How do I get it to call Bar::getClassName() from inside Foo?
EDIT: Forgot inheritance thingy
Items in fooList
must be created with new
, fooList[0] = new Bar()
. And Bar
must inherit from Foo
, as WeaselFox said.
There are two things that are undefined:
Bar::Bar() is undefined
- Bar();
+ Bar() {}
and fooList is undefined:
+std::vector<Foo*> Foo::fooList;
Here is corrected program:
test.cpp:
#include <vector>
#include <iostream>
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
std::vector<Foo*> Foo::fooList;
class Bar : public Foo {
public:
Bar() {};
private:
const char* getClassName()
{
return "Bar";
}
};
int main()
{
//intentionally leaked
Foo::fooList.push_back(new Bar());
Foo::fooList.push_back(new Bar());
Foo::fooList.push_back(new Bar());
Foo::printFoos();
}
output:
Class: Bar
Class: Bar
Class: Bar
It seems bar
does not inherit foo
. You need to declare inheritance:
class bar: public foo { ...
I have a base class:
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
And a few derived classes, example given:
class Bar : public Foo {
public:
Bar();
private:
const char* getClassName()
{
return "Bar";
}
};
The above code gives "undefined reference to Foo::getClassName()" which Im assuming because the code wants to call Foo::getClassName(), but how do I get it to call the function like a virtual call normally? I.E. How do I get it to call Bar::getClassName() from inside Foo?
EDIT: Forgot inheritance thingy
Items in fooList
must be created with new
, fooList[0] = new Bar()
. And Bar
must inherit from Foo
, as WeaselFox said.
There are two things that are undefined:
Bar::Bar() is undefined
- Bar();
+ Bar() {}
and fooList is undefined:
+std::vector<Foo*> Foo::fooList;
Here is corrected program:
test.cpp:
#include <vector>
#include <iostream>
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
std::vector<Foo*> Foo::fooList;
class Bar : public Foo {
public:
Bar() {};
private:
const char* getClassName()
{
return "Bar";
}
};
int main()
{
//intentionally leaked
Foo::fooList.push_back(new Bar());
Foo::fooList.push_back(new Bar());
Foo::fooList.push_back(new Bar());
Foo::printFoos();
}
output:
Class: Bar
Class: Bar
Class: Bar
It seems bar
does not inherit foo
. You need to declare inheritance:
class bar: public foo { ...
0 commentaires:
Enregistrer un commentaire