草庐IT

language-reference

全部标签

c++ - 绑定(bind)错误 "The C++ Programming Language [4th Edition] - Bjarne Stroustrup"

我在BjarneStroustrup的书中找到了这段代码:这段代码的问题是变量i没有停留在2,而是递增到3。你可以在这里查看:https://wandbox.org/permlink/p5JC1nOA4pIpsgXb我们不必使用std::ref()来增加这个变量。这是书中的错误还是自C++11以来发生了一些变化? 最佳答案 示例不正确,bind确实会复制其参数,除非您将其包装在std::reference_wrapper中正如文字正确所说,但这不是示例显示的内容。在示例中,参数i传递给bind返回的仿函数,而不是bind本身。如果示

c++ - "static const int"导致链接错误( undefined reference )

使用以下代码时,我对链接器错误感到困惑://static_const.cpp--completecode#includestructElem{staticconstintvalue=0;};intmain(intargc,char*argv[]){std::vectorv(1);std::vector::iteratorit;it=v.begin();returnit->value;}但是,这在链接时会失败——不知何故,它需要一个符号来表示静态const“值”。$g++static_const.cpp/tmp/ccZTyfe7.o:Infunction`main':static_con

c++ - C++ 类 : undefined reference 中的静态常量

我有一个仅供本地使用的类(即,它的拷贝只是它定义的c++文件)classA{public:staticconstintMY_CONST=5;};voidfun(intb){intj=A::MY_CONST;//noproblemintk=std::min(A::MY_CONST,b);//linkerror://undefinedreferenceto`A::MY_CONST`}所有代码都位于同一个c++文件中。在windows上使用VS编译时,完全没有问题。但是,在Linux上编译时,我只收到第二条语句的undefinedreference错误。有什么建议吗?

c++ - 如何在 C++ 中使用 "add reference"

我是C++新手,有些东西我完全不明白。在C#中,如果我想使用外部库,例如log4net,我只需添加对log4netDLL的引用,并且它的成员对我自动可用(并且在IntelliSense中)。我如何在非托管C++中做到这一点? 最佳答案 除了.dll之外,该库通常还附带1)一个头文件(.h)和2)一个.lib文件。头文件在您的代码中被#include'ed,以便您访问库中的类型和函数声明。.lib链接到您的应用程序(项目属性->链接器->输入,附加依赖项)。.lib文件通常包含自动加载dll并将函数调用转发给它的简单stub。如果您没

c++ - const-reference 限定成员函数

引用限定成员函数的股票示例似乎是这样的:#include#include#include//Easyaccesstoliteralsusingnamespacestd::literals;//FilewrapperclassFile{private://ThewrappedfileFILE*_file;public:File(constchar*name):_file(fopen(name,"r")){//unabletoopenthefile?if(!_file)throwstd::runtime_error{"Unabletoopenfile:"s+name};}~File(){f

c++ - 首先是 std::remove_reference 还是 std::remove_cv?

如果我想提取const引用的类型(如constdouble&中的double),我必须使用:typenamestd::remove_cv::type>::type或typenamestd::remove_reference::type>::type? 最佳答案 首先使用remove_reference。remove_cv仅删除顶级限定符,在引用的情况下,没有任何(或被忽略)。显示差异的示例:#include#includetemplateusingRemove_cv_ref=std::remove_cv::type>;templat

c++ - 优化器 : replace const reference with const object

ChandlerCarruth在他的talk关于编译器优化说编译器在优化具有通过引用传递的参数的函数方面很糟糕。我可以理解当参数是非常量引用时很困难,因为编译器必须处理内存,或者当参数的类型很复杂(一些奇怪的结构或类)时。但是如果参数是const引用和内置类型,真的有什么问题吗?优化器可以用constfloat替换constfloat&吗?当启用SSE指令时,它会更有帮助,因为编译器将能够为它们正确对齐数据。 最佳答案 Canoptimizerreplaceconstfloat&withconstfloat?不,他们不能这样做,因为

c++ - 链接错误 : undefined reference to `vtable for XXX`

这里有一些链接错误。我在网上找了,还是没找到问题。我该如何解决?g++test.cpp-otest/tmp/ccDfCj4N.o:Infunction`Interval::Interval()':test.cpp:(.text._ZN8IntervalC2Ev[Interval::Interval()]+0x9):undefinedreferenceto`vtableforInterval'/tmp/ccDfCj4N.o:Infunction`IntInterval::~IntInterval()':test.cpp:(.text._ZN11IntIntervalD0Ev[IntInt

c++ - 对 google::protobuf::internal::empty_string_[abi:cxx11] 的 undefined reference

我正在尝试使用ProtocolBuffers2.6.1和GNUGCC5.1.0(在Ubuntu14.10上)构建简单的测试应用程序,但出现以下错误:/home/ragnar/cpp-tools/gcc-linux/bin/g++-c"/home/ragnar/cpp-projects/gprotobuf_test/main.cpp"-g-O0-Wall-o./Debug/main.cpp.o-I.-I/home/ragnar/cpp-tools/libs/linux64/protobuf/include-I./home/ragnar/cpp-tools/gcc-linux/bin/g+

C++函数参数: use a reference or a pointer (and then dereference)?

我得到了一些代码,其中一些参数是指针,然后指针被取消引用提供值(value)。我担心指针取消引用会花费周期,但是在查看之后上一篇StackOverflow文章:Howexpensiveisittodereferenceapointer?,也许没关系。这里有一些例子:boolMyFunc1(int*val1,int*val2){*val1=5;*val2=10;returntrue;}boolMyFunc2(int&val1,int&val2){val1=5;val2=10;returntrue;}就风格而言,我个人更喜欢通过引用传递,但有一个版本更好(在流程周期方面)比另一个?