1

我已经创建了几个带有关联头文件和声明/定义函数的源文件。我不断收到一个错误,说一个类没有成员。我正在调用一个 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

StudentProfile.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

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

这是我编辑的错误(我在其他地方遇到了另一个对象问题,但我确信原因是相同的):

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’
4

3 回答 3

6

这些行:

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

于 2013-10-03T20:26:31.540 回答
4

你有这些行:

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.PInfoNew_Stu.SInfo,它们都没有被定义为StudentProfile类的一部分。您已经定义为方法的PInfo局部变量- 您打算使用这些变量吗?SInfoGetInfo()

于 2013-10-03T20:25:19.783 回答
1

实际上,在您的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我猜你也有and的课程Student。大概Student有以下成员SetAStudentSetACoursePersonSetSetAPerson。在这种情况下,您可以简单地调用(没有 New_Stu):

PInfo.SetSetAPerson(SN, first, last, a, sex);
SInfo.SetAStudent(ID, Class1, Class2, Class3);
SInfo.SetACourse(num, name);
于 2013-10-03T20:38:07.073 回答