Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with a template class & friend function

Reply
Thread Tools

Problem with a template class & friend function

 
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      08-07-2006
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+0xf0: 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<float> const&)'

Here is the code:

In point.hpp

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

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

in point.cpp

template<typename T>
std:stream & operator<<(std:stream &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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      08-07-2006
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:
> (E-Mail Removed) 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


 
Reply With Quote
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      08-08-2006

Victor Bazarov wrote:
> (E-Mail Removed) 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 T> class Foo; // pre-declare the template class
itself
template<typename T> Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>&
rhs);
template<typename T> std:stream& operator<< (std:stream& o, const
Foo<T>& x);

template<typename T>
class Foo {
public:
Foo(const T& value = T());
friend Foo<T> operator+ <>(const Foo<T>& lhs, const Foo<T>& rhs);
friend std:stream& operator<< <>(std:stream& 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<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs)
{ return Foo<T>(lhs.value_ + rhs.value_); }

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

template class Foo<int>;

>> MAIN.CPP <<


#include "foo.hpp"

int main()
{
Foo<int> lhs(1);
Foo<int> rhs(2);
Foo<int> result = 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<int> operator+<int>(Foo<int> const&,
Foo<int> const&)'
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<int> const&)'
collect2: ld returned 1 exit status

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2006
(E-Mail Removed) wrote:
> [...]
> main.o(.text+0x63): In function `main':
>> undefined reference to `Foo<int> operator+<int>(Foo<int> const&,

> Foo<int> const&)'
> 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<int> const&)'
> 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


 
Reply With Quote
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      08-08-2006
>
> 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2006
(E-Mail Removed) 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


 
Reply With Quote
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      08-09-2006
> > 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2006
(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friend template function of template class Евгений Щербина C++ 1 03-19-2012 12:11 PM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
Problem with defining template friend function of a template class. PengYu.UT@gmail.com C++ 2 11-09-2005 08:27 PM
using friend function template in class template Yueh-Wei Hu C++ 0 05-23-2004 11:36 AM



Advertisments