0

I'm having problems with the linker.

It gives me the following error:

[Linker error] undefined reference to `bool Polis :: deleteEdifice <Mine> (int) '

The prototype is declared as follows:

template <typename T> bool deleteEdifice(int);

deleteEdifice is empty, that does not do anything for now but it does not work.

The call to the prototype is:

obj->deleteEdifice<Mine>(3);

I also tried to do:

obj->template deleteEdifice<Mine>(3);

Print the following error:

`template' (as a disambiguator) is only allowed within templates

Could you tell me what am I doing wrong.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
angel
  • 163
  • 2
  • 10
  • "deleteEdifice is empty, that does not do anything for now but it does not work." Please clarify. What does it mean? Is there any code that relates to this sentence? Can you show it? – n. m. could be an AI Mar 26 '14 at 21:13
  • 2
    You have to define the template in the header file, not just declare it. `template bool deleteEdifice(int){}` – clcto Mar 26 '14 at 21:13
  • Is the definition of `deleteEdifice` in a .cpp file? –  Mar 26 '14 at 21:14
  • Sorry. I deleted my comment. I'll post an answer, but faranwath asked a very good question. The definition of templates should be in h files. – Werner Erasmus Mar 26 '14 at 21:15
  • Based on the way he said, "The prototype is declared as follows" I believe that he defined it in a cpp file. – clcto Mar 26 '14 at 21:16

2 Answers2

0

This

template <typename T> bool deleteEdifice(int);

does not mean it is empty, it means it is undefined like the error [Linker error] undefined reference

This

template <typename T> bool deleteEdifice(int) { return false; }

is empty

Claudiordgz
  • 3,023
  • 1
  • 21
  • 48
  • I did the same thing, it gives me the following error: [Linker error] undefined reference to `bool Polis :: deleteEdifice (int) ' – angel Mar 26 '14 at 21:25
0

I'm having problems with the linker. It gives me the following error: [Linker error] undefined reference to `bool Polis :: deleteEdifice (int) '

I'm assuming your code looks like this:

//.h file
class Polis
{
  public:
    //...constructor etc...
    template <class T>
    bool deleteEdifice( int );
};

Also .h file

//, where definition of template is visible at 
// point of instantiation - remember, template is instantiated
// when used (in compilation unit where used), therefore its 
// definition has to be visible in the scope where used. It is
// only instantiated when used... If it's definition is not 
// available at the point of instantiation (where called for 
// a function template), it is never 
// compiled.

template <class T>
bool Polis::deleteEdifice( int )
{
  //Implementation here...
}
Werner Erasmus
  • 3,988
  • 17
  • 31
  • I did the same thing, it gives me the following error: [Linker error] undefined reference to `bool Polis :: deleteEdifice (int) ' – angel Mar 26 '14 at 21:23