?
Solved

"fatal error C1001: INTERNAL COMPILER ERROR" When coming template class withe operator << as friend

Posted on 2005-03-31
7
Medium Priority
?
316 Views
Last Modified: 2013-12-14
The following code won't compile on VC++ 6 at all. In VC 2003, it gives me internal compiler error. Can somebody help me out?
It seems VC compiler does not support templates and operator overridding very well.

#include <iostream>
#include <string>

template<class T>
class A
{
private:
      T m;

public:
      template<class os>
      friend os& operator << (os& s, A<T> x)
      {
            return s;
      }

};

void main()
{
      A<int> x;
      std::cout << x;
}
0
Comment
Question by:luoys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13673620
>>It seems VC compiler does not support templates and operator overridding very well.

Not really, but using correct code helps - you need to use/provide 2 template arguments, T and os:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

template<class T, class os>
class A
{
private:
    T m;

public:
    template<class os>
    friend os& operator << (os& s, A<T,os> x)
    {
         return s;
    }

};

void main()
{
    A<int,ofstream> x;
    //std::cout << x; > <-- that expression does not make sense at all.
}

0
 
LVL 3

Expert Comment

by:teratoma
ID: 13674190
Borland and GCC disagree with you jkr.  The only complaint I get is that main should return an int.  VC++ 6.0 doesn't support member templates.  If you want a better compiler you can download Borland 5.5, VC++ 8 beta, or mingw.
0
 
LVL 3

Expert Comment

by:teratoma
ID: 13674297
Oh yeah, and the use of x uninitialized.  I just tried it with VC++ 2005 and it works.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 13674420
You're right:

#include <iostream>
#include <string>

template<class T>
class A
{
private:
    T m;

public:
    template<class os>
    friend os& operator << (os& s, A<T> x)
    {
         return s;
    }

};

void main()
{
    A<int> x;
    //std::cout << x;
}

compile with VC6 - without any error...
0
 

Author Comment

by:luoys
ID: 13674868
Thanks for everyone.

jkr, you can not just comment out my last line of code. otherwise the operator << () functions will not be compiled but that is the function I want.

I understand the expression does not make sense here, but I want to use the simplest code to show the error. The function body will be implemented in the future.
0
 

Author Comment

by:luoys
ID: 13674885
template<class T, class os>
class A
{
private:
    T m;

public:
    template<class os>
    friend os& operator << (os& s, A<T,os> x)
    {
         return s;
    }

};
 is incorrect. My point is to allow serializing template class A<T> to any stream. So class os can not be part of the template class A definition.
0
 
LVL 3

Accepted Solution

by:
teratoma earned 1500 total points
ID: 13677183
Again, your original code is fine (except for the warnings).  Time for a new compiler.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question