鱼C论坛

 找回密码
 立即注册
查看: 3606|回复: 4

不能创建实例

[复制链接]
发表于 2013-9-12 15:22:17 | 显示全部楼层 |阅读模式
10鱼币
如下代码:
           为什么不能创建实例
// cpluscplus.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "string"
#include <iostream>
#include <stdlib.h>
int main()
{
      int sel=0;
      int cs;
      Girl g1;
      g1.name="xiao li";
      g1.sg=165;
      g1.tz=48;
      for (;3!=sel;)
      {
            std::cout<<"要做什么?"<<std::endl;
            std::cout<<"1.做活"<<std::endl;
            std::cout<<"2.吃饭"<<std::endl;
            std::cout<<"3.退出"<<std::endl;
            std::cin>>sel;
            switch (sel)
            {
            case 1:
                  std::cout<<"要做几次:"<<std::endl;
                  std::cin>>cs;
                  for (int i=0;i<cs;i++)
                  {
                        //g1.work();
                  }
                  break;
            case 2:
                  //g1.eat(50);
                  break;
            case 3:
                  break;
            default:
                  std::cout<<"错误选择"<<std::endl;
                  break;
            }
      }

      system("pause");
      return 0;
}

class Girl
{
public:
      std::string name;
      int age;
      int sg;
      int tz;
      int pl;
      void work();
      void eat(int);

};
void Girl::work()
{
      pl-=20;
      std::cout<<"开始做活"<<std::endl;
      std::cout<<"精力度:"<<pl<<std::endl;
      if (pl>80)
      {
            std::cout<<"精力好……"<<std::endl;
      }
      else if(pl<=80&&pl<20)
      {
            std::cout<<"精力一般……"<<std::endl;
      }
      else if(pl<20&&pl>0)
      {
            std::cout<<"再不给饭吃,我要罢工了……"<<std::endl;
      }
      else if(pl<0)
      {
            std::cout<<"罢工!!罢工……"<<std::endl;
      }
};
void Girl::eat(int eat)
{
      std::cout<<"正在吃饭……"<<std::endl;
      pl+=eat;
};
未标题-1.jpg 没法实例类,只是一实例就出错,请教下如何改?

最佳答案

查看完整内容

把类Gril声明放在main()前面。就可以了 即: #include "string" #include #include class Girl { public: std::string name; int age; int sg; int tz; int pl; void work(); void eat(int); }; int main() { …… } ……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-12 15:22:18 | 显示全部楼层
本帖最后由 苹果沃珂 于 2013-9-12 15:30 编辑

把类Gril声明放在main()前面。就可以了
即:
#include "string"
#include <iostream>
#include <stdlib.h>


class Girl
{
public:
        std::string name;
        int age;
        int sg;
        int tz;
        int pl;
        void work();
        void eat(int);

};


int main()
{
……
}
……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-9-12 15:50:16 | 显示全部楼层
本帖最后由 fkeuvfhpyygy 于 2013-9-12 15:56 编辑

那请教下没有有办法让其放在main()后也行呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-12 16:19:26 | 显示全部楼层

你可能对“声明”和“定义”有些混淆。它们是不一样的。
C/C++要求;对象或变量在使用前必须声明。所以在main()中创建对象前,需要有类的声明。
函数的声明一是样的:在使用前必须声明。所以不能让声明放在调用后面。即“让其放在main()后”
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-9-12 17:14:21 | 显示全部楼层
苹果沃珂 发表于 2013-9-12 16:19
你可能对“声明”和“定义”有些混淆。它们是不一样的。
C/C++要求;对象或变量在使用前必须声明。所以在 ...

好的,感谢了哈
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-6 07:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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