C++:没有匹配调用'(concat) (std::string&)'
C++: no match for call to '(concat) (std::string&)'
我一直在尝试编写这个C++程序,以使用运算符(+=(上的运算符重载连接作为用户输入的字符串。我已经定义了构造函数来初始化对象以及将字符串作为参数。但它仍然显示错误
错误:对"(连接((标准::字符串&("的调用不匹配|
这是 3 个文件的代码 =>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;
}
康卡特·
#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
康卡特.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;
}
这就是你的concat
类原型应该是什么样子的:
class concat
{
public:
string input;
concat() = default;
concat& operator+=(const concat&);
// assignment operator
concat& operator=(const string& str) {
input = str;
return *this;
}
};
然后使用ob[i] = ip;
而不是ob[i](ip)
.
此外,您的程序崩溃,因为您编写了result += ob[i++];
而不是result += ob[--i];
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- 使用 std::string () const 函数启动线程或未来
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如何更改大小(std::string)
- std::string 的对象真的可以移动吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- 真的没有来自 std::string_view 的 std::string 的显式构造函数吗?
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 重载 + 自己的类和 std::string 的运算符
- 如何使用 std::string 作为 QHash 的键?
- 将日语 wstring 转换为 std::string
- 可以从std::string继承以提供类型一致性吗
- 构造函数采用std::string_view与std::string并移动
- 在共享缓冲区内存中创建 ::std::string 对象
- std::string.size() 未知行为
- Valgrind 在 std::string::swap 中报告 SIGILL