| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 698 人关注过本帖
标题:帮小女子看看这简单的C++错在哪里?
只看楼主 加入收藏
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
结帖率:80%
收藏
已结贴  问题点数:0 回复次数:7 
帮小女子看看这简单的C++错在哪里?

#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)
搜索更多相关主题的帖子: include public 小女子 return friend 
2011-10-12 21:19
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
回复 楼主 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 ;
}

2011-10-12 21:34
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:9 
程序代码:
#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);

}

2011-10-12 21:35
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
--------------------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)
2011-10-12 21:35
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
czsbc,谢谢了,果然是高手
2011-10-12 21:38
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:0 
程序代码:
#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 ;
}

2011-10-12 21:40
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
回复 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 ;
}
2011-10-12 21:47
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:0 
这个问题与编译器有关,在VC6中要先声明,
在VS2010中,支持标准C++的就不用了。
2011-10-12 22:15
快速回复:帮小女子看看这简单的C++错在哪里?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016547 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved