草庐IT

cset_members

全部标签

c++ - "operator = must be a non-static member"是什么意思?

我正在创建一个双链表,并重载了operator=以使列表等于另一个:templatevoidoperator=(constlist&lst){clear();copy(lst);return;}但是当我尝试编译时出现此错误:container_def.h(74):errorC2801:'operator='mustbeanon-staticmember另外,如果有帮助,第74行是定义的最后一行,带有“}”。 最佳答案 正如它所说:运算符重载必须是成员函数。(在类中声明)templatevoidlist::operator=(cons

c++ - 我在哪里将常量字符串放在 C++ : static class members or anonymous namespaces? 中

我需要定义一些仅由一个类使用的常量字符串。看起来我有三个选择:将字符串直接嵌入到使用它们的位置。将它们定义为类的私有(private)静态常量成员://A.hclassA{private:staticconststd::stringf1;staticconststd::stringf2;staticconststd::stringf3;};//A.cppconststd::stringf1="filename1";conststd::stringf2="filename2";conststd::stringf3="filename3";//stringsareusedinthisfil

c++ - 警告 : ISO C++ forbids converting a string constant to ‘char*’ for a static `constexpr char*` data member

这个问题在这里已经有了答案:constexprconstvsconstexprvariables?(3个回答)关闭3年前。为什么这段代码会返回警告warning:ISOC++forbidsconvertingastringconstantto‘char*’[-Wwrite-strings]如果Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.Aconstexprspecifierusedinafunctionorstaticmembervariab

c++ - 错误 : member access into incomplete type : forward declaration of

我在同一个.cpp文件中有两个类://forwardclassB;classA {voiddoSomething(B*b){b->add();}};classB{voidadd(){...}};转发不起作用,我无法编译。我得到这个错误:error:memberaccessintoincompletetype'B'note:forwarddeclarationof'B'我正在使用clang编译器(clang-500.2.79)。我不想使用多个文件(.cpp和.hh),我想只在一个.cpp上编写代码。我不能在A类之前写B类。您知道如何解决我的问题吗? 最佳答案

c++ - 错误 : ‘unique_ptr’ is not a member of ‘std’

我想这是不言自明的-我似乎无法使用C++11功能,即使我认为我已经正确设置了所有内容-这可能意味着我没有。这是我的代码:#include#includeclassObject{private:intvalue;public:Object(intval){value=val;}intget_val(){returnvalue;}voidset_val(intval){value=val;}};intmain(){Object*obj=newObject(3);std::unique_ptrsmart_obj(newObject(5));std::coutget_val()这是我的g++版

c++ - "to_string"是 't a member of "标准”?

好的,我有tmp.cpp:#includeintmain(){std::to_string(0);return0;}但是当我尝试编译时,我得到:$g++tmp.cpp-otmptmp.cpp:Infunction‘intmain()’:tmp.cpp:5:5:error:‘to_string’isnotamemberof‘std’std::to_string(0);^我正在运行g++版本4.8.1。与我在那里发现的所有其他对这个错误的引用不同,我没有使用MinGW,我使用的是Linux(3.11.2)。任何想法为什么会发生这种情况?这是标准行为,我做错了什么还是某处有错误?

c++ - 运算符重载 : member function vs. 非成员函数?

我读到声明为成员函数的重载运算符是不对称,因为它只能有一个参数,而自动传递的另一个参数是this指针。所以没有标准来比较它们。另一方面,声明为friend的重载运算符是symmetric,因为我们传递了两个相同类型的参数,因此可以比较它们。我的问题是,当我仍然可以将指针的左值与引用进行比较时,为什么首选friend?(使用非对称版本的结果与对称版本相同)为什么STL算法只使用对称版本? 最佳答案 如果您将运算符重载函数定义为成员函数,则编译器会将s1+s2等表达式转换为s1.operator+(s2)。这意味着,运算符重载的成员函数

ruby-on-rails - Range.include 之间的区别?和 Range.member?在 ruby

Ruby有一个非常有用的Range类来表示字母和数字范围。这个类有两个方法我似乎无法区分:Range.include?和Range.member?Ruby-doc.org对两者给出完全相同的描述。它们之间有什么区别,如果有的话? 最佳答案 他们是彼此的别名。如果您展开文档中的源代码,您会发现它们都引用相同的内部函数。 关于ruby-on-rails-Range.include之间的区别?和Range.member?在ruby,我们在StackOverflow上找到一个类似的问题:

ruby-on-rails - rails : why am I having trouble accessing this member variable in a controller method?

我在一个简单的Controller方法中发现了一个非常奇怪的问题。要么我遗漏了一些基本的东西,要么我遇到了一个错误。我打赌是前者。我有一个带有ThingController的Thing模型。Thing有两个变量,name和display,都是字符串。ThingController(下面的代码)有一个方法toggle_display,它在“on”之间切换display的内容>和“关闭”。问题是当我调用这个Action时,Rails找到了正确的Thing,但是@thing.display是nil。当我检查数据库时,“显示”列中的值是正确的。奇怪的是,当我取消注释下面代码中的第三行时(即访问

c++ - "error : a nonstatic member reference must be relative to a specific object"是什么意思?

intCPMSifDlg::EncodeAndSend(char*firstName,char*lastName,char*roomNumber,char*userId,char*userFirstName,char*userLastName){...return1;}extern"C"{__declspec(dllexport)intstart(char*firstName,char*lastName,char*roomNumber,char*userId,char*userFirstName,char*userLastName){returnCPMSifDlg::EncodeAnd