473,385 Members | 1,869 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,385 software developers and data experts.

Problem with a template class & friend function

Hi guys, I think from what I found on the net that the code is correct
and there's no problem when I compile the files. The problems occurs
when I link them. I have a friend function which outputs points data to
the ostream. Here is the error message:

objs/generalpolygons.o(.text+0xf08): In function
`GeneralPolygons::readObjFile(std::basic_string<ch ar,
std::char_traits<char>, std::allocator<char)':
core/generalpolygons.cpp:222: undefined reference to
`std::basic_ostream<char, std::char_traits<char& operator<<
<float>(std::basic_ostream<char, std::char_traits<char&,
Point<floatconst&)'

Here is the code:

In point.hpp

// foward declaration needed for the template friend functions
template<typename Tclass Point;
// forward declarion of the Point's friend function
template<typename Tstd::ostream & operator<<( std::ostream &os,
const Point<T&p );

template<typename T>
class Point
{
public:
...
friend std::ostream & operator<< <T>( std::ostream &os, const
Point<T&p );
};

in point.cpp

template<typename T>
std::ostream & operator<<(std::ostream &os, const Point<T&p )
{
os << p.x << " " << p.y << " " << p.z;
return os;
}

how I use it in the code

std::cout << Point<float>( 0.0 ) << std::endl;

Thanks for your help, Mark

Aug 7 '06 #1
8 1955
ma*****@yahoo.com wrote:
Hi guys, I think from what I found on the net that the code is correct
and there's no problem when I compile the files. The problems occurs
when I link them. [..]
This is in the FAQ. See section 35.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '06 #2
I see ... should make a habit to check the faq first.
Thanks, I made the change and it works fine now.

mark -
Victor Bazarov wrote:
ma*****@yahoo.com wrote:
Hi guys, I think from what I found on the net that the code is correct
and there's no problem when I compile the files. The problems occurs
when I link them. [..]

This is in the FAQ. See section 35.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '06 #3

Victor Bazarov wrote:
ma*****@yahoo.com wrote:
Hi guys, I think from what I found on the net that the code is correct
and there's no problem when I compile the files. The problems occurs
when I link them. [..]

This is in the FAQ. See section 35.
I am sorry Victor but I actually compiled the example of the FAQ and I
get the same error. I don't really understand ?
>FOO.HPP <<
#include <iostream>

template<typename Tclass Foo; // pre-declare the template class
itself
template<typename TFoo<Toperator+ (const Foo<T>& lhs, const Foo<T>&
rhs);
template<typename Tstd::ostream& operator<< (std::ostream& o, const
Foo<T>& x);

template<typename T>
class Foo {
public:
Foo(const T& value = T());
friend Foo<Toperator+ <>(const Foo<T>& lhs, const Foo<T>& rhs);
friend std::ostream& operator<< <>(std::ostream& o, const Foo<T>& x);
private:
T value_;
};
>FOO.CPP <<
#include "foo.hpp"

template<typename T>
Foo<T>::Foo(const T& value)
: value_(value)
{ }

template<typename T>
Foo<Toperator+ (const Foo<T>& lhs, const Foo<T>& rhs)
{ return Foo<T>(lhs.value_ + rhs.value_); }

template<typename T>
std::ostream& operator<< (std::ostream& o, const Foo<T>& x)
{ return o << x.value_; }

template class Foo<int>;
>MAIN.CPP <<
#include "foo.hpp"

int main()
{
Foo<intlhs(1);
Foo<intrhs(2);
Foo<intresult = lhs + rhs;
std::cout << result;
}
>COMPILING <<
c++ -c foo.cpp
c++ -c main.cpp
c++ -o main main.o foo.o

main.o(.text+0x63): In function `main':
: undefined reference to `Foo<intoperator+<int>(Foo<intconst&,
Foo<intconst&)'
main.o(.text+0x7f): In function `main':
: undefined reference to `std::basic_ostream<char,
std::char_traits<char& operator<< <int>(std::basic_ostream<char,
std::char_traits<char&, Foo<intconst&)'
collect2: ld returned 1 exit status

Aug 8 '06 #4
ma*****@yahoo.com wrote:
[...]
main.o(.text+0x63): In function `main':
>undefined reference to `Foo<intoperator+<int>(Foo<intconst&,
Foo<intconst&)'
main.o(.text+0x7f): In function `main':
>undefined reference to `std::basic_ostream<char,
std::char_traits<char& operator<< <int>(std::basic_ostream<char,
std::char_traits<char&, Foo<intconst&)'
collect2: ld returned 1 exit status
You instantiated Foo<int>. That instantiates all members of that
class. But it does *not* instantiate non-member functions. If you
want to instantiate those, you need two more explicit instantiations.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #5
>
You instantiated Foo<int>. That instantiates all members of that
class. But it does *not* instantiate non-member functions. If you
want to instantiate those, you need two more explicit instantiations.
Thanks Victor I did the change and now it works fine. Wouldn't it be
worth making the change to the example in the FAQ as well though. In
it's current form, the example won't work ?

-m

Aug 8 '06 #6
ma*****@yahoo.com wrote:
>You instantiated Foo<int>. That instantiates all members of that
class. But it does *not* instantiate non-member functions. If you
want to instantiate those, you need two more explicit instantiations.

Thanks Victor I did the change and now it works fine. Wouldn't it be
worth making the change to the example in the FAQ as well though. In
it's current form, the example won't work ?
What do *you* think? Take the example as is and try it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #7
Thanks Victor I did the change and now it works fine. Wouldn't it be
worth making the change to the example in the FAQ as well though. In
it's current form, the example won't work ?

What do *you* think? Take the example as is and try it.
Sorry I wasn't clear but it's no big deal. I tried the example and it
didn't compile (link) for the reason you mentionned to me. So my
suggestion was that if someone else with little knowledge of C++ was
trying like i did, to compile that same example it won't work for that
other person either, hence the idea of making that small change to the
code in the FAQ, but I wanted the opinion of someone who knows better
;-)

Aug 9 '06 #8
ma*****@yahoo.com wrote:
>>Thanks Victor I did the change and now it works fine. Wouldn't it
be worth making the change to the example in the FAQ as well
though. In it's current form, the example won't work ?

What do *you* think? Take the example as is and try it.

Sorry I wasn't clear but it's no big deal. I tried the example and it
didn't compile (link) for the reason you mentionned to me.
You took it without changing and it didn't work? I just checked 35.15,
and all suggestions worked for me. What is it that didn't work for you?
Read FAQ 5.8 and follow its suggestions. Start another thread if need
be.

Or, did you take the example, then *changed* it, and *then* it didn't
work? I can't help you with that. We cannot anticipate *all* possible
changes one can make to the examples in the FAQ to make them wrong. So
the advice is "don't".
So my
suggestion was that if someone else with little knowledge of C++ was
trying like i did, to compile that same example
Again, changed or not changed? Not changed, it works fine. If it does
not, the compiler is probably somehow disabled (retarded).
it won't work for that
other person either, hence the idea of making that small change to the
code in the FAQ, but I wanted the opinion of someone who knows better
;-)
Please contact Marshall Cline ans suggest the change. You will need to
actually formulate it instead of saying "I tried and it didn't work".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #9

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

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
2
by: PengYu.UT | last post by:
The following program works with g++3.3. I'm assuming g++-3.4 is more standard conforming that g++-3.3. Would you please tell me what is right way to declare a template friend function of a...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.