By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Inheritance from ostream

P: n/a
Any one can specify the problem of the following code? The compiling
error is on the friend function. If the base class is not inherited
from ostream, or I just remove the friend function from the base
class, the code will compile.

-------------------------------------------------------------------------
#include <iostream>

template<class Tclass base;
template<class Tstd::ostream& operator<< (std::ostream&, base<T>&);

template<class T>
class base : public std::ostream // Note that if I remove ": public
std::ostream" here, the code is fine
{
public:
base(T a):value(a){};
friend std::ostream& operator<< <T>(std::ostream&, base<T>&); //or I
remove this line, the code is fine
protected:
T value;
};

template<class T>
std::ostream& operator<< (std::ostream& os, base<T>& b)
{
os << b.value << std::endl;
};
--------------------------------------------------------------------------------------
The compiling errors are:
: error C2143: syntax error : missing ';' before '<'
: see reference to class template instantiation 'base<T>' being
compiled
: error C2433: '<<' : 'friend' not permitted on data declarations
: error C2365: 'operator <<' : redefinition; previous definition was
'function'
: see declaration of 'operator <<'
: error C2530: '<<' : references must be initialized
: error C2238: unexpected token(s) preceding ';'
Thanks for any response!

Jun 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
friend std::ostream& operator<< <T>(std::ostream&, base<T>&);
<Tafter << is redundant remove it.

Jun 23 '07 #2

P: n/a
Cagdas Ozgenc wrote:
> friend std::ostream& operator<< <T>(std::ostream&, base<T>&);

<Tafter << is redundant remove it.
No it's not. It's necesary to tell the compiler that the friend function
is a template function.

The error seems to be that the operator<< in the friend declaration is
somehow getting confused with the operator<< in std::ostream. Rename
operator<< to some other name and the code compiles.

The posted code fails on at least two different compilers so it doesn't
seem to be a compiler bug. Unfortunately I'm not expert enough to
diagnose the problem, let alone offer a solution.

john
Jun 23 '07 #3

P: n/a
John Harrison wrote:
Cagdas Ozgenc wrote:
>> friend std::ostream& operator<< <T>(std::ostream&, base<T>&);

<Tafter << is redundant remove it.

No it's not. It's necesary to tell the compiler that the friend function
is a template function.
If you remove '<T>' g++ even tells you to put it back in with a warning
message.

john
Jun 23 '07 #4

P: n/a
On 2007-06-23, syang8 <sy****@gmail.comwrote:
Any one can specify the problem of the following code? The compiling
error is on the friend function. If the base class is not inherited
from ostream, or I just remove the friend function from the base
class, the code will compile.

-------------------------------------------------------------------------
#include <iostream>

template<class Tclass base;
template<class Tstd::ostream& operator<< (std::ostream&, base<T>&);

template<class T>
class base : public std::ostream // Note that if I remove ": public
std::ostream" here, the code is fine
{
public:
base(T a):value(a){};
friend std::ostream& operator<< <T>(std::ostream&, base<T>&); //or I
remove this line, the code is fine
protected:
T value;
};

template<class T>
std::ostream& operator<< (std::ostream& os, base<T>& b)
{
os << b.value << std::endl;
};
--------------------------------------------------------------------------------------
There are many operator<< defined in std::ostream which is a base of
this class, but you are trying to befriend a global operator. Try
this:

friend std::ostream& ::operator<< <T>(std::ostream&, base<T>&);

Also, this function needs to return something. Try this:

template<class T>
std::ostream& operator<< (std::ostream& os, base<T>& b)
{
os << b.value << std::endl;
return os;
}

I removed the [currently incorrect but possibly in the future just]
redundant trailing ';'.

I don't know why you are deriving from std::ostream in this case, but
without further evidence to the contrary I wouldn't have thought that
it would be a useful thing to do.
Jun 23 '07 #5

P: n/a
>
There are many operator<< defined in std::ostream which is a base of
this class, but you are trying to befriend a global operator. Try
this:

friend std::ostream& ::operator<< <T>(std::ostream&, base<T>&);
That is so obvious! Why didn't I think of that.

john
Jun 23 '07 #6

P: n/a

Charles Bailey <us**********@hashpling.orgwrote in message...
On 2007-06-23, syang8 <sy****@gmail.comwrote:
Any one can specify the problem of the following code? The compiling
error is on the friend function. If the base class is not inherited
from ostream, or I just remove the friend function from the base
class, the code will compile.

-------------------------------------------------------------------
#include <iostream>
template<class Tclass base;
template<class Tstd::ostream& operator<< (std::ostream&, base<T>&);

template<class T>
// Note that if I remove ": public std::ostream" here, the code is fine
class base : public std::ostream {
public:
base(T a):value(a){};
// or I remove this line, the code is fine
friend std::ostream& operator<< <T>(std::ostream&, base<T>&);
protected:
T value;
};

template<class T>
std::ostream& operator<< (std::ostream& os, base<T>& b){
os << b.value << std::endl;
};
----------------------------------------------------------------
There are many operator<< defined in std::ostream which is a base of
this class, but you are trying to befriend a global operator. Try
this:

friend std::ostream& ::operator<< <T>(std::ostream&, base<T>&);

Also, this function needs to return something. Try this:

template<class T>
std::ostream& operator<< (std::ostream& os, base<T>& b){
os << b.value << std::endl;
return os;
}

I removed the [currently incorrect but possibly in the future just]
redundant trailing ';'.

I don't know why you are deriving from std::ostream in this case, but
without further evidence to the contrary I wouldn't have thought that
it would be a useful thing to do.
Did you try to instantiate the template?

int main(){
base<intB1( 777444 );
std::cout<<"base<intB1( 777444 ); ="<< B1 <<std::endl;
return 0;
}

Did it work?

Try this:

#include <iostream>

class baseSos : public std::ostream { public:
baseSos() : value( 0 ){};
protected:
int value;
};

int main(){
baseSos B1;
return 0;
}

Did it compile for you?

--
Bob R
POVrookie
Jun 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.