C++ 类别'';没有名为'的成员';
我已经创建了几个带有相关头和声明/定义函数的源文件。我不断收到一个错误,说一个类没有成员。我正在调用一个set函数,该函数将成员与我的私有成员明确关联。还有什么我应该包括在内的吗C++ 类别'';没有名为'的成员';,c++,C++,我已经创建了几个带有相关头和声明/定义函数的源文件。我不断收到一个错误,说一个类没有成员。我正在调用一个set函数,该函数将成员与我的私有成员明确关联。还有什么我应该包括在内的吗 #ifndef STUDENTCOLLECTION_H #define STUDENTCOLLECTION_H #include<string> #include<vector> #include<iostream> #include<fstream> using n
#ifndef STUDENTCOLLECTION_H
#define STUDENTCOLLECTION_H
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
using namespace std;
#include "StudentProfile.h"
#include "Person.h"
#include "Student.h"
#include "Course.h"
class StudentCollection
{
private:
vector<StudentProfile> StCollection;
public:
void GetInfo();
void PrintCollection();
StudentCollection(){};
StudentCollection(vector<StudentProfile> StCollection){};
};
#endif
StudentProfile.cpp:
#ifndef STUDENTPROFILE_CPP
#define STUDENTPROFILE_CPP
#include<iostream>
using namespace std;
#include "StudentProfile.h"
void StudentProfile::SetAStudentProfile(Person PInfo, Student SInfo)
{
PersonalInfo = PInfo;
StdInfo = SInfo;
}
void StudentProfile::PrintAStudentProfile()
{
}
#endif
您有以下几行:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
StudentProfile New_Stu;
New_Stu.SetAStudentProfile(PInfo, SInfo);
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
试图访问新课程PInfo
和新课程SInfo
,这两个都不属于学生档案
类。您将PInfo
和SInfo
定义为GetInfo()
方法的局部变量-您打算使用这些变量吗?您有以下几行:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
StudentProfile New_Stu;
New_Stu.SetAStudentProfile(PInfo, SInfo);
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
试图访问新课程PInfo
和新课程SInfo
,这两个都不属于学生档案
类。您已将PInfo
和SInfo
定义为GetInfo()
方法的局部变量-是否打算使用这些变量?这些行:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
StudentProfile New_Stu;
New_Stu.SetAStudentProfile(PInfo, SInfo);
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
应该是:
StudentProfile New_Stu;
New_Stu.SetAStudentProfile(PInfo, SInfo);
New_Stu.PersonalInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.StdInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.StdInfo.SetACourse(num, name);
但是您可能还需要将这些成员的访问修饰符更改为public
,因为这是StudentCollection
类中的代码。或者,您可以在StudentProfile
中创建一些额外的setter,这样您就不需要尝试从外部访问PersonalInfo
和StdInfo
成员。这些行:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
StudentProfile New_Stu;
New_Stu.SetAStudentProfile(PInfo, SInfo);
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
应该是:
StudentProfile New_Stu;
New_Stu.SetAStudentProfile(PInfo, SInfo);
New_Stu.PersonalInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.StdInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.StdInfo.SetACourse(num, name);
但是您可能还需要将这些成员的访问修饰符更改为
public
,因为这是StudentCollection
类中的代码。或者,您可以在StudentProfile
中创建一些额外的setter,这样您就不需要尝试从外部访问PersonalInfo
和StdInfo
成员。实际上,在StudenProfile
类中没有PInfo和SInfo成员。如果查看StudentCollection.cpp,您将声明单独的局部变量PInfo和SInfo
Person PInfo;
Student SInfo;
...
稍后在循环中,当您错误使用时:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
我想你们也有为人
和学生
开设的课程。可能Student
有以下成员SetAStudent
和SetACourse
和classPerson
有SetSetAPerson
。在这种情况下,您可以简单地调用(无需使用New_Stu):
实际上,在您的StudenProfile
类中没有PInfo和SInfo成员。如果查看StudentCollection.cpp,您将声明单独的局部变量PInfo和SInfo
Person PInfo;
Student SInfo;
...
稍后在循环中,当您错误使用时:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
我想你们也有为人
和学生
开设的课程。可能Student
有以下成员SetAStudent
和SetACourse
和classPerson
有SetSetAPerson
。在这种情况下,您可以简单地调用(无需使用New_Stu):
我正在使用class StudentProfile中的函数将我的私有成员设置为与其他成员相等的值…如果这有意义的话。我不能直接从外部访问该类的私有成员。因此,在我的set函数中,我将它们设置为可以在类外访问的其他名称。@CarNorum-是的,我正在尝试使用这些局部变量。我正在使用class StudentProfile中的函数将我的私有成员设置为其他名称。@CarNorum-如果有意义的话。我不能直接从外部访问该类的私有成员。所以在我的set函数中,我将它们设置为可以在类外访问的其他名称。@CarNorum-是的,我正在尝试使用这些局部变量。你刚才所说的绝对有意义。我应该从那里调用函数。我想我也应该有一个计数器,这样我可以在以后打印向量中的信息时区分学生。我会做一些改变,看看我的错误是否会消失。我已经意识到错在哪里了。由于New_Stu是StudentProfile类型,我可以直接从我的类访问我的set函数!我不知道我怎么会错过那个!你刚才说的完全有道理。我应该从那里调用函数。我想我也应该有一个计数器,这样我可以在以后打印向量中的信息时区分学生。我会做一些改变,看看我的错误是否会消失。我已经意识到错在哪里了。由于New_Stu是StudentProfile类型,我可以直接从我的类访问我的set函数!我不知道我怎么会错过那个!我被要求所有的类属性都是私有的。如果它是公开的,我现在就不会在这个网站上。@KevinSimpsonII:这应该由StudentProfile
类中的setter来解决。我编辑了我的答案:)我被要求所有的类属性都是私有的。如果它是公开的,我现在就不会在这个网站上。@KevinSimpsonII:这应该由StudentProfile
类中的setter来解决。我编辑了我的答案:)