注册 登录
编程论坛 C++教室

帮小女子看看这简单的C++错在哪里?

avator123 发布于 2011-10-12 21:19, 698 次点击

#include <iostream>
using namespace std;

class Point {

   int x , y ;

    public :
    void set (int a ,int b) {   x = a ,y = b ;}

    friend    Point operator+(const Point& d ) ;
   

    friend ostream& operator<<(ostream& o, const Point& d) ;


} ;

Point operator+(const Point &d )
{
   
    Point s ;
    s.set (x+d.x,y+d.y);
    return s ;
   
    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
operator+(s,t);

}

错误信息:
-------------------Configuration: training - Win32 Debug--------------------
Compiling...
training.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\myproject\training.cpp(11) : 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 时出错.

training.obj - 1 error(s), 0 warning(s)
7 回复
#2
avator1232011-10-12 21:34
回复 楼主 avator123
程序代码:
#include <iostream>
using namespace std;

class Point {

   int x , y ;

    public :

    void set (int a ,int b) {   x = a ,y = b ; }

    friend  operator+(const Point& d ) ;
  

    friend ostream& operator<<(ostream& o, const Point& d) ;


} ;


 operator+(const Point &d )

{
   

    Point s ;
    s.set (x+d.x,y+d.y);
    return s ;
   

    }


 ostream& operator<<(ostream& o, const Point& d)  {



return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{

 Point s,t ;

 s.set(2,5);

 t.set(3,1);

 operator+(s,t);
// cout<<s+t ;
}

#3
czsbc2011-10-12 21:35
程序代码:
#include <iostream>

using namespace std;

class Point;

Point operator+(const Point &e,const Point &d );
ostream& operator<<(ostream& o, const Point& d);

class Point {

    int x , y ;

    public :
    void set (int a ,int b) {   x = a ,y = b ;}

    friend Point operator+(const Point &e,const Point &d  ) ;
  
    friend ostream& operator<<(ostream& o, const Point& d) ;
} ;

Point operator+(const Point &e,const Point &d )
{
  
    Point s ;
    s.set (e.x+d.x,e.y+d.y);
    return s ;
  
    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
operator+(s,t);

}

#4
avator1232011-10-12 21:35
--------------------Configuration: training - Win32 Debug--------------------
Compiling...
training.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\myproject\training.cpp(11) : 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 时出错.

training.obj - 1 error(s), 0 warning(s)
#5
avator1232011-10-12 21:38
czsbc,谢谢了,果然是高手
#6
czsbc2011-10-12 21:40
程序代码:
#include <iostream>
using namespace std;

class Point;
ostream& operator<<(ostream& o, const Point& d);

class Point {

    int x , y ;

    public :

    void set (int a ,int b) {   x = a ,y = b ; }

    Point operator+(const Point& d ) ;
  

    friend ostream& operator<<(ostream& o, const Point& d) ;


} ;

Point Point::operator+(const Point &d )
{
  
    Point s ;
    s.set (x+d.x,y+d.y);
    return s ;
  
    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t ;
}

#7
avator1232011-10-12 21:47
回复 6楼 czsbc
#include <iostream>
using namespace std;

class Point; //why add this declaration to here? could you please explanation to me clearly ?
ostream& operator<<(ostream& o, const Point& d);//why add this declaration to here?

class Point {

    int x , y ;

    public :

    void set (int a ,int b) {   x = a ,y = b ; }

    Point operator+(const Point& d ) ;
 

    friend ostream& operator<<(ostream& o, const Point& d) ;


} ;

Point Point::operator+(const Point &d )
{
 
    Point s ;
    s.set (x+d.x,y+d.y);
    return s ;
 
    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t ;
}
#8
czsbc2011-10-12 22:15
这个问题与编译器有关,在VC6中要先声明,
在VS2010中,支持标准C++的就不用了。
1