【发布时间】:2013-03-18 11:47:55
【问题描述】:
我创建了一个类:
Data::Data(char szFileName[MAX_PATH]) {
string sIn;
int i = 1;
ifstream infile;
infile.open(szFileName);
infile.seekg(0,ios::beg);
std::vector<std::string> fileRows;
while ( getline(infile,sIn ) )
{
fileRows.push_back(sIn);
}
}
之后我创建了这个:
std::vector<std::string> Data::fileContent(){
return fileRows;
}
在那之后,我想在某个地方称它为 fileContent(),如下所示:
Data name(szFileName);
MessageBox(hwnd, name.fileContent().at(0).c_str() , "About", MB_OK);
但这不起作用...如何调用它?
【问题讨论】:
-
什么是
Adatkezeles? -
我猜你想让
fileRows成为Data的成员? -
对不起 Adatkezeles = 数据数据名称(szFileName); MessageBox(hwnd, name.fileContent().at(0).c_str() , "关于", MB_OK);我得到了这个错误:在抛出 'std::out_of_range' what() 的实例后调用终止:vector::_M_range_check
-
@David:请使用edit 链接使用此信息更新您的问题。此外,您可能希望
Data::fileContent()(a) 成为const-qualified 并且 (b) 通过引用返回std::vector<std::string>。
标签: c++ string function vector