类没有命名的成员
Class '' has no member named ''
本文关键字:成员 更新时间:2023-10-16
我已经创建了几个带有关联标头和声明/定义函数的源文件。 我不断收到一个错误,说一个类没有成员。 我正在调用一个 set 函数,该函数清楚地将成员与我的私有成员相关联。 还有什么我应该包括的吗?
#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
它是.cpp文件:
#ifndef STUDENTCOLLECTION_CPP
#define STUDENTCOLLECTION_CPP
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
#include "StudentCollection.h"
#include "StudentProfile.h"
#include "Student.h"
#include "Person.h"
#include "Course.h"
void StudentCollection::GetInfo()
{
long SN;
string first;
string last;
string a;
char sex;
long ID;
Course Class1;
Course Class2;
Course Class3;
long num;
string name;
Person PInfo;
Student SInfo;
ifstream fin;
fin.open("info.txt");
fin >> first >> last >> SN >> a >> sex >> ID >> SInfo.Class1.num >> SInfo.Class1.name >> SInfo.Class2.num >> SInfo.Class2.name >> SInfo.Class3.num >> SInfo.Class3.
name;
while (!fin.eof())
{
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);
StCollection.push_back(New_Stu);
fin >> first >> last >> SN >> a >> sex >> ID >> SInfo.Class1.num >> SInfo.Class1.name >> SInfo.Class2.num >> SInfo.Class2.name >> SInfo.Class3.num >> SInfo.Class3.
name;
}
fin.close();
}
void StudentCollection::PrintCollection()
{
}
#endif
学生档案.h 文件:
#ifndef STUDENTPROFILE_H
#define STUDENTPROFILE_H
using namespace std;
#include "Person.h"
#include "Student.h"
class StudentProfile
{
private:
Person PersonalInfo;
Student StdInfo;
public:
void SetAStudentProfile(Person PInfo, Student SInfo);
void PrintAStudentProfile();
StudentProfile(){};
StudentProfile(Person PInfo, Student SInfo){};
};
#endif
学生档案.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
这是我编辑的错误(我在其他地方的对象有另一个问题,但我确定原因是一样的):
StudentCollection.cpp:43: error: ‘class StudentProfile’ has no member named ‘PInfo’
StudentCollection.cpp:44: error: ‘class StudentProfile’ has no member named ‘SInfo’
StudentCollection.cpp:45: error: ‘class StudentProfile’ has no member named ‘SInfo’
这些行:
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
中创建一些额外的资源库,这样您就不需要尝试从外部访问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);
它试图访问New_Stu.PInfo
和New_Stu.SInfo
,两者都没有被定义为StudentProfile
类的一部分。 您PInfo
和SInfo
定义为GetInfo()
方法的局部变量 - 您是否打算使用这些变量?
事实上,
在你的StudenProfile
类中,没有PInfo和SInfo成员。如果你看一下 StudentCollection.cpp你声明了单独的局部变量 PInfo 和 SInfo
Person PInfo;
Student SInfo;
...
稍后在while
循环中,您错误地使用了:
New_Stu.PInfo.SetSetAPerson(SN, first, last, a, sex);
New_Stu.SInfo.SetAStudent(ID, Class1, Class2, Class3);
New_Stu.SInfo.SetACourse(num, name);
我想你也有Person
和Student
的课程.可能Student
有以下成员SetAStudent
和SetACourse
和类Person
有SetSetAPerson
。在这种情况下,您可以简单地调用(无需New_Stu):
PInfo.SetSetAPerson(SN, first, last, a, sex);
SInfo.SetAStudent(ID, Class1, Class2, Class3);
SInfo.SetACourse(num, name);
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 我们可以访问一个不存在的联盟的成员吗