Inheritance from ostream

S

syang8

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 T> class base;
template<class T> std::eek:stream& operator<< (std::eek:stream&, base<T>&);

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

template<class T>
std::eek:stream& operator<< (std::eek:stream& 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!
 
J

John Harrison

Cagdas said:
<T> after << 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::eek:stream. 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
 
J

John Harrison

John said:
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
 
C

Charles Bailey

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 T> class base;
template<class T> std::eek:stream& operator<< (std::eek:stream&, base<T>&);

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

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

There are many operator<< defined in std::eek:stream which is a base of
this class, but you are trying to befriend a global operator. Try
this:

friend std::eek:stream& ::eek:perator<< <T>(std::eek:stream&, base<T>&);

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

template<class T>
std::eek:stream& operator<< (std::eek:stream& 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::eek:stream in this case, but
without further evidence to the contrary I wouldn't have thought that
it would be a useful thing to do.
 
J

John Harrison

There are many operator<< defined in std::eek:stream which is a base of
this class, but you are trying to befriend a global operator. Try
this:

friend std::eek:stream& ::eek:perator<< <T>(std::eek:stream&, base<T>&);

That is so obvious! Why didn't I think of that.

john
 
B

BobR

Charles Bailey said:
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 T> class base;
template<class T> std::eek:stream& operator<< (std::eek:stream&, base<T>&);

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

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

friend std::eek:stream& ::eek:perator<< <T>(std::eek:stream&, base<T>&);

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

template<class T>
std::eek:stream& operator<< (std::eek:stream& 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::eek:stream 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<int> B1( 777444 );
std::cout<<"base<int> B1( 777444 ); ="<< B1 <<std::endl;
return 0;
}

Did it work?

Try this:

#include <iostream>

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

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

Did it compile for you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top