| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 369 人关注过本帖
标题:c++ 声明与定义
取消只看楼主 加入收藏
xlhcy2014
Rank: 2
等 级:论坛游民
帖 子:33
专家分:81
注 册:2014-2-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
c++ 声明与定义
今天看《c++ primer》,作者建议头文件中写变量声明,在源文件中定义变量。

我有个程序是这样的,我建立了2个头文件 (fish.h,animal.h)3个源文件(fish.cpp,animal.cpp,main.cpp)
我在animal.h 中声明了 animal 类 :    如 class animal;
    fish.h 中声明了 fish类:            如 class  fish;

我分别在它们对应的源文件中定义数据类型;
animal.cpp的内容 :
#include<iostream>
#include"animal.h"
using std::cout;

class animal
{
public:
    animal();
    ~animal();
    void eat(){cout<<"animal can eat something.";}
    void sleep(){cout<<"animal need sleeping.";}
};
animal::animal()
{cout<<"构造了一只动物\n";}
animal::~animal()
{cout<<"销毁了一只动物\n";}

fish.cpp的内容如下:
#include<iostream>
#include"animal.h"
#include"fish.h"
using std::cout;

class fish :public animal
{
public:
    fish();
    ~fish();
    void swim(){cout<<"fish can swim."<<endl;}
private:
    int tall;
};
fish::fish()
{cout<<"构造了一只鱼\n";}
fish::~fish()
{cout<<"销毁了一只鱼\n";}

最后main .cpp的内容如下:
#include<iostream>
#include"fish.h"
#include"animal.h"
using namespace std;
int main()
{
fish myfish;
myfish.eat();
myfish.swim();
return 0;
}

编译没有通过,提示 fish animal 没有定义 ,该怎么改?

图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 xlhcy2014 于 2014-3-24 20:49 编辑 ]
搜索更多相关主题的帖子: 源文件 animal public include something 
2014-03-24 20:47
快速回复:c++ 声明与定义
数据加载中...
 
   



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

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