473,835 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
`GeneralPolygon s::readObjFile( std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char)':
core/generalpolygons .cpp:222: undefined reference to
`std::basic_ost ream<char, std::char_trait s<char& operator<<
<float>(std::ba sic_ostream<cha r, std::char_trait s<char&,
Point<floatcons t&)'

Here is the code:

In point.hpp

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

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

in point.cpp

template<typena me 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 1980
ma*****@yahoo.c om 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.c om 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.c om 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<typena me Tclass Foo; // pre-declare the template class
itself
template<typena me TFoo<Toperator+ (const Foo<T>& lhs, const Foo<T>&
rhs);
template<typena me Tstd::ostream& operator<< (std::ostream& o, const
Foo<T>& x);

template<typena me 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<typena me T>
Foo<T>::Foo(con st T& value)
: value_(value)
{ }

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

template<typena me 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+0x 63): In function `main':
: undefined reference to `Foo<intoperato r+<int>(Foo<int const&,
Foo<intconst&)'
main.o(.text+0x 7f): In function `main':
: undefined reference to `std::basic_ost ream<char,
std::char_trait s<char& operator<< <int>(std::basi c_ostream<char,
std::char_trait s<char&, Foo<intconst&)'
collect2: ld returned 1 exit status

Aug 8 '06 #4
ma*****@yahoo.c om wrote:
[...]
main.o(.text+0x 63): In function `main':
>undefined reference to `Foo<intoperato r+<int>(Foo<int const&,
Foo<intconst&)'
main.o(.text+0x 7f): In function `main':
>undefined reference to `std::basic_ost ream<char,
std::char_trait s<char& operator<< <int>(std::basi c_ostream<char,
std::char_trait s<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.c om 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.c om 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
12541
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 <class T> class test{ test(void); ~test(void); friend bool operator== <> (const test<T> &p1, const test<T> &p2); }
5
2725
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: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
2
3080
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
2477
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. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include <string> #include <vector>
2
1717
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 template class? Thanks, Peng #include <iostream> template <typename T>
2
2055
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
3763
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
2
1981
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 the operator << is also designed as a template function. The program has following linking errors: "error LNK2019: unresolved external symbol "class
4
1899
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); };
0
9802
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10517
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10559
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7765
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5802
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3086
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.