0

我想的Student試圖對象的數組傳遞給函數

數組傳遞到功能processStudent(string myFilename, Student* myArray, int &mySize)時得到錯誤。 但它給了我不同的錯誤。

學生()什麼也不做,但我想給它們分配某種價值,但它仍然給出確切的同樣的錯誤信息:

在主我有這樣的:

// Create an array of students, with the size of theMax (256) 
Student* arrayOfStudent= new Student[theMax]; 

// An integer that will keep track of actually how many students 
// Because when we loop, we want to loop within the cell 
// that actually have data or student. 
int actualSize = 0; 

// Invoke the helper function to set up the array of students 
// It passed the arryOfStudent by reference, so the changes 
// inside of the function will be reflected when it returns 
processStudent(filename, arrayOfStudent, actualSize); 

的功能是這樣的:

void processStudent(string myFilename, Student* myArray, int& mySize) 
{ 
    // Something here, but removed still gives that error 
} 

//在課堂上學生的CPP文件

Student::Student() 
{ 
    // Nothing here 
} 

錯誤消息:

new-host-2:csci135p1 george$ g++ -Wall -o csci135p2main csci135p2main.cpp 
Undefined symbols for architecture x86_64: 
    "Student::Student()", referenced from: 
     _main in cc3fTXti.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 

我一直在剝離和剝離下來我的代碼,但這個錯誤是不會消失。我想創建這個數組,並將其傳遞給processStudent函數,因此它可以在讀取文件時設置每個數組。

+1

聽起來像一個鏈接錯誤,你怎麼建設? – Shep 2012-04-13 14:36:23

回答

1

你應該問自己一些問題,可以幫助你:

「如何創建學生的新實例?」
嗯,我不喜歡這樣寫道:Student* s = new Student(); - 它創造了新的對象和referrence存儲到它作爲一個指針(Student*

「那麼,如何創造的10 Student秒的陣列?」
那麼,它可能會是指向新Student組成的數組,我可能要打電話不止一次new更多...通過這樣的想法,你會很容易地像這樣結束:

Student** students = new Student*[10]; 
for (int i = 0; i < 10; ++i) 
    students[i] = new Student(); 

......這意味着當你清理它時,你必須每Student*打電話delete加上你必須清理陣列本身:在Student**上調用delete[] - 當你意識到醜陋的內存管理連接指針數組,它應該讓你尋找更簡單和更好的實現方法,因此你最終應該使用std::vector,而不是數組,對象而不是指針,如果可能的話,如果不是,則至少使用智能指針而不是裸指針。

+0

我的情況很好的解釋...真的很好。謝謝。但是,當然,第一年的作業,沒有矢量是允許的..但我可以更多地理解我的問題,處理指針是非常棘手的。 – George 2012-04-13 14:56:21

+1

是的,我理解你的觀點。編寫這樣的程序是爲了理解基礎知識,這很好,但是問題是你應該認識到有更好的代碼編寫方法,所以不要太習慣它:)我建議你[本講座](http://www.youtube.com/watch?v=OB-bdWKwXsU),特別是0:12:50部分叫做「Ghastly Style」,0:27叫「資源與錯誤」部分:30 ...它會讓你更多地思考它;) – LihO 2012-04-13 15:21:56

1

有錯誤信息,告訴你答案:

Student*’ to ‘Student**’

更改myArray爭執中processStudent()類型:

void processStudent(string myFilename, Student** myArray, int& mySize) 
{ 
} 

當一個數組傳遞給它衰變到一個指針的函數:

void some_func(char* b) {...} 

char buf[100]; 
some_func(buf); 

當您通過arrayOfStudent數組衰減到一個指針,它恰好是該數組是一個指針數組,因此**


鏈接器抱怨它找不到默認構造函數的定義Student。該定義包含在Student.cpp文件,以便編譯所有的源文件來解決鏈接錯誤:

g++ -Wall -o csci135p2main csci135p2main.cpp Student.cpp

+0

你仍然可以使用'myArray [1]'。 – hmjd 2012-04-13 14:52:16

1

的第一個錯誤(這似乎已經從因爲我寫了這個問題去掉)是告訴你,第二個參數processStudent是一個指針指針,Student**,而不是指針,Student*。這很奇怪,因爲你用Student*參數顯示你的聲明;你確定這是你的代碼的實際聲明嗎?你有不同的聲明嗎?

第二個錯誤可能是因爲你沒有鏈接到包含構造函數定義的單元。你只需要一個源文件(csci135p2main.cpp)就可以調用編譯器,我猜測你在另一個源文件中定義了構造函數。

+0

是的,第二個錯誤是因爲我沒有包含我的OO類的源文件。簡直不敢相信在過去的2個小時裏我怎麼也弄不清楚。 – George 2012-04-13 14:41:16

+1

這意味着你應該得到一些休息;) – zmo 2012-04-13 14:44:48

0

你最好使用學生的向量(更喜歡使用數組只與基本類型(int,char ..)和其他所有向量)。

#include <vector> 

std::vector<Student*> arrayOfStudent = new vector<Student*>(); 
/* ... */ 
processStudent(filename, arrayOfStudent, actualSize); 

void processStudent(std::string& filename, vector<Student*>& arrayOfStudent, int& actualSize) { 
/* ... */ 
} 

我不知道是否會解決你的問題,但至少這是一個更好的利用......

+0

不能使用'矢量',但是謝謝.. – George 2012-04-13 14:43:33

+2

'std :: vector '是「leaktrocity」的祕訣。使用'std :: vector '''學生'執行_move semantics_或'vector >'或'vector >'。 – 2012-04-13 14:54:18

+0

我的歉意,你是對的;) – zmo 2012-04-13 16:40:25

0

你的代碼是容易出錯的難以維護的異常不安全的C++。 在現代C++中,你應該使用std::vector<Student>,與Student類基於所有權實現移動語義,或vector<shared_ptr<Student>>vector<unique_ptr<Student>>Student實例(共享與否)。

而且你的函數原型應該是這樣的,如果學生陣列是一個輸入/輸出參數:

void processStudent(const std::string & filename, std::vector<Student> & students); 

相反,如果processStudent函數創建學生的陣列(例如,基於文件的內容),只是返回它作爲返回值(這是非常有效的感謝移動語義):

std::vector<Student> processStudent(const std::string & filename);