使用友元,编译出错fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) 的解决

 

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://lihuan-dianxian.blogbus.com/logs/42102230.html

       同学拿了个很简单的小程序过来问我,重载了个运算符,如果作为成员函数,一点问题没有;如果作为友元函数重载,就会出现下面的编译出错提示:

01-------------------Configuration: money - Win32 Debug--------------------
02Compiling...
03money.cpp
04F:\c++workspaces\money\money.cpp(12) : fatal error C1001: INTERNAL COMPILER ERROR
05        (compiler file 'msc1.cpp', line 1786) 
06         Please choose the Technical Support command on the Visual C++ 
07         Help menu, or open the Technical Support help file for more information
08Error executing cl.exe.
09  
10money.obj - 1 error(s), 0 warning(s)

最怕这种叽歪的错误,其实程序的编写是没问题的,也会这样。。于是,常规思路,上网搜搜呗。

寻寻觅觅的过程就不罗嗦了,解决的办法如下:

方法一:

头文件的 #include<iostream> 改成 #include<iostream.h>

然后删除语句 using namespace std;

方法二:【0】
不改动头文件那,在类的声明前面加上下面两条语句,作为前导声明
class money;
money operator + (const money& account1,const money& account2)

就都可以编译通过了~

究其原因,我试着说说吧。先说包含头文件的时候,带不带.h的扩展名的意思是差很多的。若用<iostream.h>的话,表示告诉编译器,使用VisualC++6.0的基础开发环境自己的输入输出流类库头文件iostream.h。而<iostream>是指使用ISO C++标准的输入输出流类库头文件iostream。并非省略扩展名,包含的本身就是两个不同的文件。

如果你觉得没理解,还有下面的这种解释方式:

当使用<iostream.h>时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用<iostream>的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。【1】

还没明白就算了,知道这么回事先。其实我也不是特别透。

此外,关于fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) 的错误,还有一说:

因为别的非友元的问题引起的这个错误,具体原因未知,如果遇到,可以用下面的解决方法44看:

一、在Project/Setting/ c/c++中的Project option,从中删除 /Zg 或 /Gz 或者一些个别的比如/oa之类的,然后重写下出错的那行,再编译试试。一次删除一个,如果仍出错那么就说明不是原因,可添加回去再删除别的试试。【2】

二、某大人介绍了自己用到的情况:“我在调用一个自己的函数时出现这个错误,我通过多方努力找到了原因:我的这个函数有一个结构体参数,并且该结构体比较庞大,之前我是将整个结构体都传入而导致编译出错,最后我改成传入该结构体的地址(传引用),这下编译就对了”。【3】

我就简单的贴在这里了。

为了保持严谨的科学态度,下面标明参考文献:

【0】http://topic.csdn.net/t/20041203/13/3612519.html

【1】http://www.kuqin.com/language/20080107/3532.html

【2】http://book.77169.org/ask36/how167856.htmhttp://www.codeguru.com/forum/showthread.php?t=314848

【3】http://www.china-askpro.com/msg41/qa74.shtml

最后,附上那段程序好了

 

001#include<iostream>
002#include<cstdlib>
003#include<cctype>
004using namespace std;
005  
006int char_to_int(char c);
007  
008class money;
009money operator + (const money& account1,const money& account2);
010  
011class money
012{
013public:
014 friend money operator + (const money& account1,const money& account2);
015   
016    bool operator == ( const money& account1);
017 money percent(int percent_figure) const;
018 money(long dollars,int cents);
019 money(long dollars);
020 money();
021 double get_value() const;
022 void input(istream& ins);
023 void output(ostream& outs) const;
024private:
025 long all_cents;
026};
027  
028int main()
029{
030 money luli(1,3),guo(5,8),total;
031 total=luli+guo;
032 cout<<"luli's money is";
033 luli.output(cout);
034 cout<<endl;
035 cout<<"guo's money is";
036 guo.output(cout);
037 cout<<endl;
038 cout<<"luli's money+guo's money=";
039 total.output(cout);
040 cout<<endl;
041 if(luli==guo)
042  cout<<"your money are equal!\n";
043 else
044  cout<<"one of you are richer!\n";
045 money lugang;
046 lugang.input(cin);
047 lugang.output(cout);
048 lugang.percent(10);
049 lugang.output(cout);
050 return 0;
051}
052  
053money operator + (const money& account1,const money& account2)
054{
055 money temp;
056 temp.all_cents=account2.all_cents+account1.all_cents;
057 return temp;
058}
059  
060bool money:: operator == (const money& account1)
061{
062 return(all_cents==account1.all_cents);
063}
064  
065   
066  
067money::money(long dollars,int cents):all_cents(dollars*100+cents)
068{
069 if(dollars<0||cents<0)
070 
071  cout<<"illeagal number for money!\n";
072  exit(1);
073 }
074}
075  
076money::money(long dollars):all_cents(dollars*100)
077{
078 if(dollars<0)
079 
080  cout<<"illeagal number for money!\n";
081  exit(1);
082 }
083}
084  
085money::money():all_cents(0)
086{
087}
088  
089double money:: get_value() const
090{
091 return(all_cents);
092}
093  
094void money::input(istream& ins)
095{
096 long dollars;
097 char date1,date2;
098 char one_char,decimal_point;
099 bool negative;
100 cout<<"please enter one char('-'or'$')\n";
101 ins>>one_char;
102 if(one_char=='-')
103 {
104  negative=true;
105  cout<<"please enter one char('$')\n";
106  ins>>one_char;
107 }
108 cout<<"please enter dollars\n";
109 ins>>dollars;
110 cout<<"enter decimal_point\n";
111 ins>>decimal_point;
112 cout<<"please enter your cents(date1,date2 in char)\n";
113 ins>>date1>>date2;
114 if(one_char!='$'||dollars<0||decimal_point!='.'||!isdigit(date1)||!isdigit(date2))
115 {
116  cout<<"error illeagal form for money!\n";
117  exit(1);
118 }
119 all_cents=dollars*100+char_to_int(date1)*10+char_to_int(date2);
120 if(negative)
121  all_cents=-all_cents;
122}
123  
124void money::output(ostream& outs) const
125{
126 long positive_cents,dollars,cents;
127 if(all_cents<0)
128 {
129  outs<<"-";
130  positive_cents=labs(all_cents);
131 }
132 positive_cents=all_cents;
133 outs.setf(ios::fixed);
134 outs.setf(ios::showpoint);
135 outs.precision(2);
136 dollars=positive_cents/100;
137 cents=positive_cents%100;
138 outs<<"$"<<dollars;
139 outs<<".";
140 if(cents<10)
141 {
142  outs<<"0";
143 }
144 outs<<cents<<endl;
145}
146  
147int char_to_int(char c)
148{
149 return(int(c)-int('0'));
150}
151  
152money money:: percent(int percent_figure) const
153{
154 return all_cents*percent_figure/100;
155}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值