2011-09-22 67 views
0

我有以下代码总结2个结构元素定义的ostream运算符<<总结2结构

#include <iostream> 
#include <ostream> 
#include <fstream> 
using namespace std; 
struct USC{ 

int dollars; 
int cents; 
USC operator+(const USC o){ 
    USC temp={0,0}; 
    temp.cents=cents+o.cents; 
    temp.dollars=dollars+o.dollars; 
    if (temp.cents>=100){ 
     temp.dollars+=1; 
     temp.cents-=100; 

    } 
     return temp; 
} 


}; /* <-- make sure that semicolon is there */ 
     ostream& operator<<(ostream &output, const USCurrency &o) { 
      output<<o.dollars<<" "<<o.cents<<endl; 
     return output; 

     } 
int main(){ 

    USC a={2,50}; 
    USC b={1,75}; 
     USC c=a+b; 

     cout<<c<<endl; 




return 0; 
} 

但这里是错误的列表

1>------ Build started: Project: struct, Configuration: Debug Win32 ------ 
1> struct.cpp 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2143: syntax error : missing ';' before '&' 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2872: 'ostream' : ambiguous symbol 
1>   could be 'c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23) : USC ostream' 
1>   or  'c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(636) : std::ostream' 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2872: 'ostream' : ambiguous symbol 
1>   could be 'c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23) : USC ostream' 
1>   or  'c:\program files\microsoft visual studio 10.0\vc\include\iosfwd(636) : std::ostream' 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2065: 'output' : undeclared identifier 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2059: syntax error : 'const' 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2143: syntax error : missing ';' before '{' 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(23): error C2447: '{' : missing function header (old-style formal list?) 
1>c:\users\datuashvili\documents\visual studio 2010\projects\struct\struct\struct.cpp(34): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'USC' (or there is no acceptable conversion) 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(726): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(764): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(811): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(937): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(944): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(951): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(958): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(968): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>,USC>(std::basic_ostream<_Elem,_Traits> &&,_Ty)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ty=USC 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085): or  'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(186): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(192): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(199): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(206): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(226): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(260): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(280): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(305): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(325): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(386): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(407): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(427): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(447): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(467): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\ostream(487): or  'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char> 
1>   ] 
1>   while trying to match the argument list '(std::ostream, USC)' 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

请你们帮我什么是错?

回答

6

您需要结构定义后分号

(第22行的大括号之后)我编辑的职位,以显示它去

+0

谢谢@foo呸,我忘了,如类,结构还需要分号身体的尽头,非常感谢 –

1

编译器说,它给你:

c:\ users \ datuashvili \ documents \ visual studio 2010 \ projects \ struct \ struct \ struct.cpp(23):error C2143:syntax error:missing';'前“&”

在一般情况下,当你有编译错误,从第一个开始......(-`

相关问题