The_Icer 2016-07-15 05:57 采纳率: 62.5%
浏览 1014
已采纳

关于C++文件结构的问题

以下三个文件报错:[Error] ld returned 1 exit status
类的定义(未命名3.h)
class Point
{
public:
Point(int x=0, int y=0):x(x),y(y){}
Point(const Point &p);
~Point(){ count--; }
int GetX() const{return x;}
int GetY() const{return y;}
static void GetC();

private:
int x,y;
static int count;
};
类的实现
#include "未命名3.h"
#include
using namespace std;
int Point::count=0;
Point::Point(const Point &p):x(p.x),y(p.y)

{
count++;
}
void Point::GetC(){
cout<<" Object count="< }
主函数
#include "未命名3.h"
#include
using namespace std;
int main()
{
Point A(4,5);
cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
A.GetC();
Point B(A);
cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
B.GetC();
return 0;
}
求教为什么无法编译

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-07-15 09:47
    关注
     文件包含就是把被包含文件“机械地”插入到包含指令对应的位置。相当于你打开被包含文件ctrl+a ctrl+c,打开包含文件,删除include那行,并且再按下ctrl+v的效果。
    你的代码,由于两次包含头文件,导致定义重复了。为了避免定义重复,你可以打开未命名3.h
    
    #ifndef "未命名3_H"
    #define "未命名3_H"
    
    class Point
    {
    public:
        Point(int x=0, int y=0):x(x),y(y){}
        Point(const Point &p);
        ~Point(){  count--; }
        int GetX() const{return x;}
        int GetY() const{return y;}
        static void GetC();
    
    private:
        int x,y;
        static int count;
    };
    
    #endif
    
    这样不管你include几次,都只插入一次,后面的被忽略。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作