1

Hi it's a little bit hard for me to understand what the compiler is saying:

[BCC32 Error] frmNew.cpp(333): E2285 Could not find a match for 'std::getline<_Elem,_Traits,_Alloc>(ifstream,std::vectorstd::string,std::allocator<std::string >)' Full parser context frmNew.cpp(303): parsing: void _fastcall TFrmNewPeta::showDefaultRute()

I'm using std::vector<std::string>mystring to store my strings file, but while (std::getline(ifs_Awal, mystring)) throws the error.

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.

In many tutorials they prefer to using std::vector to store a string to a dynamic array. So I did the same thing, but the error occurred when I tried using std::vector<>.

0

3 Answers 3

3

second parameter of std::getline could be std::string but not 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>.

Sign up to request clarification or add additional context in comments.

Comments

1

billz and tomi rights your passing wrong argument so I change your code. 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();
    }

2 Comments

thanks for change my code to be the correct answer. I've been do the same way as you answer. thanks
I'm so glad if that's you want. also you can use the members functions of the vector at() to shown the element. example std::cout<<mystring.at(0)<<endl;
1

Your passing wrong argument to getline.

mystring is not a std::string as it should be, but a std::vector. Thus line std::getline(ifs_Awal,mystring) causes an error since the second argument is not std::string.

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.

So, as suggested already, changing myline to std::string is the answer.

1 Comment

Hi @Tomi.lee.jones, I realize I'm wrong declare using mystring as string to use as vector. so I change my code as you suggest and its works, Thanks for your advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.