function template question

Glenn MacGregor gtm@highstreetnetworks.com
Tue Mar 30 00:21:00 GMT 2004


Hi all,

I have a question about the templates (function templates). It may be best 
described using an example.

contents of Module.h file

class Module 
{
public:
 Module()
...

 template<class T> void forEach(T &t);
...
private:
 string mName;
 vector<string> mParamNames;
};


contents of Module.cpp file

Module::Module() :
mName(),
mParamNames()
{
}

...

template<class T>
void
Module::forEach(T &t)
{
  ::for_each(mParamName.begin(), mParamNames.end(), t());
}


contents of file CellObject.cpp

#include "Module.h"

...
...

struct forEachObj
{
private:
 string mText;
public:
 forEachObj(const string &s) : mText(s) {};

 void operator()(int) {
   string test = "test";
   test += mText;
 };
};

CellObject::func1()
{
  Module *m = new Module("test");
  m->addParamName("p1");
  m->addParamName("p2");
  m->addParamName("p3");
  m->addParamName("p4");

  forEachObj f("HERE!");
  m->forEach(f);
}

I am using gcc 3.2.2 and ld 2.13.90.0.2 on RedHat 8 updated. All compilation is 
fine but the linker fails with the following message:

undefined reference to `void Module::forEach<forEachObj>(forEachObj&)'

I assume this means that the compiler did not create the code from the template 
when it say the call to the function in the CellObject.cpp file. Is this 
correct? If so did I do something wrong? Am I missing something here?

  Thanks so much!

   Glenn


Glenn MacGregor
HighStreet Networks

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/



More information about the Gcc mailing list