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>:ataBase()
{ ... 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>:ataBase()'
usedb.o(.text+0x138): In function `main':
/src/usedb.cpp:12: undefined reference to
`DataBase<char*>:ataBase()'
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
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>:ataBase()
{ ... 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>:ataBase()'
usedb.o(.text+0x138): In function `main':
/src/usedb.cpp:12: undefined reference to
`DataBase<char*>:ataBase()'
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