C++: no match for call to '(concat) (std::string&)' -
i have been trying write c++ program concatenate strings taken input user using operator overloading on operator(+=). , have defined constructor initialize objects take string argument. still shows error
error: no match call '(concat) (std::string&)'|
here's code 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 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; }
that how 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 use ob[i] = ip;
instead of ob[i](ip)
.
also, program crashes because wrote result += ob[i++];
instead of result += ob[--i];
Comments
Post a Comment