Harshit Damani Harshit Damani - 1 month ago 10
C++ Question

C++: no match for call to '(concat) (std::string&)' ! Please help rectify this

So, I have been trying to write this C++ program to concatenate strings taken as input from the user using Operator Overloading on the operator(+=). And I have defined the constructor to initialize the objects as well as to take string as an argument. But it still shows the error


error: no match for call to '(concat) (std::string&)'|


Here's the code for the 3 files=>
main.cpp

#include <iostream>
#include "concat.h"
#include<string>
using namespace std;

int main()
{
concat ob[10];
concat result;
int i;
string ip;
cout<<"Enter the strings upto 10 in different lines.. \n";
for(i=0;i<(sizeof(ob)/sizeof(ob[0]));i++){
cin>>ip;
ob[i](ip);
}

while(i!=0){
result += ob[i++];
}
cout<<result.input;
}


concat.h

#ifndef CONCAT_H
#define CONCAT_H
#include<string>
using namespace std;

class concat
{
public:
string input;
concat();
concat(string ip);
concat operator+=(concat );

};

#endif // CONCAT_H


concat.cpp

#include <iostream>
#include "concat.h"
#include<string>
using namespace std;

concat::concat(){
}
concat::concat(string ip)
:input(ip)
{
}
concat concat::operator+=(concat ipObj){
this->input += ipObj.input;
return *this;
}

Answer Source

That is how your concat class prototype should look:

class concat
{
public:
    string input;
    concat() = default;
    concat& operator+=(const concat&);

    // assignment operator
    concat& operator=(const string& str) {  
        input = str;
        return *this;
    }
};

And then use ob[i] = ip; instead of ob[i](ip).

Also, your program crashes because you wrote result += ob[i++];
instead of result += ob[--i];