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

时间:2013-10-06 09:17:57

标签: c++ string vector

您好我很难理解编译器的内容

  

[BCC32错误] frmNew.cpp(333):E2285找不到匹配&#39; std :: getline&lt; _Elem,_Traits,_Alloc&gt;(ifstream,std :: vector&gt;)&#39;     完整的解析器上下文       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&lt;&gt;

3 个答案:

答案 0 :(得分:3)

std::getline的第二个参数可能是std::string,但不是std::vector<std::string>。错误消息显示非常清楚。

更新

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

为:

std::string mystring;

您不会发布myline声明的方式,我认为它是std::vector<std::string>

答案 1 :(得分:1)

你向getline传递错误的论点。

mystring不应该是std::string,而是std::vector。这样一线 std::getline(ifs_Awal,mystring)导致错误,因为第二个参数不是std::string

另外,行

myline.push_back(mystring)

不起作用,因为myline可能是字符串的向量,并且您尝试将vector<string>的元素推送到它。

因此,正如已建议的那样,将myline更改为std::string就是答案。

答案 2 :(得分:1)

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();
    }
相关问题