-1

所以我有这个问题。我想从一个不同的类中获取一些变量,然后是主类,我知道隐藏你的数据是很好的,这样它就不会那么容易地改变并使用 getXXX 函数来访问它。我尝试使用private:public:东西但是当我这样做时我得到一个错误说

错误:“私人”之前的预期不合格 ID

我得到了类 nr1# 称为对话框,带有变量的类称为种族(不像黑白那样)

无论如何我这样调用函数:(类对话框)

    above this is all the #include stuff
    dialog::dialog(int y)
    {
        race raceO;
       switch(y)
       {

    case 1: cout << "choose a class \n1     ELF = " << raceO.getStats(1.1) << endl;
break:
}

这是比赛课

//我应该把私有:和公共:

    include "race.h"
    include <iostream>
    include <string>

    using namespace std;



    race::race(){
    }

    int race::raceElf(){
    return 0;
    }

        int attack = 5;
        int defence = 3;
        int stamina = 6;


    int race::getStats(int x){

    if(x == 11){
      return attack;
    }
    return 0;
    }
4

2 回答 2

0

在类声明中它应该看起来像

class myClass{
public:
myClass();
private:
double x,y,z;
}

这就是你应该如何使用公共和私人,但否则我看不出有什么问题,请提供头文件或类声明。

于 2013-08-23T17:00:21.140 回答
0

这取决于你想如何使用你的类。我会将您的所有数据成员保密。方法应该是公开的,因为你会在你的对象上调用它们raceElf()getStats()如果一个方法仅在其他类方法中使用而不在外部使用,则它应该是私有的。如果要创建类的任何对象,构造函数必须是公共的。

class race{
public:
    race();
    int raceElf();
    int getStats(int);
private:
    int attack;
    int defence;
    int stamina;
}

race::race(){
    attack = 5;
    defence = 3;
    stamina = 6;
}

int race::raceElf(){ return 0;  }

int race::getStats(int x){
        if(x == 11){
            return attack;
        }
        return 0;
 }
于 2013-08-23T17:04:59.660 回答