![](/img/trans.png)
[英]using std::vector<std::string*> and std::vector<std::ofstream*>
[英]Error Using std::vector<std::string> myString
嗨,我有點難以理解編譯器的內容
[BCC32錯誤] frmNew.cpp(333):E2285找不到與'std :: getline <_Elem,_Traits,_Alloc>(ifstream,std :: vector>)'匹配的完整解析器上下文frmNew.cpp(303):解析:void _fastcall TFrmNewPeta :: showDefaultRute()
我正在使用std::vector<std::string>mystring
來存儲我的字符串文件。 但是這段代碼while (std::getline(ifs_Awal, mystring))
我得到了錯誤。
這是我完整的代碼
void __fastcall TFrmNewPeta::showDefaultRute()
{
std::string mystring;
std::ifstream ifs_Awal;
int tempIndexAwal = 0;
ifs_Awal.open("DefaultDataAwal");
while (std::getline(ifs_Awal, mystring)) {++tempIndexAwal;}
std::vector<std::string> mystring(tempIndexAwal);
while (std::getline(ifs_Awal, mystring)) // error
{
mystring.push_back(mystring); // error
}
ifs_Awal.close();
}
我正在使用C ++ Builder 2010
在許多教程中,他們更喜歡使用std :: vector將字符串存儲到動態數組。 所以我做了同樣的方法,但是當我嘗試使用std :: vector <>
std::getline
第二個參數可以是std::string
但不能是std::vector<std::string>
。 錯誤消息顯示很清楚。
更新
std::vector<std::string> mystring(tempIndexAwal);
至:
std::string mystring;
您不發布如何聲明myline
,我想它是std::vector<std::string>
。
您對getline
傳遞錯誤的論點。
mystring
不是應該的std::string
,而是std::vector
。 因此std::getline(ifs_Awal,mystring)
由於第二個參數不是std::string
std::getline(ifs_Awal,mystring)
因此std::getline(ifs_Awal,mystring)
會導致錯誤。
還行
myline.push_back(mystring)
之所以myline
是因為myline
可能是字符串的向量,並且您嘗試將vector<string>
的元素推入其中。
因此,正如已經建議的那樣,將myline
更改為std::string
是答案。
billz和tomi會為您傳遞錯誤的參數提供權利,因此我更改了您的代碼。 應該
void __fastcall TFrmNewPeta::showDefaultRute() {
std::string lines;
std::ifstream ifs_Awal;
int tempIndexAwal = 0;
ifs_Awal.open("DefaultDataAwal");
/*get the strings and counting the lines*/
while(std::getline(ifs_Awal,lines)){++tempIndexAwal;}
std::vector<std::string> mystring(tempIndexAwal);
while(std::getline(ifs_Awal,lines)) //put your 'lines' here
{
mystring.push_back(lines); // theres no error again :)
}
ifs_Awal.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.