草庐IT

Reference_wrapper

全部标签

c++ - 使用 bind2nd() : "member function already defined or declared" instead of "reference to reference" 的奇怪编译器错误

我最近花了很多时间来理解在这段代码中调用func()时的错误消息:intmain(){vector>v;doublesum=0;for_each(v.begin(),v.end(),bind2nd(ptr_fun(func),&sum));return0;}当func()像这样声明时,代码编译正常:voidfunc(vectorv,double*sum){}当我使用这个声明(为了提高效率)时,我得到了一个编译器错误:voidfunc(constvector&v,double*sum){}我期望看到的错误类似于reference-to-reference错误,因为binder2nd的op

c++ - add_lvalue_reference 有什么作用?

我有这样一个类,它为montecarlo模拟器创建路径,它需要从可用整数数组创建整数路径。因此,例如,我们可以从包含{0,1,2,3,4}的数组中提取长度为3的路径,这将生成3,1,2和1,4,0​​。//ThispathgeneratorjustgeneratesalistofintsforeachpathtemplateclassMCPathGen{public:typedefvectormcpath_t;typedefrandgenrandgen_t;typedeftypenameadd_lvalue_reference::typerandgen_ref_t;MCPathGend

c++ - Clang claims that `member reference base type ' X' is not a structure or union`,但 X 是具有推导参数的结构模板

考虑以下代码:templatestructX{X(T){}voidfoo(){}};templatestructY{intobject=0;voidbar(){X(object).foo();}};Liveongcc.godbold.orgGCC8.2编译它,而Clang7吐出以下错误::13:18:error:memberreferencebasetype'X'isnotastructureorunionX(object).foo();~~~~~~~~~^~~~这对我来说像是一个错误。条件非常具体:如果任一结构不是模板,或者object不是成员变量,或者不涉及CTAD(类模板参数推导

c++ - 严格指针别名 : is access through a 'volatile' pointer/reference a solution?

在aspecificproblem之后,一个self回答和评论,我想了解它是否是一个合适的解决方案、变通方法/破解或完全错误。具体来说,我重写了代码:Tx=...;if(*reinterpret_cast(&x)==0)...作为:Tx=...;if(*reinterpret_cast(&x)==0)...带有指向指针的volatile限定符。让我们假设在我的情况下将T视为int是有意义的。这种通过volatile引用访问是否解决了指针别名问题?作为引用,来自规范:[Note:volatileisahinttotheimplementationtoavoidaggressiveopti

c++ - 隐式转换 : const reference vs non-const reference vs non-reference

考虑这段代码,structA{};structB{B(constA&){}};voidf(B){cout这compilesfine,运行良好。但是,如果我将f(B)更改为f(B&),它doesn'tcompile.如果我写f(constB&),它又是compilesfine,运行良好。原因和道理是什么?总结:voidf(B);//okayvoidf(B&);//errorvoidf(constB&);//okay对于每种情况,我想听听语言规范中的原因、基本原理和引用资料。当然,函数签名本身并没有错。A隐式转换为B和constB&,但不会转换为B&,这会导致编译错误。

c++ - 用 C++ 编译的 C 代码 : undefined reference

我有一个小程序,可以毫无困难地使用GCC和ICC进行编译,但我也希望该代码可以使用G++和ICPC。我试着添加这个:#ifdef__cplusplusextern"C"{#endif开头和这个:#ifdef__cplusplus}#endif在所有头文件的末尾,但我仍然遇到几个“未定义对“...”的引用”错误。 最佳答案 我认为你弄错了...externC用于禁用函数重整;因此,如果您仅针对头文件执行此操作,当您尝试链接损坏的目标代码时,声明的函数名称将与目标文件中的函数名称不匹配。无论如何,如果整个应用程序正在使用相同的C++编译

c++ - 实现观察者模式时出现问题 : "Member reference base type ________ is not a structure or union"

我一直在实现准系统观察者模式,但遇到了一个有点神秘的错误:“成员引用基类型‘Observer*’不是结构或union”。我认为这与我对模板的使用有关,我对模板的使用仍然相当不舒服。这是有问题的代码(为了简化事情而删除了大多数缺点/析构函数):主题界面:classSubject{public:virtualvoidnotify();private:listm_observers;};主题实现:voidSubject::notify(){list::iteratori;for(i=m_observers.begin();i!=m_observers.end();i++){*i->updat

c++ - Eclipse 和 Gtkmm - "undefined reference to"

我使用Eclipse,我想在其中使用gtkmm。我有以下代码:#include#includeintmain(intargc,char*argv[]){Gtk::Mainkit(argc,argv);Gtk::WindowmainWindow;Gtk::Buttonbutton("Clickhere");mainWindow.set_title("Eclipse/GTKmmDemo");mainWindow.set_border_width(4);mainWindow.set_default_size(200,50);mainWindow.add(button);button.show

c++ - openCV 错误 : undefined reference to `cvLoadImage' Ubuntu

我已经安装了openCV库,但我仍然收到错误$g++-I/usr/include/opencv/-L-lcxcore-lhighguihello.cpp-o你好/tmp/ccjjrbXr.o:在函数main'中:hello.cpp:(.text+0x2d):undefinedreferencecvLoadImage'collect2:ld返回1退出状态当我检查我得到的库的路径时$pkg-config--libsopencv-lml-lcvaux-lhighgui-lcv-lcxcore我写了一个非常简单的程序来测试它:entercodehere#include#include/*req

c++ - Qt "signal undefined reference error"继承自QObject后

我最近需要给一个类添加一个信号,所以我把类改成继承自QObject,并在类定义中添加了Q_OBJECT宏。自从这样做以来,我在下面的类行上得到了“信号undefinedreference错误为'vtableforCLICommand'”错误://Fileclicommand.h#include#include#include#include"telnetthread.h"classCLICommand:publicQObject{Q_OBJECTpublic:CLICommand(TelnetThread*parentTelnetThread);signals:voidsignal_s