Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 希望创建一个类,其中变量可用于其他类 #包括 #包括“people.h” #包括“生日.h” #包括 int main(){ std::字符串myname; INTA; int b; INTC; 标准::cout_C++ - Fatal编程技术网

C++ 希望创建一个类,其中变量可用于其他类 #包括 #包括“people.h” #包括“生日.h” #包括 int main(){ std::字符串myname; INTA; int b; INTC; 标准::cout

C++ 希望创建一个类,其中变量可用于其他类 #包括 #包括“people.h” #包括“生日.h” #包括 int main(){ std::字符串myname; INTA; int b; INTC; 标准::cout,c++,C++,第一点 #include <iostream> #include "people.h" #include "birthday.h" #include <string> #include "askquestions.h" int main(){ askquestions getquestions(); birthday birthdayobjects(a ,b ,c); people peopleobject

第一点

    #include <iostream>
    #include "people.h"
    #include "birthday.h"
    #include <string>
    #include "askquestions.h"

int main(){
    askquestions getquestions();
    birthday birthdayobjects(a ,b ,c);
    people peopleobjects(myname, birthdayobjects);
    peopleobjects.printInfo();
}
是错误的(它声明了一个名为
getquestions
的函数,而不是一个变量),应该是

askquestions getquestions();
像这样设计你的
askquestions
课程

askquestions getquestions;
class askquestions
{
public:
     askquestions();
     int geta() { return a; }
     int getb() { return b; }
     int getc() { return c; }
private:
     int a;
     int b;
     int c;
};
三个
getX
方法称为getter,是从类中获取信息的典型方法

然后在主函数中使用如下getter

askquestions getquestions;
class askquestions
{
public:
     askquestions();
     int geta() { return a; }
     int getb() { return b; }
     int getc() { return c; }
private:
     int a;
     int b;
     int c;
};

查看如何使用您先前声明的
getquestions
变量调用getter方法。

没有理由将
askquestions
作为一个类。为此,您应该更喜欢返回
生日的免费函数,或者更好的是返回
人的

比如说

int main()
{
    askquestions getquestions;
    birthday birthdayobjects(getquestions.geta(), getquestions.getb(), getquestions.getc());
    ...
}

这听起来很有趣,但是
askquestions getquestions();
是一个函数声明,而不是一个变量定义。使用
askquestions getquestions;
askquestions getquestions{};
来代替。这是一个输入错误,假设它是askquestion类的对象
int main(){
    people peopleobjects = getquestions();
    peopleobjects.printInfo();
}