我無法在另一個類中訪問一個類的成員函數,但我可以在main()中很好地訪問它。我一直在嘗試切換,但我無法理解我做錯了什麼。任何幫助,將不勝感激。如何訪問另一個類中的一個類的成員函數?
這裏是生成該錯誤的行:
cout << "\n\nRetrieve key from inside Envelope class: " << e.getData() << "\n\n";
這裏是代碼:
class Record{
private:
string key;
public:
Record(){ key = ""; }
Record(string input){ key = input; }
string getData(){ return key; }
Record operator= (string input) { key = input; }
};
template<class recClass>
class Envelope{
private:
recClass * data;
int size;
public:
Envelope(int inputSize){
data = new recClass[inputSize];
size = 0;
}
~Envelope(){ delete[] data; }
void insert(const recClass& e){
data[size] = e;
cout << "\n\nRetrieve key from inside Envelope class: " << e.getData() << "\n\n";
++size;
}
string getRecordData(int index){ return data[index].getData(); }
};
int main(){
Record newRecord("test");
cout << "\n\nRetrieve key directly from Record class: " << newRecord.getData() << "\n\n";
Envelope<Record> * newEnvelope = new Envelope<Record>(5);
newEnvelope->insert(newRecord);
cout << "\n\nRetrieve key through Envelope class: " << newEnvelope->getRecordData(0) << "\n\n";
delete newEnvelope;
cout << "\n\n";
return 0;
}
什麼是錯誤訊息? – 0x499602D2