C++ Interface Throwing Unresolved External Symbol Error
Question
Alright, I've got what should be an easy question but there are literally no examples of what I am looking for online, only copies of the same example which does everything in one file. I'm trying to make an interface and a method to use it like the example below, but I am getting unresolved external symbol errors from visual studio when I compile. Any ideas about what I am doing wrong?
IFoo.h
class IFoo {
public:
virutal int Bar();
}
Foo.h
class Foo : public IFoo {
virtual int Bar();
}
Foo.cpp
int Foo::Bar() {
return 1;
}
Solution
In IFoo.h, the virtual method must be pure virtual:
class IFoo {
public:
virtual int Bar() = 0;
};
Alternately, provide an default implementation, either inline or in an IFoo.cpp. However, that would make the I prefix misleading, as it wouldn't act like an interface, then.
OTHER TIPS
Assuming that the unresolved symbol is for int IFoo::Bar()
, please note that this function is declared to be a callable function and it can, indeed, be called, e.g. using
p->IFoo::Bar()
That is, you either need to define this function or you need to declare it as not existing:
virtual int Bar() = 0;
(which still allows you to define it, though).