views:

52

answers:

2

I implemented the "Strategy" design pattern using an Abstract template class, and two subclasses. Goes like this:

template <class T> 
class Neighbourhood {
public:
  virtual void alter(std::vector<T>& array, int i1, int i2) = 0;
};

and

template <class T> 
class Swap : public Neighbourhood<T> {
public:
  virtual void alter(std::vector<T>& array, int i1, int i2);
};

There's another subclass, just like this one, and alter is implemented in the cpp file. Ok, fine! Now I declare another method, in another class (including neighbourhood header file, of course), like this:

void lSearch(/*parameters*/, Neighbourhood<LotSolutionInformation> nhood);

It compiles fine and cleanly. When starting to link, I get the following error:

1>SolverFV.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall lsc::Neighbourhood<class LotSolutionInformation>::alter(class std::vector<class LotSolutionInformation,class std::allocator<class LotSolutionInformation> > &,int,int)" (?alter@?$Neighbourhood@VLotSolutionInformation@@@lsc@@UAEXAAV?$vector@VLotSolutionInformation@@V?$allocator@VLotSolutionInformation@@@std@@@std@@HH@Z)
+3  A: 

There's another subclass, just like this one, and alter is implemented in the cpp file.

No can do - it's got to be in the header.

anon
Oh, yeah, this too. Since C++ is great for a plethora of things, I had even forgotten how it sucked as an OO language... :/
Luís Guilherme
A: 

It seems it was a pretty rookie mistake. Since Neighbourhood is an abstract class, I must use it always as a pointer (EDIT: or a reference), since it must never be instantiated.

Changed signatures and it worked fine.

EDIT: Also, thanks to Neil, I know "it's got to be in the header".

Luís Guilherme
In fact, you might prefer using references to Neighbourhood instead of pointers.
Luc Touraille
I would upvote if it wasn't saying you have to use pointers. What @Luc says
Johannes Schaub - litb
Thanks you both :)
Luís Guilherme