Manish Jha
Manish Jha

Reputation: 1

no matching function for call to 'std::vector<std.. '

The question I am solving states: I have to take T test cases. For each test case I have to take string as an input, then I need to arrange the input string as: string at even position {double space} string at odd position (example: input - StackOverflow, output - Sakvrlw tcOefo). I've written the following code where I am taking input for all test cases and storing it in a vector. Then I am assigning the elements of vector to another declared string s.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
     int T,i;
     cout << "Enter no. of test cases: ";
     cin >> T;

     vector<string> v;
     vector<string> odd;
     vector<string> even;
     string str;

     for(int i=0; i<T; i++){
        cin >> str;
        v.push_back(str);
     }


     string s;

     for(i=0; i<v.size(); i++){
        s = v.at(i);

        for(i=0; i<s.size(); i++){
            if(i==0){
                even.push_back(s[i]);  //*This is where I am getting error*.
            }else if(i==1){
                odd.push_back(s[i]);
            }else{
                if(i%2==0){
                    even.push_back(s[i]);
                }else{
                    odd.push_back(s[i]);
                }
            }
        }

        for(i=0; i<even.size(); i++){
            cout << even.at(i);
        }

        cout << "  ";

        for(i=0; i<odd.size(); i++){
            cout << odd.at(i);
        }

        cout << endl;

        even.clear();
        odd.clear();
        s.clear();
     }


     return 0;
}


While compiling the above code I'm getting "no matching error for call std::vector...". What exactly am I doing wrong?

Upvotes: 0

Views: 4438

Answers (2)

rekkalmd
rekkalmd

Reputation: 181

Welcome to Stack Overflow @Manish Jha,

So s[i] is a char

s is a std::string or an array of chars

The method must be push_back(std::string&) not push_back(std::char&)

The class template method std::vector::push_back(T&) is kind of "specialized" when you initialized your even/odd vector so, either append a std::string to std::vector<std::string> or a char to std::vector<char>

Otherwise, there is no maching for call the method std::vector<T>::push_back(T&)

Hope i answered correctly to your waitings, if there are any errors, please notify me.

Upvotes: 0

Cosmin Pascaru
Cosmin Pascaru

Reputation: 51

I am getting the following error when compiling your code:

main.cpp:34:36: error: no matching function for call to ‘std::vector >::push_back(char&)’.

This happens because even is a vector<string>, and s[i] is a char. You are trying to insert a char into a vector of strings, and that is not possible, as they are different types.

If I understood your problem properly, even and odd must both be either vector<char> or string, not vector<string>.

Change the declarations to:

string odd;
string even;

This also allows you to replace the printing:

for(i=0; i<even.size(); i++) {
    cout << even.at(i);
}

With:

cout << even;

Upvotes: 5

Related Questions