中国笺纸笺谱:c++编程问题,请指教

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 07:36:09
#include <iostream>
using namespace std;
class sample
{
private:int x;
public:sample(){}
sample(int a)
{
x=a;
}
void disp()
{
cout<<"x="<<x<<endl;
}
friend sample operator+(sample &s1,sample &s2);
};
sample operator+(sample &s1,sample &s2)
{
return sample(s1.x+s2.x);
}
void main()
{
sample ob1(10);
sample ob2(20);
sample ob3;
ob3=ob1+ob2;
ob3.disp();
system("pause");
}

81.cpp(16) : 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
执行 cl.exe 时出错.

怎么回事啊??
用友元函数重载+

第一个错误的原因:
using namespace std是新式写法 #include是旧式写法 新写法的出现是为了避免不同的编译器厂家选择不同的库文件扩展名,另外,各种不同的操作系统对扩展名存在不同的限制,特别是文件名的长度,这导致了源代码的可移植性问题,但是:#include <iostream.h> 和:#include <iostream> using namespace std; 还是不完全相同的,个人感觉windows平台下的程序还是沿用旧的写法好些
加<process.h>是因为你用了system()函数
第二个错误的原因:
最简单的办法是重装VC,
实际上这个问题很多情况下是由于路径设置的问题引起的,“CL.exe”是VC使用真正的编译器(编译程序),其路径在“VC根目录\VC98\Bin”下面,你可以到相应的路径下找到这个应用程序。
因此问题可以按照以下方法解决:点击VC“TOOLS(工具)”—>“Option(选择)”—>“Directories(目录)”重新设置“Excutable Fils、Include Files、Library Files、Source Files”的路径。很多情况可能就一个盘符的不同(例如你的VC装在C,但是这些路径全部在D),改过来就OK了。

我不知道怎么回事,但我知道这种问题怎么改正:
把程序头部改成这样:#include <iostream.h>
再加一句:#include <process.h>
之后去掉
using namespace std;
运行结果:
x=30
请按任意键继续. . .

using namespace std; 是按std的命名空间,而你在代码里又加入了操作符重载,这和标准命名空间就相冲突了,会报这样的错误了。
改法:就按lewvan850307这位高手,就可以了

不懂呢