F
fabio de francesco
Hello,
I'm just joking with the Subject, but I really don't know how to make
a synthesis of two questions about some code I'm trying to write.
In the following I post this little code, with "..." meaning what I
think can be omitted for brevity.
// file database.h
....
template <typename T>
class DataBase
{
public:
DataBase();
...
private:
...
void run();
ostream& print( ostream & );
friend ostream& operator<<(ostream &out, DataBase<T> &db)
{ return db.print( out ); }
};
....
// file database.cpp
....
template <typename T>
DataBase<T>:ataBase()
{...}
template<typename T>
void DataBase<T>::run()
{...}
....
// file usedb.cpp
....
#include "database.h"
#include "person.h"
....
int main()
{
DataBase<Person> db; // usedb.cpp:11 (error)
...
return 0;
}
The first problem is that the linker editor ( I suppose ) comes out
with this error:
/database/src/usedb.cpp:11: undefined reference to
`DataBase<Person>:ataBase[in-charge]()'
I got the same error also if I try to instantiate 'DataBase<int> db'.
This is not the first time I use templates but I've never seen such an
error.
I'm sure I am doing some stupid thing.
The second problem arises if I try to extract the implementation code
of the friend overloaded operator "<<" from the class definition (
database.h ) to be put in the implementation file ( database.cpp ).
// file DataBase.h
....
friend ostream& operator<<( ostream &out, DataBase<T> &db );
....
// file DataBase.cpp
....
template<typename T>
ostream& operator<<( ostream &out, DataBase<T> &db )
{ return db.print( out ); }
....
Here the errors, while compiling:
/src/database.h:31: warning: friend declaration `std:stream&
operator<<(std:stream&, DataBase<T>&)' declares a non-template
function
/src/database.h:31: warning: (if this is not what you intended, make
sure the function template has already been declared and add <> after
the function name here) -Wno-non-template-friend disables this warning
That's all. I want to thank in advance everyone who will explain what
kind of errors I put in the code. I would appreciate also any partial
reply on the first topic that is the most important for me.
Ciao,
Fabio De Francesco.
I'm just joking with the Subject, but I really don't know how to make
a synthesis of two questions about some code I'm trying to write.
In the following I post this little code, with "..." meaning what I
think can be omitted for brevity.
// file database.h
....
template <typename T>
class DataBase
{
public:
DataBase();
...
private:
...
void run();
ostream& print( ostream & );
friend ostream& operator<<(ostream &out, DataBase<T> &db)
{ return db.print( out ); }
};
....
// file database.cpp
....
template <typename T>
DataBase<T>:ataBase()
{...}
template<typename T>
void DataBase<T>::run()
{...}
....
// file usedb.cpp
....
#include "database.h"
#include "person.h"
....
int main()
{
DataBase<Person> db; // usedb.cpp:11 (error)
...
return 0;
}
The first problem is that the linker editor ( I suppose ) comes out
with this error:
/database/src/usedb.cpp:11: undefined reference to
`DataBase<Person>:ataBase[in-charge]()'
I got the same error also if I try to instantiate 'DataBase<int> db'.
This is not the first time I use templates but I've never seen such an
error.
I'm sure I am doing some stupid thing.
The second problem arises if I try to extract the implementation code
of the friend overloaded operator "<<" from the class definition (
database.h ) to be put in the implementation file ( database.cpp ).
// file DataBase.h
....
friend ostream& operator<<( ostream &out, DataBase<T> &db );
....
// file DataBase.cpp
....
template<typename T>
ostream& operator<<( ostream &out, DataBase<T> &db )
{ return db.print( out ); }
....
Here the errors, while compiling:
/src/database.h:31: warning: friend declaration `std:stream&
operator<<(std:stream&, DataBase<T>&)' declares a non-template
function
/src/database.h:31: warning: (if this is not what you intended, make
sure the function template has already been declared and add <> after
the function name here) -Wno-non-template-friend disables this warning
That's all. I want to thank in advance everyone who will explain what
kind of errors I put in the code. I would appreciate also any partial
reply on the first topic that is the most important for me.
Ciao,
Fabio De Francesco.