VC++6.0在重载操作符时时定义为友元,报错fatal error C1001: INTERNAL COMPILER ERROR解决方案

今天帮别人回答问题的时候,用VC6.0测试了一下他的代码,因为这个编译器启动较快,这点比较喜欢,发现了以前一直没有注意的问题。

先贴出错误现象:

--------------------Configuration: 2 - Win32 Debug--------------------
Compiling...
2.cpp
C:\Users\Administrator\Desktop\2.cpp(14) : fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file 'msc1.cpp', line 1786) 
         Please choose the Technical Support command on the Visual C++ 
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

2.obj - 1 error(s), 0 warning(s)

当时看到这样的错误,心想这是神马错误。然后再仔细看了一遍代码,发现确实没有问题啊?这个时候首先想到的就是换个编译器试试,换了个高级的编译器(VC9.0),跑了一下果然没有问题。然后就到谷歌上贴上错误提示搜了一下,果然还是有很多人遇到了这个问题,提到了两种解决方法。

1.在VC6.0的代码中,用#include <iostream.h>

2.增加前导声明

代码如下:

//第一种方式
/*
#include<iostream> 
using namespace std; 

class A;					//前导声明
A operator+(A a, A b);	//前导声明
*/

//第二种方式
#include <iostream.h>

class A {
private:
	int x; 
public: 	
	A() : x(0) {} 	
	A(int a) : x(a) {} 	
	A(A &a) { x = a.x; } 	
	friend A operator+(const A &a1, const A &a2); 
	void disp() { 	
		cout << x << endl; 
	} 
}; 

A operator+(const A &a1, const A &a2) {
	A temp(a1.x + a2.x); 
	return temp; 	
} 

int main() { 	
	A a1(1), a2(3), temp; 	
	temp = a1 + a2; 	
	temp.disp(); 

	return 0;
}
PS:经过这个事情更加觉得还是少用VC6.0的好,个人想法,仅供参考。
参考资料: http://lihuan-dianxian.blogbus.com/logs/42102230.html

猜你喜欢

转载自blog.csdn.net/u011248694/article/details/29199893