我是 C++ 新手,我还不太了解。我有这个奇怪的问题。我有一个正常工作的函数,但是当我尝试将它作为类的成员函数运行而不做任何更改时,它不起作用它说:未定义对 gsiread::get_rows(char *) 的引用
#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 )
}