没有运算符 "+=" 与这些操作数匹配
No operator “+=” matches these operands
>我有一个h文件,其中有一个名为MainControl
的类,以及一个名为Vote
的结构。
在 MainControl 中,我以这种方式定义一个公共运算符(这是一个成员函数):
MainControl& operator+=(Vote& v);
在匹配的.cpp文件中,我有这个功能:
MainControl& MainControl::operator+=(Vote& v){
...
}
当我尝试在另一个文件中编写类似的东西时:
mc+=v
其中mc
是来自类 MainControl 的对象,v 是来自结构Vote
的对象。
我收到此错误:
error C2679: binary '+=': no operator found which takes a right-hand operand of type 'Vote' (or there is no acceptable conversion)
我确实包含了我所相信的正确文件,因为我有一个非常相似的运算符为我工作(而不是结构Vote
它涉及另一个类)。
我不知道是什么原因造成的,有人可以帮忙吗?
编辑:
运算符的使用方式如下:
mc += Vote(vr1, "Cyprus");
mc
来自类MainControl
的地方。
结构投票如下所示:
struct Vote
{
Voter voter;
string* voted_state;
// ALL is public here.
Vote(Voter current_voter, string state1, string state2 = "", string state3 = "", string state4 = "", string state5 = "", string state6 = "", string state7 = "", string state8 = "", string state9 = "", string state10 = "") :
voter(current_voter), voted_state(new string[VOTE_ARRAY_SIZE]){
voted_state[0] = state1;
voted_state[1] = state2;
voted_state[2] = state3;
voted_state[3] = state4;
voted_state[4] = state5;
voted_state[5] = state6;
voted_state[6] = state7;
voted_state[7] = state8;
voted_state[8] = state9;
voted_state[9] = state10;
}
~Vote() {
delete[] voted_state;
}
};
在不给我编译错误的类似操作中,运算符是这样使用的:
mc += p1
其中p1
是类名参与者中的对象,mc 是类MainControl
中的对象。
在我定义类的 .h 文件中,MainControl
我有这个标记:
MainControl& operator+=(Participant& p);
课程参与者如下所示:
class Participant
{
string state_name;
string song_name;
int time_length;
string singer_name;
bool is_registered;
public:
Participant(string state, string song, int time, string singer):
state_name(state),song_name(song),singer_name(singer),time_length(time),is_registered(false){
}
~Participant() = default;
string state() const;
string song() const;
int timeLength() const;
string singer() const;
int isRegistered() const;
void update(const string song, const int time, const string singer);
void updateRegistered(const bool status);
};
p1
是这样定义的:
Participant p1("USA", "Song_USA", 175, "Singer_USA");
您的 += 运算符采用(非常量)L 值引用
MainControl& operator+=(Vote& v);
然后,将 r 值传递给它:
mc += Vote(vr1, "Cyprus");
这不能转换为(非恒量)L 值参考
如果您(如前所述)在此操作期间需要修改"投票",您可以执行以下操作:
auto v1 = Vote(vr1, "Cyprus");
mc += v1;
这样,您可以按预期将投票传递给运营商。
但: 这不是一个好的设计,以后会咬你。
相关文章:
- 控制允许动态运行c++的并发操作数
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++ 带有向量的 IF 计算操作数
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- 与'operator='不匹配(操作数类型'String'且"void")
- 不正确的操作数类型 MSVC
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- 如果我在指针中使用 ++ 操作数,我的值就会出错
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数
- 非法,左操作数具有类型 'DWORD [29]'