Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange compiler error w.r.t 'friend ostream& operator<< <>(ostream&,...)'

Reply
Thread Tools

Strange compiler error w.r.t 'friend ostream& operator<< <>(ostream&,...)'

 
 
abhay.burli@gmail.com
Guest
Posts: n/a
 
      02-25-2009
I have the following piece of code ...

NOTE: I have intentionally qualified 'ostream' and 'cout' with 'std::'
for the purposes of the example

#include<iostream>
using namespace std;

template<class T>
class SomeClass{
public:
SomeClass(const T& a) : member(a)
{
}
friend std:stream& operator<< <>(std:stream &,SomeClass <T>&);
private:
T member;
};

template <typename T>
std:stream& operator<< (std:stream &os, SomeClass<T>&some)
{
os<<"( " << some.member <<") ";
return os;
}

int main(int argc, char* argv[])
{
SomeClass<int> sc(5);
std::cout << sc;
return 0;
}

This compiles fine.
Now just comment out the line 'using namespace std;' and comeau spits
out the following ...
<--------------------------------------------------------------------------------------
>

Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for
ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 11: error: operator<< is not a template,
Should it be XX:perator<<?, where XX is some namespace?
Did you #include the right header?
friend std:stream& operator<< <>(std:stream &,SomeClass <T>&);
^

"ComeauTest.c", line 19: error: member "SomeClass<T>::member [with
T=int]" (declared
at line 13) is inaccessible
os<<"( " <<some.member <<") ";
^
detected during instantiation of "std:stream
&operator<<(std:stream &, SomeClass<T> &) [with
T=int]"
at line 26

2 errors detected in the compilation of "ComeauTest.c".
<--------------------------------------------------------------------------------------
>


and following from MSVC 2005 (Team Edition) is worse ...
<--------------------------------------------------------------------------------------
>

1>Compiling...
1>Hello World.cpp
1>.\Hello World.cpp(2112) : error C2143: syntax error : missing ';'
before '<'
1> .\Hello World.cpp(2113) : see reference to class template
instantiation 'S<T>' being compiled
1>.\Hello World.cpp(2112) : error C2433: '<<' : 'friend' not permitted
on data declarations
1>.\Hello World.cpp(2112) : error C2530: '<<' : references must be
initialized
1>.\Hello World.cpp(2112) : error C2238: unexpected token(s) preceding
';'
1>.\Hello World.cpp(2119) : error C2365: '<<' : redefinition; previous
definition was 'data variable'
1> .\Hello World.cpp(2112) : see declaration of '<<'
1>.\Hello World.cpp(2119) : error C2904: '<<' : name already used for
a template in the current scope
1> .\Hello World.cpp(2112) : see declaration of '<<'
1>.\Hello World.cpp(2112) : error C2143: syntax error : missing ';'
before '<'
1> .\Hello World.cpp(2124) : see reference to class template
instantiation 'S<T>' being compiled
1> with [... a long template instantiation error ... ]
<--------------------------------------------------------------------------------------
>


Why??
 
Reply With Quote
 
 
 
 
Annie Testes
Guest
Posts: n/a
 
      02-25-2009

abhay.bu...@gmail.com wrote:
> I have the following piece of code ...
>
> NOTE: I have intentionally qualified 'ostream' and 'cout' with 'std::'
> for the purposes of the example
>
> #include<iostream>
> using namespace std;
>
> template<class T>
> class SomeClass{
> public:
> SomeClass(const T& a) : member(a)
> {
> }
> friend std:stream& operator<< <>(std:stream &,SomeClass <T>&);
> private:
> T member;
> };
>
> template <typename T>
> std:stream& operator<< (std:stream &os, SomeClass<T>&some)
> {
> os<<"( " << some.member <<") ";
> return os;
> }
>
> int main(int argc, char* argv[])
> {
> SomeClass<int> sc(5);
> std::cout << sc;
> return 0;
> }
>
> This compiles fine.


The above doesn't compile with gcc (4.3.3)
(either with and without 'using namespace std'):

yyy.cc: In instantiation of ‘SomeClass<int>’:
yyy.cc:27: instantiated from here
yyy.cc:11: error: template-id ‘operator<< <>’ for ‘std:stream&
operator<<(std:stream&, SomeClass<int>&)’ does not match any
template declaration
yyy.cc: In function ‘std:stream& operator<<(std:stream&,
SomeClass<T>&) [with T = int]’:
yyy.cc:28: instantiated from here
yyy.cc:13: error: ‘int SomeClass<int>::member’ is private
yyy.cc:20: error: within this context

If I forward-declare SomeClass and operator<<, it compiles fine
(both with and without 'using namespace std'):

#include<iostream>
//using namespace std;

template <class T>
class SomeClass;

template <class T>
std:stream& operator<<(std:stream &,SomeClass <T>&);

template<class T>
class SomeClass{
public:
SomeClass(const T& a) : member(a)
{
}
friend std:stream& operator<< <>(std:stream &,SomeClass
<T>&);
private:
T member;

};

template <typename T>
std:stream& operator<< (std:stream &os, SomeClass<T>&some)
{
os<<"( " << some.member <<") ";
return os;

}

int main(int argc, char* argv[])
{
SomeClass<int> sc(5);
std::cout << sc;
return 0;

}

[skip Comeau and MSVC errors]

>
> Why??


I have no idea why the using directive would change anything.
 
Reply With Quote
 
 
 
 
abhay.burli@gmail.com
Guest
Posts: n/a
 
      02-26-2009
On Feb 25, 7:29*pm, Annie Testes <annie.tes...@googlemail.com> wrote:
> If I forward-declare SomeClass and operator<<, it compiles fine
> (both with and without 'using namespace std'):
>
> #include<iostream>
> //using namespace std;
>
> template <class T>
> class SomeClass;
>
> template <class T>
> std:stream& operator<<(std:stream &,SomeClass <T>&);


Thanks. This serves my purpose for now. I did not want the *evil*
'using namespace std;' in my code.

> > Why??

>
> I have no idea why the using directive would change anything.- Hide quoted text -
>


Yes, it is really strange or maybe I am relatively new to the world of
C++ templates!

Regards,
Abhay
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Trouble implementing an interface, strange compiler error Robert Maas, see http://tinyurl.com/uh3t Java 4 05-15-2005 09:05 PM
Strange compiler behavior after CS0006 error Kris Vanherck ASP .Net 2 04-21-2004 02:34 PM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments