2013-10-03 55 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

相關問題