473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

errors = templates * friends;

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>::DataBase()
{...}
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>::DataBase[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::ostream&
operator<<(std::ostream&, 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.
Jul 22 '05 #1
7 1314
fabio de francesco wrote in
news:ba**************************@posting.google.c om in comp.lang.c++:
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 & );
This is a non-template function, you can't define it outside of the
class-template, as you try to do later.
friend ostream& operator<<(ostream &out, DataBase<T> &db)
{ return db.print( out ); }
};
...

Here's your first problem, templates need to be defined in the
the same compilation that uses them, put these defenition's in
your header file.
// file database.cpp
...
template <typename T>
DataBase<T>::DataBase()
{...}
template<typename T>
void DataBase<T>::run()
{...}
...

[snip]

Here's an example of a template friend (note all the cruft):

#include <iostream>
#include <ostream>
/* Forward declare X, so we can forward declare operator <<
*/
template < typename T > struct X;
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & );
template < typename T >
struct X
{
friend /* Note the <> */
std::ostream & operator << <>( std::ostream &os, X< T > const & );
};

/* declaration/defenition - in the *header*
*/
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & )
{
os << "friend\n";
return os;
}

int main()
{
X< int > x;
std::cout << x;
}

Alternativly do it like this:

#include <iostream>
#include <ostream>

template < typename T >
struct X
{
private:
void printon( std::ostream &os ) const;
friend std::ostream & operator << ( std::ostream &os, X< T > const &x )
{
x.printon( os );
return os;
}
};
template < typename T >
void X< T >::printon( std::ostream &os ) const
{
os << "friend\n";
}

int main()
{
X< int > x;
std::cout << x;
}

Slightly less cruft :).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
>
Here's an example of a template friend (note all the cruft):

#include <iostream>
#include <ostream>
/* Forward declare X, so we can forward declare operator <<
*/
template < typename T > struct X;
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & );


This confuses me, I know what to do but I still don't understand why.

Why is it necessary to forward declare the operator in this case?

john
Jul 22 '05 #3
John Harrison wrote in news:2k*************@uni-berlin.de in
comp.lang.c++:

Here's an example of a template friend (note all the cruft):

#include <iostream>
#include <ostream>
/* Forward declare X, so we can forward declare operator <<
*/
template < typename T > struct X;
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & );


This confuses me, I know what to do but I still don't understand why.

Why is it necessary to forward declare the operator in this case?


Because the friend /statement/ in this:

template < typename T >
struct X
{
friend /* Note the <> */
std::ostream & operator << <>( std::ostream &os, X< T > const & );
};

isn't a declaration.

The following example contains a declaration but its to generic, i.e. it
grants friendship to widely, not that that is ever a /real/ problem.

#include <iostream>
#include <ostream>

template < typename T >
struct Y
{
template < typename U >
friend
std::ostream &operator << ( std::ostream &os, Y< U > const & );

};

template < typename T >
std::ostream & operator <<( std::ostream &os, Y< T > const & )
{
os << "friend\n";
return os;
}

int main()
{
Y< int > y;
std::cout << y;
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
John Harrison wrote in news:2k*************@uni-berlin.de in
comp.lang.c++:

Here's an example of a template friend (note all the cruft):

#include <iostream>
#include <ostream>
/* Forward declare X, so we can forward declare operator <<
*/
template < typename T > struct X;
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & );


This confuses me, I know what to do but I still don't understand why.

Why is it necessary to forward declare the operator in this case?


Because the friend /statement/ in this:

template < typename T >
struct X
{
friend /* Note the <> */
std::ostream & operator << <>( std::ostream &os, X< T > const & );
};

isn't a declaration.


I think I'd quibble with your terminology. I guess (if I understand you
right) that I'd say although the above does declare friendship it doesn't
declare the function template itself. Which thinking about it seems
reasonable enough, it doesn't look anything like a function template
declaration.

Thanks, John
Jul 22 '05 #5
Rob Williscroft <rt*@freenet.co.uk> wrote in message news:<Xn**********************************@130.133 .1.4>...
Here's your first problem, templates need to be defined in the
the same compilation that uses them, put these defenition's in
your header file.
// file database.cpp
...
template <typename T>
DataBase<T>::DataBase()
{...}
template<typename T>
void DataBase<T>::run()
{...}
... [snip]

.... /* Forward declare X, so we can forward declare operator <<
*/
template < typename T > struct X;
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & );
template < typename T >
struct X
{
friend /* Note the <> */
std::ostream & operator << <>( std::ostream &os, X< T > const & );
};

/* declaration/defenition - in the *header*
*/
template < typename T >
std::ostream & operator << ( std::ostream &os, X< T > const & )
{
os << "friend\n";
return os;
}

int main()
{
X< int > x;
std::cout << x;
}


Thank you Rob.

I actually solved the problem by using the forward declarations plus
the "<>" notation as you wrote, but fortunately I wasn't to much
inclined to put the definitions of the functions ( that are a thousand
of lines of code ) into the header file.

With this is my mind I found a way to keep untouched the code in
database.cpp by adding pre-declarations of the possible use of
template parameters ( I'm not sure I can name them this way ),

So I added ( in database.cpp ):

#include "person.h"
template DataBase<Person>;

#include "myfriends"
template DataBase<MyFriends>;

and so on. Now I can ( in usedb.cpp ) instantiate

DataBase<Person> dbPerson;
DataBase<MyFriends> dbMyFriends;

with no more linker editor errors.

Your advice was enough to make me put that friend function
implementation in database.cpp too, without any further modification.

Ciao,
Fabio De Francesco.
Jul 22 '05 #6
"fabio de francesco" <fm**@tiscali.it> schreef in bericht
news:ba**************************@posting.google.c om...
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>::DataBase()
{...}
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>::DataBase[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::ostream&
operator<<(std::ostream&, 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.

Templates that will be used in multiple files have to be implemented in the
header file.
That's all, I think.
Jul 22 '05 #7
"Wouter Lievens" <li***********@snotmail.com> wrote in message news:<40**********************@news.skynet.be>...
Templates that will be used in multiple files have to be implemented in the
header file.
That's all, I think.


I think, with all respect, you'd better read all the thread, in which
more solutions have been found, before stating something that has been
outdone a few days ago (without implementing templates in the header
file).

I don't want to be polemical, it is just that someone can rely on what
is added at the end of a thread by thinking that some steps forward
have been taken after days of discussion.

Regards,

Fabio De Francesco
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gianni Mariani | last post by:
I have 2 distinct template classes which co-operate, hence are friends. However, I can't seem to figure out what syntax to use to make this work. What is the right(tm) way to write a friend...
10
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
12
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
1
by: Rebecca Hoffmann | last post by:
Hi, I have a serious problem while compiling a small project (a part of the Modular Flow Scheduling Middleware: ex1): There are 3 linker errors, all from symbols that point to templates: --...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
2
by: Niklas Norrthon | last post by:
I want to share a technique I recently have found to be useful to get around some obstacles that data protection can raise. Consider the following class: // foo.h #ifndef H_FOO #define H_FOO...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
1
by: deepaks85 | last post by:
Hi Friends, I am looking for some good web banners or some good web design templates. I am a web designer and developer and I want some banners and templates for my own website so that I can make...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.