鱼C论坛

 找回密码
 立即注册
查看: 2994|回复: 5

unexpected class 新手求助

[复制链接]
发表于 2016-9-20 20:43:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是小甲鱼上课用的一段代码,但是总是提示我unexpected class  'Dog'

#include <iostream>
#include <string>

class Pet
{
public:
        Pet(std::string theName);
        ~Pet();

        static int getCount();

protected:
        std::string name;

private:
        static int count;
}

class Dog:public Pet
{
public:
        Dog(std::string theName);
};

class Cat:public Pet
{
public:
        Dog(std::string theName);
};

int Pet::count=0;

Pet::Pet(std::string theName)
{
name=theName;
}

Pet::~Pet()
{
        count--;
        std::cout<<name<<"挂掉了\n";
}

int Pet::getCount()
{
return count;
}

Dog::Dog(std::string theName):Pet(theName)
{
}

Cat::Cat(std::string theName):Pet(theName)
{
}

int main()
{
Dog dog("Tom");
Cat cat("Terry");

std::cout<<"\n已经诞生了"<<Pet::getCount()<<"zhichongwu\n\n";
{
        Dog dog2("tom 2");
        Cat cat2("jerry 2");
std::cout<<"\n现在呢,已经诞生了"<<Pet::getCount()<<"只宠物\n\n";
}

std::cout<<"\nAnd you're back to"<<Pet::getCount()<<"Pets!\n\n";

return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-9-20 21:06:45 | 显示全部楼层
我用的vc++6.0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-20 21:21:43 | 显示全部楼层
class Cat:public Pet
{
public:
         Dog(std::string theName);//应该是Cat(std::string theName);
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-21 10:18:29 | 显示全部楼层
这小雨有点大 发表于 2016-9-20 21:21
class Cat:public Pet
{
public:

这个已经改了 但是还是不对 不知道为什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-21 20:59:55 | 显示全部楼层
pet 类结束少一个“;”,再把上次跟你说的dog改成cat,我用的vs2015 运行成功,还有问题的话你最好把错误的提示截图一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-21 21:01:09 | 显示全部楼层
这个就是我运行的


#include <iostream>
#include <string>

class Pet
{
        public:
         Pet(std::string theName);
         ~Pet();

         static int getCount();

        protected:
         std::string name;

        private:
         static int count;
};

class Dog:public Pet
{
        public:
         Dog(std::string theName);
};

class Cat:public Pet
{
        public:
         Cat(std::string theName);
};

int Pet::count=0;

Pet::Pet(std::string theName)
{
name=theName;
}

Pet::~Pet()
{
         count--;
         std::cout<<name<<"挂掉了\n";
}

int Pet::getCount()
{
return count;
}

Dog::Dog(std::string theName):Pet(theName)
{
}

Cat::Cat(std::string theName):Pet(theName)
{
}

int main()
{
Dog dog("Tom");
Cat cat("Terry");

std::cout<<"\n已经诞生了"<<Pet::getCount()<<"zhichongwu\n\n";
{
         Dog dog2("tom 2");
         Cat cat2("jerry 2");
std::cout<<"\n现在呢,已经诞生了"<<Pet::getCount()<<"只宠物\n\n";
}

std::cout<<"\nAnd you're back to"<<Pet::getCount()<<"Pets!\n\n";

return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 03:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表