简体   繁体   English

使用std :: vector时出错 <std::string> myString

[英]Error Using std::vector<std::string> myString

Hi I'm little bit hard to understand what that the compiler says 嗨,我有点难以理解编译器的内容

[BCC32 Error] frmNew.cpp(333): E2285 Could not find a match for 'std::getline<_Elem,_Traits,_Alloc>(ifstream,std::vector >)' Full parser context frmNew.cpp(303): parsing: void _fastcall TFrmNewPeta::showDefaultRute() [BCC32错误] frmNew.cpp(333):E2285找不到与'std :: getline <_Elem,_Traits,_Alloc>(ifstream,std :: vector>)'匹配的完整解析器上下文frmNew.cpp(303):解析:void _fastcall TFrmNewPeta :: showDefaultRute()

I'm using std::vector<std::string>mystring to stored my strings file. 我正在使用std::vector<std::string>mystring来存储我的字符串文件。 but this code while (std::getline(ifs_Awal, mystring)) I get the error. 但是这段代码while (std::getline(ifs_Awal, mystring))我得到了错误。

this is my complete code 这是我完整的代码

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();
}

I'm using c++ builder 2010 我正在使用C ++ Builder 2010

in many tutorials they prefer to using std::vector to store string to dynamic array. 在许多教程中,他们更喜欢使用std :: vector将字符串存储到动态数组。 so I did the same ways, but this happened when I try using std::vector<> 所以我做了同样的方法,但是当我尝试使用std :: vector <>

second parameter of std::getline could be std::string but not std::vector<std::string> . std::getline第二个参数可以是std::string但不能是std::vector<std::string> It's quite clear as error message shows. 错误消息显示很清楚。

update 更新

std::vector<std::string> mystring(tempIndexAwal);

to: 至:

std::string mystring;

You do not post how you declare myline , I suppose it's std::vector<std::string> . 您不发布如何声明myline ,我想它是std::vector<std::string>

Your passing wrong argument to getline . 您对getline传递错误的论点。

mystring is not a std::string as it should be, but a std::vector . mystring不是应该的std::string ,而是std::vector Thus line std::getline(ifs_Awal,mystring) causes an error since the second argument is not std::string . 因此std::getline(ifs_Awal,mystring)由于第二个参数不是std::string std::getline(ifs_Awal,mystring)因此std::getline(ifs_Awal,mystring)会导致错误。

Also, line 还行

myline.push_back(mystring)

does not work because myline is probably a vector of string, and you try to push an element of vector<string> to it. 之所以myline是因为myline可能是字符串的向量,并且您尝试将vector<string>的元素推入其中。

So, as suggested already, changing myline to std::string is the answer. 因此,正如已经建议的那样,将myline更改为std::string是答案。

billz and tomi rights your passing wrong argument so I change your code. billz和tomi会为您传递错误的参数提供权利,因此我更改了您的代码。 should be 应该

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM