错误C2678:二进制'+':未找到采用'volatile A'类型的左操作数的运算符(或没有可接受的转换) [英] error C2678: binary '+': no operator found which takes a left-hand operand of type 'volatile A' (or there is no acceptable conversion)

查看:81
本文介绍了错误C2678:二进制'+':未找到采用'volatile A'类型的左操作数的运算符(或没有可接受的转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前用C ++编程,但是已经有好几年了。我是C ++ 11的新手,并且在以下方面遇到了麻烦。

I've programmed in C++ before, but it's been several years. I'm new to C++11 and I'm having trouble with the following.

我的类比只存储构造函数中给定值的两倍更为复杂。 ,但为简单起见,此示例仅将构造函数输入乘以2。该类必须隐式转换为&来自 int ,因此 operator int() operator =(const int),以及采用 int 的构造函数。

My class is more complicated than just storing "twice the value given in the constructor", but for the sake of simplicity this example just multiplies the constructor input by 2. The class must implicitly convert to & from an int, thus the operator int(), operator=(const int), and constructor that takes an int.

一切正常,除非我的实例类定义为 volatile 。当我随后尝试对易失性实例进行操作时,Visual Studio会抱怨:

Everything works fine unless an instance of my class is defined as volatile. When I subsequently try to do operations with the volatile instance, Visual Studio complains:

#include <iostream

class A
{
private:
   int _i;
public:
   A() = default;
   constexpr A(const int i) : _i(i*2) {}
   constexpr operator int() const { return _i/2; }
   A& operator=(const int i) { _i = i*2; return *this; }
};

//A va; // <---- this works (though I need it to be 'volatile')
volatile A va; // <--- this gives error C2678: binary '+': no operator found which takes a left-hand operand of type 'volatile A' (or there is no acceptable conversion)

int main()
{
   int j;
   j = va + 12; // <--- Here's where the error occurs
   std::cout << "j = " << j << std::endl;
}

看到错误提示没有可接受的转换,我尝试添加到该类的副本构造函数需要 volatile ,但这并不能解决问题:

Seeing that the error suggests that there's "no acceptable conversion", I tried adding to the class a copy constructor that takes a volatile other, but that didn't fix the problem:

   constexpr A(volatile const A& other) : _i(other._i) {}

我可以通过抛弃 volatile ...

I can "fix" it by casting away the volatile...

   j = (A)va + 12;

...但是该解决方案对我不起作用,因为我的课程实际上是模拟器的一部分试图模拟嵌入式硬件并在模拟环境中运行嵌入式代码的环境。我的课必须充当硬件寄存器的替代者,我不能(或不想)在语句<$ c $中丢弃 volatile c> j = va + 12; ,因为该行是嵌入式固件本身的一部分。是否可以将某些转换运算符或方法添加到我的类A 中,以使语句 j = va + 12;

...but that solution doesn't work for me because my class is actually part of a simulator environment that's trying to simulate embedded hardware and run the embedded code within the simulation environment. My class has to act as a stand-in for hardware registers, and I can't (or don't want to) cast away volatile in the statement j = va + 12; because that line is part of the embedded firmware itself. Is there some conversion operator or method I can add to my class A to make the statement j = va + 12; work without modification?

推荐答案

您要在 volatile 中添加错误的地方。

You're adding the volatile in the wrong place.

为了在 j = va + 12 中执行加法, va 需要转换为 int ,因为在其中没有定义 operator + A 。现有的转换运算符标记为 const ,编译器将不会使用该运算符转换 volatile 对象。

In order to perform the addition in j = va + 12, va needs to be converted to an int since there isn't an operator+ defined within A. The existing conversion operator is marked const, which the compiler will not use to convert a volatile object.

解决方案是向您的类中添加一个附加的 operator int ,以支持易失性对象的转换:

The solution is to add an additional operator int to your class that supports conversion of volatile objects:

constexpr operator int() volatile { return _i/2; }

您既需要现有的 const 一个和另一个。

You'll need both the existing const one and this one.

这篇关于错误C2678:二进制'+':未找到采用'volatile A'类型的左操作数的运算符(或没有可接受的转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆