No matching function for: insert

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <string>
#include <fstream>
#include <iostream>
using namespace std;
 
int main()
{
    int i;//counter
    string line;//Currentb string from f1
    string text;//Currentb string from f1
    ifstream f1("f1.txt");//
    ofstream f2("f2.txt");//
    if(!f1.is_open())
        cout<<"Error open f1.txt"<<endl;
    else
    if(!f2.is_open())
        cout<<"Error open f2.txt"<<endl;
    else
    {
        while(getline(f1, line,'.'))
        {
            //string clearing
            for(i = 0; i < line.size(); i++)
            {
                if(line[i] == '\n' || line[i] == '\r')
                    line.erase(1, i);
            }
            text = "";//string clearing
            text.insert(0,line.begin(),40);
            cout<<text<<endl;
            f2  <<text<<endl;
            //Если длинна line > 40 бьём стркоу на части
            for(i = 1; i < line.size() / 40; i++)
            {
                text = "";//string clearing
                text.insert(0,line.begin() + 40*i,40);
                cout<<text<<endl;
                f2  <<text<<endl;
            }
            if(line.size() % 40)
            {
                text = "";//string clearing
                text.insert(0,line.begin() + 40*i,line.size() % 40);
                cout<<text<<endl;
                f2  <<text<<endl;
            }
        }
    }
    f1.close();
    f2.close();
    return 0;
}
In this code we copy the contect from f1 to f2. Then decompose it by strings (lines) thsi way that every string should be remain the same, but if thisi string do not contain string it should contain 40 letters (characters).
But when compling this code there is an error:
error:no matching function for call to 'std::basic_string<cchar, std::char_traits<char...
at the 23rd line near text.insert and yet in other two places. What to correct?
There is no version of insert taking an integer, a string iterator and an integer as argument in that order.
http://en.cppreference.com/w/cpp/string/basic_string/insert

It is totally unclear what you are trying to do by means of statement

text.insert(0,line.begin(),40);

If you want to assign 40 characters from string line to text then you can write

text.assign( line, 0, 40 );
Last edited on
Without testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
#include <fstream>
#include <iostream>

using namespace std;
 
int main()
{
    ifstream f1( "f1.txt" );
    ofstream f2( "f2.txt" );

    if ( !f1.is_open() )
    {
        cout<<"Error open f1.txt"<<endl;
        return 1;
    }

    if ( !f2.is_open() )
    {
        cout<<"Error open f2.txt"<<endl;
        return 2;
    }

    string f1_line;
    while ( getline( f1, f1_line, '.' ) )
    {
            //string clearing
            for ( string::size_type i = 0; i < f1_line.size(); /*empty */ )
            {
                if ( f1_line[i] == '\n' || f1_line[i] == '\r' ) f1_line.erase( i, 1 );
                else i++;
            }

            const string::size_type SLICE = 40;

            for ( string::size_typei = 0; i < f1_line.size();  i += SLICE )
            {
                string f2_line = f1_line( i, SLICE );
                cout << f2_line << endl;
                f2  << f2_line << endl;
            }
        }
    }

    return 0;
}
Last edited on
This code I found in the net. It is solving of one of handbook task. That I also should to do. The task is also some unclear for me. As I understand the content of copied file to another shoud be displayed line by line. If line is ended with point so it should be remained so in other case it shoud be substituted by 46 letter. Maybe we can use assign function. But we need delineate what string to append. There is three lines in codes with unworkable 3 strings with insert. You also see that there is function begin . In the second case the 40 i is added. So it difficutl to guess what the author meant but I need this code urgently. Anyway. I do not know how display text or contain it line by line. If i could I shoud to search in the text to the line with point and do not change and than to search without point and change to some 40 letter combination. So I am Interested how to change it. Maybe it needs to find the token of line transfer for example \n and them easy to find the point?
Topic archived. No new replies allowed.