hnuqlm 2017-02-07 15:04 采纳率: 0%
浏览 1353

c++多文件程序怎么运行不了

类的定义(.h):
class Point
{
public:
Point(int x=0,int y=0):x(x),y(y){}
Point(const Point &p);
~Point(){count--;}
int getX(){return x;}
int getY(){return y;}
static void showCount();
private:
int x,y;
static int count;
};
类的实现(.cpp):
#include
#include"Point.h"
using namespace std;
int Point::count=0;
Point::Point(const Point &p):x(p.x),y(p.y)
{
count++;

}
void Point::showCount()
{
cout<<"Object count="< }
主函数(.cpp):
#include
#include"Point.h"
using namespace std;
int main()
{
Point a(4,5);
cout<<"Point A:"<<a.getX()<<","<<a.getY();
Point::showCount();
Point b(a);
cout<<"Point B:"<<b.getX()<<","<<b.getY();
Point::showCount();
return 0;
}
主函数的文件运行不了,这是为什么

  • 写回答

3条回答 默认 最新

  • HannanKan 2017-02-07 15:29
    关注

    图片说明
    你的程序写法有问题,类定义应该放在头文件中,
    头文件定义如下

     #ifndef POINT_H_
    #define POINT_H_
    #include<iostream>
    class Point
    {
    public:
        Point(int x = 0, int y = 0) :x(x), y(y){}
        Point(const Point &p);
        ~Point(){ count--; }
        int getX(){ return x; }
        int getY(){ return y; }
        static void showCount();
    private:
        int x, y;
        static int count;
    };
    //int Point::count = 0;
    int Point::count = 0;
    Point::Point(const Point &p) :x(p.x), y(p.y)
    {
        count++;
    
    }
    void Point::showCount()
    {
        std::cout << "Object count=" << std::endl;
    }
    #endif
    

    主程序如下:

    #include<iostream>
    #include"Point.h"
    using namespace std;
    int main()
    {
        Point a(4, 5);
        cout << "Point A:" << a.getX() << "," << a.getY();
        Point::showCount();
        Point b(a);
        cout << "Point B:" << b.getX() << "," << b.getY();
        Point::showCount();
        system("pause");
        return 0;
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊