Andrew Marshall
Andrew Marshall

Reputation: 149

error when inserting a char via string::insert

My question is similar to this one but there is no sample code there, and I didn't find it helpful.

My error is

calling a private constructor of class 'std::__1::__wrap_iter'

A minimal example of my problem can be found below:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string g = "this_word";
    cout << g << endl;
    char temp = g[0];
    g.erase(0,1);
    cout << g << endl;
    g.insert(0,temp); // Compiler seems to dislike this.  Why?
    cout << g << endl;
    return 0;
}

I've tried this through two compilers, and the same error. Read as much as I could from the standard documentation, but don't understand my error.

Upvotes: 3

Views: 189

Answers (1)

songyuanyao
songyuanyao

Reputation: 172924

It would be better to check all the signatures of overloads of std::string::insert() then decide to use which one. g.insert(0,temp); just doesn't match any of them.

For inserting a char, you can pass an iterator like

g.insert(g.begin(), temp);

or pass the index and count together:

g.insert(0, 1, temp);

Upvotes: 5

Related Questions