Define friend operator << for class template.



 DEVELOP > c-Plus-Plus > Define friend operator << for class template.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Joe Hesse"
Date: 06 Dec 2007 11:05:07 AM
Object: Define friend operator << for class template.
Hi,
I have a template class and I want to define an operator << as a friend
function. For each instantiation of the class I want a corresponding
instantiation of operator <<.
The following example fails to compile with g++ version 4.1.2.
I would appreciate it if you could help me fix it or point me to a suitable
reference.
Thank you,
Joe Hesse
********************************************
#include <iostream>
// forward declaration
template <typename T>
class Foo;
// forward declaration
template <typename T>
std::ostream & operator << (std::ostream &, const Foo<T> &);
template <typename T>
class Foo {
private:
T value;
public:
Foo(const T & v) : value(v) {}
friend std::ostream & operator << <> (std::ostream &, const Foo<T> &);
};
// implement operator <<
template <typename T>
std::ostream & operator << (std::ostream &o, const Foo<T> &f) {
return o << f.value ;
}
int main() {
Foo<int> fi;
std::cout << fi;
return 0;
}
/* Here are the compiler error messages
Test.cpp: In function int main():
Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
*/
********************************************
.

User: "terminator"

Title: Re: Define friend operator << for class template. 06 Dec 2007 11:26:22 AM
On Dec 6, 8:05 pm, "Joe Hesse" <joe_he...@actcx.com> wrote:

Hi,

I have a template class and I want to define an operator << as a friend
function. For each instantiation of the class I want a corresponding
instantiation of operator <<.
The following example fails to compile with g++ version 4.1.2.
I would appreciate it if you could help me fix it or point me to a suitable
reference.

Thank you,
Joe Hesse

********************************************
#include <iostream>

// forward declaration
template <typename T>
class Foo;

// forward declaration
template <typename T>
std::ostream & operator << (std::ostream &, const Foo<T> &);

template <typename T>
class Foo {
private:
T value;
public:
Foo(const T & v) : value(v) {}
friend std::ostream & operator << <> (std::ostream &, const Foo<T> &);

};

// implement operator <<
template <typename T>
std::ostream & operator << (std::ostream &o, const Foo<T> &f) {
return o << f.value ;

}

int main() {
Foo<int> fi;
std::cout << fi;

return 0;

}

/* Here are the compiler error messages
Test.cpp: In function int main():
Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
*/

********************************************

Instein says "take it simple, as simple as posible but not simpler".
Why did you not use the simplest imaginable syntax?
I would write:
friend std::ostream & operator << (std::ostream &, const Foo &);
but this is not what the compiler complaigns about;Please learn to
read:

Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)

Since you have defined a constructor ,C++ will no more automatically
generate a default constructor. this is the errorneous line:
Foo<int> fi;
In order to resolve this add the following inside the braces for
declaration of your template class:
Foo(){};
regards,
FM.
.

User: "want.to.be.professer"

Title: Re: Define friend operator << for class template. 07 Dec 2007 03:01:24 AM
Once you not provide a constructor ,There will be a default
constructor;
When you provide, There will be your provided constructor only;
.

User: "Joe Hesse"

Title: Re: Define friend operator << for class template. 06 Dec 2007 11:25:38 AM
Please forgive me, I have an obvious error. No response is needed from the
newsgroup.
"Joe Hesse" <joe_hesse@actcx.com> wrote in message
news:13lgau67k1b9c6c@corp.supernews.com...

Hi,

I have a template class and I want to define an operator << as a friend
function. For each instantiation of the class I want a corresponding
instantiation of operator <<.
The following example fails to compile with g++ version 4.1.2.
I would appreciate it if you could help me fix it or point me to a
suitable reference.

Thank you,
Joe Hesse

********************************************
#include <iostream>

// forward declaration
template <typename T>
class Foo;

// forward declaration
template <typename T>
std::ostream & operator << (std::ostream &, const Foo<T> &);

template <typename T>
class Foo {
private:
T value;
public:
Foo(const T & v) : value(v) {}
friend std::ostream & operator << <> (std::ostream &, const Foo<T> &);
};

// implement operator <<
template <typename T>
std::ostream & operator << (std::ostream &o, const Foo<T> &f) {
return o << f.value ;
}

int main() {
Foo<int> fi;
std::cout << fi;

return 0;
}

/* Here are the compiler error messages
Test.cpp: In function int main():
Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
Test.cpp:16: note: candidates are: Foo<T>::Foo(const T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
*/

********************************************

.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER