本文介绍了为什么g ++说'没有匹配的'operator ='当有明显是,并且Visual Studio可以看到有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个接口库,允许访问 regula :: State 类型的对象中的表中的变量(理论上无限深度)。我在一个类中重载 operator [] ,然后返回另一个类,并调用 operator [] 。例如:

I'm writing an interface library that allows access to variables within tables (up to a theoretically infinite depth) in an object of type regula::State. I'm accomplishing this by overloading operator[] within a class, which then returns another of that same class, and calls operator[] again as needed. For example:

regula::State t;
t["math"]["pi"] = 3.14159;

上面的值应该是 3.14159 math 中的变量 pi 中。基本上,它通过 t 返回代表 math 的代理对象来返回代表 pi ,我们实际保存的变量。

The above is supposed to place the value 3.14159 within variable pi in table math. Basically, it does this by have t return a proxy object representing math, which returns another proxy object representing pi, to which we actually save the variable. The internals of this aren't really relevant to the question, but here is the function header.

LObject LObject::operator[] (const std::string name);

基本上,在上面的例子中,程序应该调用 t '运算符[] 与字符串math并返回另一个对象,然后调用对象的运算符[] 与返回最终对象的字符串pi,然后将值分配给一个使用 operator = 。

Basically, in the example above, the program should call t's operator[] with the string "math" and return another object, and then call that object's operator[] with the string "pi", which returns the final object, and then assigns the value to that one using operator=.

template <typename T>
T LObject::operator= (const T& value);

返回的 T value 传递。

现在,我的代码在Visual C ++ 2008中没有产生错误,但是当我尝试在Linux上使用 g ++ 编译它时,会出现以下错误:

Now, my code produces NO errors in Visual C++ 2008 and works perfectly. But when I try to compile it on Linux with g++, I get the following error:

../../test/regula-test.cpp:100: error: no match for ‘operator=’ in 
‘L.regula::State::operator[](std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >(((const char*)"Numbers"), ((const std::allocator<char>&)((const 
std::allocator<char>*)(& std::allocator<char>()))))) = Numbers’
../../include/regula.hpp:855: note: candidates are: regula::LObject&
 regula::LObject::operator=(const regula::LObject&)

某些原因, g ++ 似乎试图在上调用 operator = code>,而不是在返回的对象,因为它应该是。

For some reason, g++ seems to be trying to call operator= on operator[], rather than on the returned object like it is supposed to be.

我可以修复这个错误,替换 operator = 与 void :

I can actually fix this error by replacing the return type on operator= with void:

template <typename T>
/*T*/ void LObject::operator= (const T& value);

但这不是首选,此外,我在其他几个位置有类似的重载类似的错误 operator == :

But this is not preferable, and besides, I have similar errors in several other locations with a similarly overloaded operator==:

../../test/regula-test.cpp:153: error: no match for ‘operator==’ in ‘pi == 
L.regula::State::operator[](std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >(((const char*)"pi"), ((const std::allocator<char>&)((const 
std::allocator<char>*)(& std::allocator<char>())))))’


$ b b

我不明白为什么这个错误发生在g ++,或为什么它不会发生在Visual C ++。

I don't understand why this error is occurring in g++, or why it is not occurring in Visual C++. Can anyone shed any light on this or recommend any solutions?

推荐答案

ISO标准的第5.17条规定

Section 5.17 of the ISO standard says

您的 operator = 不仅返回错误的类型,而且甚至不返回左值。假设GCC的错误消息不包括除了 operator =(const regula :: LObject&)之外的任何其他候选,GCC完全忽略了你的重载。 operator = 它提到是默认的自动生成的函数。

Your operator= returns not only the wrong type, but not even an lvalue. Assuming GCC's error message didn't include any other candidates besides operator=(const regula::LObject&), GCC has simply ignored your overload entirely. The operator= it mentions is the default, automatically generated function.

第一眼看到你的 operator [] 也应该返回一个引用。

On second glance, your operator[] also should return a reference. As written, no assignment expressions like your example should work at all.

所以,你应该有函数

LObject &LObject::operator[] (const std::string name);

template <typename T>
LObject &LObject::operator= (const T& value);

这篇关于为什么g ++说'没有匹配的'operator ='当有明显是,并且Visual Studio可以看到有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:01