• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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

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
luoys
Asked:
luoys
  • 3
  • 2
  • 2
1 Solution
 
jkrCommented:
>>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
 
teratomaCommented:
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
 
teratomaCommented:
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.

 
jkrCommented:
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
 
luoysAuthor Commented:
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
 
luoysAuthor Commented:
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
 
teratomaCommented:
Again, your original code is fine (except for the warnings).  Time for a new compiler.
0

Featured Post

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.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now