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