templates and translation units

  • Thread starter fabio de francesco
  • Start date
F

fabio de francesco

Hello.

Some time ago I had two different problems in compiling my code using
templates and some of you were able to drive me to a complete solution
that worked with gcc-3.2.3.

Now I have the very same code not compiling with gcc-3.4.1 (only for
one of the two problems fixed at that time) and I can't understand
what has changed and how to solve the problem.

The best way to explain the issue is to show the code itself:


// file database.h

#ifndef _DATABASE_H_
#define _DATABASE_H_

[cut]

template <typename T>
class DataBase;

// friend template functions must be forward declared
template <typename T>
ostream& operator<<( ostream &out, DataBase<T> &db );

template <typename T>
class DataBase
{
public:
DataBase();
private:

[irrelevant code cut]

// Note the unusual symbol <>
friend ostream& operator<< <> ( ostream &out, DataBase<T> &db );
// With your help, still not having problems with the friend
template.
};

#endif

// file database.cpp

#include "person.h" // don't think I need to post person.h
#include "database.h"

// I have been told from you to put the following lines, one line for
each
// different type the class DataBase would be instantiated
template DataBase<Person>; // this is line 37
template DataBase<char*>; // this is line 38

template <typename T> // constructor definition
DataBase<T>::DataBase()
{ ... cut ... }

[irrelevant code cut]


// file usedb.cpp

#include "person.h"
#include "database.h"

int main()
{
// just to quickly run two different instances of DataBase and return
DataBase<Person> dbPerson; // line 11
DataBase<char*> dbChar; // line 12
return 0;
}


The messages I got from the compiler is the following:

/src/database.cpp:37: error: expected unqualified-id before ';'token
/src/database.cpp:38: error: expected unqualified-id before ';'token


If I simply remove the above mentioned lines from database.cpp I got
from the linker:

usedb.o(.text+0x12a): In function `main':
/src/usedb.cpp:11: undefined reference to
`DataBase<Person>::DataBase()'
usedb.o(.text+0x138): In function `main':
/src/usedb.cpp:12: undefined reference to
`DataBase<char*>::DataBase()'

The two messages from the linker are exactly the ones the linker was
producing before some of you said to me to insert lines 37-38 in
database.cpp that eventually worked untill now that I've upgraded the
compiler.

So I think I still need those lines (37-38) in database.cpp but the
new gcc refuses to comply...

Please, can you show me how to make things working again?

Ciao,

Fabio De Francesco
 
V

Victor Bazarov

fabio said:
Some time ago I had two different problems in compiling my code using
templates and some of you were able to drive me to a complete solution
that worked with gcc-3.2.3.

Now I have the very same code not compiling with gcc-3.4.1 (only for
one of the two problems fixed at that time) and I can't understand
what has changed and how to solve the problem.

The best way to explain the issue is to show the code itself:


// file database.h

#ifndef _DATABASE_H_
#define _DATABASE_H_

[cut]

template <typename T>
class DataBase;

// friend template functions must be forward declared
template <typename T>
ostream& operator<<( ostream &out, DataBase<T> &db );

template <typename T>
class DataBase
{
public:
DataBase();
private:

[irrelevant code cut]

// Note the unusual symbol <>
friend ostream& operator<< <> ( ostream &out, DataBase<T> &db );
// With your help, still not having problems with the friend
template.
};

#endif

// file database.cpp

#include "person.h" // don't think I need to post person.h
#include "database.h"

// I have been told from you to put the following lines, one line for
each
// different type the class DataBase would be instantiated
template DataBase<Person>; // this is line 37
template DataBase<char*>; // this is line 38

Shouldn't those be

template class DataBase<Person>;
template class DataBase<char*>;

???
template <typename T> // constructor definition
DataBase<T>::DataBase()
{ ... cut ... }

[irrelevant code cut]


// file usedb.cpp

#include "person.h"
#include "database.h"

int main()
{
// just to quickly run two different instances of DataBase and return
DataBase<Person> dbPerson; // line 11
DataBase<char*> dbChar; // line 12
return 0;
}


The messages I got from the compiler is the following:

/src/database.cpp:37: error: expected unqualified-id before ';'token
/src/database.cpp:38: error: expected unqualified-id before ';'token


If I simply remove the above mentioned lines from database.cpp I got
from the linker:

usedb.o(.text+0x12a): In function `main':
/src/usedb.cpp:11: undefined reference to
`DataBase<Person>::DataBase()'
usedb.o(.text+0x138): In function `main':
/src/usedb.cpp:12: undefined reference to
`DataBase<char*>::DataBase()'

The two messages from the linker are exactly the ones the linker was
producing before some of you said to me to insert lines 37-38 in
database.cpp that eventually worked untill now that I've upgraded the
compiler.

So I think I still need those lines (37-38) in database.cpp but the
new gcc refuses to comply...

Please, can you show me how to make things working again?

See inline.


V
 
F

fabio de francesco

Victor Bazarov said:
fabio said:
Some time ago I had two different problems in compiling my code using
templates and some of you were able to drive me to a complete solution
that worked with gcc-3.2.3.

Now I have the very same code not compiling with gcc-3.4.1 (only for
one of the two problems fixed at that time) and I can't understand
what has changed and how to solve the problem.
// different type the class DataBase would be instantiated
template DataBase<Person>; // this is line 37
template DataBase<char*>; // this is line 38
[cut]


Shouldn't those be

template class DataBase<Person>;
template class DataBase<char*>;

???

Yes, thank you.

I made a mistake in forgeting 'class' qualifier but I'm pretty sure
that it worked with a previous release of gcc, so I didn't notice
something was wrong untill now.

Regards,

Fabio De Francesco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top