我是C++的新手,但我還不太瞭解。我有這個奇怪的問題。我有正常工作的功能,但是當我嘗試運行它作爲沒有任何改變一個類的成員函數,它不工作 它說: 未定義參考gsiread :: get_rows(字符*)函數編譯正確,但它不作爲類的成員函數編譯
#include <string>
#include <vector>
#include <fstream>
using namespace std;
//vector<string> get_rows (char filepath[]); ... it works
class gsiread {
public:
vector<string> get_rows (char filepath[]); ... it doesnt work
private:
};
vector<string> get_rows (char filepath[]) {
vector<string> data;
string str;
ifstream file;
file.open(filepath);
if(file.is_open()) {
while(getline(file,str)) {
if (str.empty()) continue;
data.push_back(str);
}
}
return data;
}
// This part is "like" main i am using Qt creator and i have copied parts of code
from separate files
gsiread obj;
vector<string> vypis;
vypis = obj.get_rows("ninja.txt"); ....... //This doesnt work
vypis = get_rows("ninja.txt"); .......... //This works if I put declaration of
//function get_rows outside the class and
//and use // on declaration inside the class
for(int i = 0; i < vypis.size(); i++) {
QString n = QString::fromStdString(vypis[i]);
QString t = "%1 \n";
ui->plainTextEdit->insertPlainText(t.arg(n));
// QString is like string but zou have to use it if wanna use widgets
// of qt (I think)
}
它幫了很多,謝謝。 :) – Rokusjar