草庐IT

conversion-operator

全部标签

c++ - 错误 : invalid conversion from 'void (*)()' to 'void (*)()' -- what?

我正在尝试将回调函数从C++传递到OpenGL(CAPI):gluQuadricCallback(qobj,GLU_ERROR,errorCallback);其中errorCallback是编译为C++代码的文件中的函数,声明为voiderrorCallback();代码在Linux上使用g++4.4编译干净,但在Windows上使用mingw32g++4.4时出现以下错误:..\glwidget.cpp:172:error:invalidconversionfrom'void(*)()'to'void(*)()'..\glwidget.cpp:172:error:initializi

c++ - istringstream operator>> 返回值如何工作?

此示例读取包含一个整数、一个运算符和另一个整数的行。例如,25*34/2//sstream-line-input.cpp-Exampleofinputstringstream.//Thisacceptsonlylineswithanint,achar,andanint.//FredSwartz11Aug2003#include#include#includeusingnamespacestd;//================================================================mainintmain(){strings;//Wheretos

c++ - 不匹配 C++ 中的 operator+ 错误

这是我一直在研究的Rational类:理性.h#includeusingnamespacestd;#ifndefRATIONAL_H#defineRATIONAL_HclassRational{intnumerator,denominator;public://thevariousconstructorsRational();Rational(int);Rational(int,int);//memberfunctionsintget_numerator()const{returnnumerator;}intget_denominator()const{returndenominato

c++ - 错误 C2678 : binary '=' : no operator found which takes a left-hand operand of type 'const Recipe' (or there is no acceptable conversion)

我正在尝试对每个元素中包含一个int和一个字符串的vector进行排序。它是一个称为vector食谱的类类型的vector。出现上述错误,这是我的代码:在我的Recipe.h文件中structRecipe{public:stringget_cname()const{returnchef_name;}private:intrecipe_id;stringchef_name;在我的Menu.cpp文件中voidMenu::show()const{sort(recipes.begin(),recipes.end(),Sort_by_cname());}在我的Menu.h文件中#include

c++ - 使用 operator== 时 std::set 中 unique_ptr 的深度比较

我正在尝试使用std::set将一组unique_ptr保存到我定义的自定义对象中。我在定义集合时提供了一个自定义比较函数(以启用深度比较)。在将元素插入集合时,此比较功能似乎可以正常工作,即具有相同内容的项目不会被插入两次。但是,如果我使用operator==比较两个集合,它似乎会被忽略,即具有等效元素的集合返回为不相等,而我期望(希望)它相等(因为我提供的自定义比较功能会进行深度比较)。compare函数是否只在插入时使用?如果是这样,是否有替代方法让operator==进行深度比较?感谢任何指点。谢谢:)示例代码////main.cpp//Test#include#include

c++ - MS VC++ 上直接和复制初始化的不同行为(使用用户定义的转换运算符)

以下代码compilesfine同时使用g++9.1和clang8.0.0(编译标志为-std=c++17-Wall-Wextra-Werror-pedantic-errors),但不适用于MSVC19.22(编译标志为/std:c++17/permissive-):structX{};structBar{Bar()=default;Bar(X){}};structFoo{operatorX()const{returnX{};}operatorBar()const{returnBar{};}};intmain(){Foofoo;[[maybe_unused]]Barb1=foo;//O

c++ - 如何将重载函数传递给运算符(operator)?

我需要将函数传递给运算符(operator)。具有正确arg类型的任何一元函数。返回类型可以是任何东西。因为这是库代码,所以我无法将其包装或将f强制转换为特定重载(在operator*之外)。函数将operator*第一个参数作为它自己的参数。下面的人工示例编译并返回正确的结果。但是它有硬编码的int返回类型——使这个例子可以编译。#include#includeusingnamespacestd;templateintoperator*(Tx,int&(*f)(T&)){return(*f)(x);};intmain(){tupletpl(42,43);cout;}是否可以让oper

c++ - operator<<(ostream& os, ...) 用于模板类

为什么我不能对采用模板参数的友元函数使用相同的模板参数?我的意思是下面的代码没问题!templateclassEdge{templatefriendostream&operator&e);///...};templateostream&operator&e){returnos"但是这个不行。为什么?问题是什么?(我收到链接器错误。)templateclassEdge{friendostream&operator&e);///...};templateostream&operator&e){returnos" 最佳答案 您可以使用以下

c++ - 使用哪个 : move assignment operator vs copy assignment operator

我似乎不明白为什么要使用移动赋值运算符:CLASSA&operator=(CLASSA&&other);//moveassignmentoperator结束了,复制赋值运算符:CLASSA&operator=(CLASSAother);//copyassignmentoperator移动赋值运算符仅采用r值引用,例如CLASSAa1,a2,a3;a1=a2+a3;在复制赋值运算符中,other可以是使用复制构造函数或移动构造函数的构造函数(如果other是用右值初始化的,它可以是移动构造的——如果move-constructor定义了——)。如果它是copy-constructed,我

c++ - 我应该使用 operator+= 而不是 operator+ 来连接 std::string 吗?

我经常看到这个结构是有原因的吗:std::stringmyString=someString+"text"+otherString+"moretext";...而不是这个(我很少看到):std::stringmyString;myString+=someString+="text"+=otherString+="moretext";阅读std::stringAPI,在我看来operator+创建了很多临时对象(可能被编译器RVO优化掉了?),而operator+=变体仅append文本。在某些情况下,operator+变体将是可行的方法。但是,当您只需要将文本append到现有的非常量