版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://lihuan-dianxian.blogbus.com/logs/42102230.html
同学拿了个很简单的小程序过来问我,重载了个运算符,如果作为成员函数,一点问题没有;如果作为友元函数重载,就会出现下面的编译出错提示:
viewsource
print?
01 | -------------------Configuration:money-Win32Debug-------------------- |
04 | F:\c++workspaces\money\money.cpp(12):fatalerrorC1001:INTERNALCOMPILERERROR |
05 | (compilerfile
'msc1.cpp' ,line1786)
|
06 | PleasechoosetheTechnicalSupportcommandontheVisualC++ |
07 | Helpmenu,oropentheTechnicalSupporthelpfile for moreinformation |
10 | money.obj-1error(s),0warning(s) |
最怕这种叽歪的错误,其实程序的编写是没问题的,也会这样。。于是,常规思路,上网搜搜呗。
寻寻觅觅的过程就不罗嗦了,解决的办法如下:
方法一:
头文件的#include<iostream>改成#include<iostream.h>然后删除语句usingnamespacestd;方法二:【0】
不改动头文件那,在类的声明前面加上下面两条语句,作为前导声明
classmoney;moneyoperator+(constmoney&account1,constmoney&account2)
就都可以编译通过了~
究其原因,我试着说说吧。先说包含头文件的时候,带不带.h的扩展名的意思是差很多的。若用<iostream.h>的话,表示告诉编译器,使用VisualC++6.0的基础开发环境自己的输入输出流类库头文件iostream.h。而<iostream>是指使用ISOC++标准的输入输出流类库头文件iostream。并非省略扩展名,包含的本身就是两个不同的文件。
如果你觉得没理解,还有下面的这种解释方式:
当使用<iostream.h>时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用<iostream>的时候,该头文件没有定义全局命名空间,必须使用namespacestd;这样才能正确使用cout。【1】
还没明白就算了,知道这么回事先。其实我也不是特别透。
此外,关于fatalerrorC1001:INTERNALCOMPILERERROR(compilerfile'msc1.cpp',line1786)的错误,还有一说:
因为别的非友元的问题引起的这个错误,具体原因未知,如果遇到,可以用下面的解决方法44看:
一、在Project/Setting/c/c++中的Projectoption,从中删除/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
最后,附上那段程序好了
viewsource
print?
006 | int
char_to_int( char
c); |
009 | moneyoperator+( const
money&account1, const
money&account2); |
014 | friend
moneyoperator+( const
money&account1, const
money&account2); |
016 | bool
operator==( const
money&account1); |
017 | moneypercent( int
percent_figure) const ; |
018 | money( long
dollars, int
cents); |
021 | double
get_value() const ; |
022 | void
input(istream&ins); |
023 | void
output(ostream&outs) const ; |
030 | moneyluli(1,3),guo(5,8),total; |
032 | cout<< "luli'smoneyis" ; |
038 | cout<< "luli'smoney+guo'smoney=" ; |
042 | cout<< "yourmoneyareequal!\n" ; |
044 | cout<< "oneofyouarericher!\n" ; |
053 | moneyoperator+( const
money&account1, const
money&account2) |
056 | temp.all_cents=account2.all_cents+account1.all_cents; |
060 | bool
money::operator==( const
money&account1) |
062 | return (all_cents==account1.all_cents); |
067 | money::money( long
dollars, int
cents):all_cents(dollars*100+cents) |
069 | if (dollars<0||cents<0) |
071 | cout<< "illeagalnumberformoney!\n" ; |
076 | money::money( long
dollars):all_cents(dollars*100) |
080 | cout<< "illeagalnumberformoney!\n" ; |
085 | money::money():all_cents(0) |
089 | double
money::get_value() const |
094 | void
money::input(istream&ins) |
098 | char
one_char,decimal_point; |
100 | cout<< "pleaseenteronechar('-'or'$')\n" ; |
105 | cout<< "pleaseenteronechar('$')\n" ; |
108 | cout<< "pleaseenterdollars\n" ; |
110 | cout<< "enterdecimal_point\n" ; |
112 | cout<< "pleaseenteryourcents(date1,date2inchar)\n" ; |
114 | if (one_char!= '$' ||dollars<0||decimal_point!= '.' ||! isdigit (date1)||! isdigit (date2)) |
116 | cout<< "errorilleagalformformoney!\n" ; |
119 | all_cents=dollars*100+char_to_int(date1)*10+char_to_int(date2); |
124 | void
money::output(ostream&outs) const |
126 | long
positive_cents,dollars,cents; |
130 | positive_cents= labs (all_cents); |
132 | positive_cents=all_cents;
|
133 | outs.setf(ios::fixed);
|
134 | outs.setf(ios::showpoint);
|
136 | dollars=positive_cents/100;
|
137 | cents=positive_cents%100;
|
149 | return ( int (c)- int ( '0' )); |
152 | moneymoney::percent( int
percent_figure) const |
154 | return
all_cents*percent_figure/100; |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理