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

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

-------------------Configuration: money - Win32 Debug--------------------
Compiling...
money.cpp
F:\c++workspaces\money\money.cpp(12) : 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.money.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.htm,http://www.codeguru.com/forum/showthread.php?t=314848

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

最后,附上那段程序好了

#include<iostream>
#include<cstdlib>
#include<cctype>
using namespace std;int char_to_int(char c);class money;
money operator + (const money& account1,const money& account2);class money
{
public:friend money operator + (const money& account1,const money& account2);bool operator == ( const money& account1);money percent(int percent_figure) const;money(long dollars,int cents);money(long dollars);money();double get_value() const;void input(istream& ins);void output(ostream& outs) const;
private:long all_cents;
};int main()
{money luli(1,3),guo(5,8),total;total=luli+guo;cout<<"luli's money is";luli.output(cout);cout<<endl;cout<<"guo's money is";guo.output(cout);cout<<endl;cout<<"luli's money+guo's money=";total.output(cout);cout<<endl;if(luli==guo)cout<<"your money are equal!\n";elsecout<<"one of you are richer!\n";money lugang;lugang.input(cin);lugang.output(cout);lugang.percent(10);lugang.output(cout);return 0;
}money operator + (const money& account1,const money& account2)
{money temp;temp.all_cents=account2.all_cents+account1.all_cents;return temp;
}bool money:: operator == (const money& account1)
{return(all_cents==account1.all_cents);
}money::money(long dollars,int cents):all_cents(dollars*100+cents)
{if(dollars<0||cents<0){ cout<<"illeagal number for money!\n";exit(1);}
}money::money(long dollars):all_cents(dollars*100)
{if(dollars<0){ cout<<"illeagal number for money!\n";exit(1);}
}money::money():all_cents(0)
{
}double money:: get_value() const
{return(all_cents);
}void money::input(istream& ins)
{long dollars;char date1,date2;char one_char,decimal_point;bool negative;cout<<"please enter one char('-'or'$')\n";ins>>one_char;if(one_char=='-'){negative=true;cout<<"please enter one char('$')\n";ins>>one_char;}cout<<"please enter dollars\n";ins>>dollars;cout<<"enter decimal_point\n";ins>>decimal_point;cout<<"please enter your cents(date1,date2 in char)\n";ins>>date1>>date2;if(one_char!='$'||dollars<0||decimal_point!='.'||!isdigit(date1)||!isdigit(date2)){cout<<"error illeagal form for money!\n";exit(1);}all_cents=dollars*100+char_to_int(date1)*10+char_to_int(date2);if(negative)all_cents=-all_cents;
}void money::output(ostream& outs) const
{long positive_cents,dollars,cents;if(all_cents<0){outs<<"-";positive_cents=labs(all_cents);}positive_cents=all_cents;outs.setf(ios::fixed);outs.setf(ios::showpoint);outs.precision(2);dollars=positive_cents/100;cents=positive_cents%100;outs<<"$"<<dollars;outs<<".";if(cents<10){outs<<"0";}outs<<cents<<endl;
}int char_to_int(char c)
{return(int(c)-int('0'));
}money money:: percent(int percent_figure) const
{return all_cents*percent_figure/100;
}

转载于:https://www.cnblogs.com/xinjun/archive/2010/07/16/1778637.html

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

  1. fatal error C1001: INTERNAL COMPILER ERROR(compiler file 'msc1.cpp', line 1786)解决方法

    有时会碰到奇怪的编译错误 fatal error C1001: INTERNAL COMPILER ERROR(compiler file 'msc1.cpp', line 1786) 致命错误C10 ...

  2. VC6.0的一个编译错误:fatal error C1001: INTERNAL COMPILER ERROR。

    最近在编译一个VC6.0工程(开发环境:win2000+VS6.0+Sp6),在加入预编译头后出现了下面的编译错误提示: c:/program files/microsoft visual studi ...

  3. fatal error C1001: INTERNAL COMPILER ERROR

    http://www.ieasy.org/yuzuo/archives/2005_07.html (余佐的blog) VC6.0的一个编译错误的解决 在window98下使用vc6.0时,如果预编译头 ...

  4. thymeleaf There was an unexpected error (type=Internal Server Error, status=500).

    thymeleaf There was an unexpected error (type=Internal Server Error, status=500). 使用thymeleaf依赖,无法访问 ...

  5. jupyter lab插件无法打开,且报错Error: 500 (Internal Server Error)

    报错信息 WARNING Error communicating with server extension. Consult the documentation for how to ensure ...

  6. 在使用pydelicious时出现HTTP Error 500: Internal Server Error的错误的解决方法:

    在使用pydelicious时出现HTTP Error 500: Internal Server Error的错误的解决方法: 参考文章: (1)在使用pydelicious时出现HTTP Error ...

  7. nms_rotated编译出错fatal error: THC/THC.h: No such file or directory

    问题描述: 使用 python setup.py develop #or "pip install -v -e ." 编译nms_rotated时出错: fatal error: ...

  8. 【Error未解决】Error:500 - Internal Server Error

    错误描述 https://stackoverflow.com/questions/55804038/transaction-has-500-error-when-i-test-in-angular-a ...

  9. 【无标题】There was an unexpected error (type=Internal Server Error, status=500).

    漏掉==$==出错 有此可知错误为漏掉 $ Caused by: org.attoparser.ParseException: Could not parse as expression: " ...

最新文章

  1. ERP选型 SAP PK Oracle
  2. 计算机网络各层代表设备
  3. 安装windows2003+SQL Server2005集群
  4. js技巧之与或运算符 || 妙用
  5. Android开发之星期天数的实现与日期转星期几
  6. 恶心的Oracle的if else if...
  7. 初学者python笔记(re模块、正则表达式完全解析)
  8. func_ext.php,fsockopen和pfsockopen函数替换
  9. python-excel读取代码1
  10. 如何跨越线程调用窗体控件?(3)
  11. paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置..
  12. 华为杯数学建模2020什么时候出结果_第17届华为杯数学建模竞赛来啦
  13. Vue使用iconfont图标
  14. 照片怎么加水印,照片加水印操作步骤
  15. Amazon Alexa系列介绍(3)--Alexa Voice Service API介绍
  16. 2022腾讯云双十一服务器价格出炉来看看便宜不
  17. Web前端实训两天记录
  18. 运行时动态引入JS文件
  19. iPhone开发部分总结
  20. java 修饰符 详解,详解Java修饰符

热门文章

  1. Python--网络编程-----传输层tcp/udp协议
  2. python 线程, GIL 和 ctypes
  3. ping通网关 ping不通dns
  4. windows 2003 网络负载平衡设置实战
  5. 安装Office2007找不到OfficeMUI.msi解决方案
  6. Cassandra数据读取机制
  7. Delphi面向对象学习随笔一:类与对象的关系
  8. 【Vue】组件的创建以及 data methods 属性的使用
  9. Python的数据类型与结构
  10. 7-64 计算平均成绩 (15 分)