2013-10-03 113 views
1

我已经创建了几个具有关联标题和声明/定义函数的源文件。我不断收到一个错误,指出一个班级没有成员。我正在调用一个明确地将一个成员关联到我的私人成员的集合函数。还有什么我应该包括在内?类''没有名为''的成员'

#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

你有这些行:

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类的一部分。您有PInfoSInfo定义为局部变量GetInfo()方法 - 您是否打算使用这些?

+0

我在课堂上使用StudentProfile功能设定我的私有成员等于别的东西......如果是有道理的。我不应该直接从外面访问班级的私人成员。所以在我的设置函数中,我已经将它们设置为等于我可以在课堂外访问的其他名称。 – Kevin

+0

@CarNorum - 是的,我试图使用这些局部变量。 – Kevin

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之内创建一些额外的安装人员,以便您不需要尝试从外部访问PersonalInfoStdInfo成员。

+0

我被指示拥有所有类别属性。如果它是公开的,我现在不会在这个网站上。 – Kevin

+0

@KevinSimpsonII:这应该由'StudentProfile'类中的setter解决。我编辑了我的答案:) – LihO

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); 

我想你也有类PersonStudent。可能Student有以下成员SetAStudentSetACourse和类PersonSetSetAPerson。在这种情况下,你可以简单地调用(不New_Stu):

PInfo.SetSetAPerson(SN, first, last, a, sex); 
SInfo.SetAStudent(ID, Class1, Class2, Class3); 
SInfo.SetACourse(num, name); 
+0

你刚刚说的话是绝对有道理的。我应该从那里调用这个函数。我想我应该也有一个柜台,这样我才能最终从我的媒体中打印信息,从而将学生从另一个中区分出来。我会稍作修改,看看我的错误是否会消失。 – Kevin

+0

我意识到什么是错的。由于New_Stu是StudentProfile类型,我可以直接从我的课程中访问我的设置功能!我不知道我是如何错过的! – Kevin

相关问题