草庐IT

this-reference

全部标签

c++ - 为什么编译器说 : 'enable_if' cannot be used to disable this declaration

templateusingEnable_if=typenamestd::enable_if::type;classDegree;templateconstexprinlineboolIs_Degree(){returnstd::is_base_of::value;}classDegree{public:std::size_tinDeg=0;};templateclassVertex:publicSatellite{public:explicitVertex(intnum):n(num){}private:std::size_tn;};templateclassEdge{public:/

c++ - CLion "Instantiating an unknown structure without reference"但编译正常

我一直在尝试使用CLion编辑器和MinGW在Windows10(64位)上使用HDF5设置我的cmake项目。经过大量时间尝试正确设置我的CMakeLists文件后,我得到了一些工作-代码编译,mingw32-make或cmake没有错误。但是,我仍然在CLion中遇到带有红色下划线的错误,这些错误似乎对构建没有任何影响,但我感觉它们存在是因为我做错了什么。(除了使用C++进行类项目外,我还很陌生)这是我的CMakeLists.txtcmake_minimum_required(VERSION2.8)project(testProject)add_definitions(-std=c

c++ - 对非成员函数的 undefined reference - C++

我在头文件中有以下内容。namespacesilc{classpattern_token_map{/*Contents*/};pattern_token_map*load_from_file(constchar*);}在CPP文件中(这有适当的包含)pattern_token_map*load_from_file(constchar*filename){//Implementationgoeshere}在另一个CPP文件中。这已经包含了所有正确的内容。voidsome_method(){constchar*filename="sample.xml";pattern_token_map*

c++ - 练习 : pointers and references in C++

这是正确的吗:a)指向一个字符的指针p1:char*p1;b)指向char的常量指针p2:char*constp2;c)指向常量char的指针p3:constchar*p3;d)指向常量char的常量指针p4:constchar*constp4;e)对char的引用r1:char&r1;f)对常量char的引用r2:constchar&r2;如果有任何错误,请告知我好吗? 最佳答案 他们都是对的。我看不到任何错误:-) 关于c++-练习:pointersandreferencesinC+

c++ - 为什么隐含的 "lambda to function pointer conversion"禁止静态成员的 "by reference"捕获?

C++11标准说(或者至少,我拥有的版本——不是最终版本):Theclosuretypeforalambda-expressionwithnolambda-capturehasapublicnon-virtualnon-explicitconstconversionfunctiontopointertofunctionhavingthesameparameterandreturntypesastheclosuretype’sfunctioncalloperator.我理解为什么无法从有状态lambda中获取函数指针,因为函数指针本身不能保存任何数据。但是当捕获的对象只是一个静态成员/静

c++ - "Undefined reference"到定义的构造函数

我有一个构造函数,其签名是这样的:cpuInput(conststd::string&label);实际的构造函数使用一个引用超构造函数的初始化列表,所以它看起来像这样:cpuInput::cpuInput(conststring&label):StateMonitor::input(label){}该类可以很好地编译为目标文件。如果我将该文件与调用构造函数的驱动程序一起编译:cpuInput*cpu=newcpuInput();当然,我从g++得到一个错误:demo.cpp:15:31:error:nomatchingfunctionforcallto‘cpuInput::cpuIn

c++ - 有没有办法强制使用 "this->"用于 clang-format/clang-tidy 中的类成员/方法?

我到处搜索,但我可能用错了术语。我还没有为此找到选项。我唯一发现的是这个未回答的问题(但是有点宽泛):CPPlint:Canyouenforceuseofthisforclassvariables?. 最佳答案 鉴于existingoptions,我不相信这在clang-format中是可能的,future也不会。主要原因是程序的工作方式。它不会将C++代码解析为AST,而是将文本标记化而不需要包含(定义它的成员和全局变量)而不是编译数据库(影响定义、包含路径……)它是甚至可以给它一段代码并重新格式化。从问题的性质来看,如果它可以存

C++篇--类大小计算、this指针

文章目录一、类大小计算二、this指针一、类大小计算类的大小是只计算它的成员变量或者自定义成员,不会计算它的成员函数大小。#includeusingnamespacestd;classA1{public: voidfun() { _a=1; _b=2; } int_a; int_b;};intmain(){ coutsizeof(A1)endl;//8字节,为何是8字节 return0;}光是类中成员变量_a,_b所占的字节大小就为8了,但是类中还要成员函数,为何还是8字节难道成员函数没有在类之中,对的类成员函数在公共代码区的,不同对象的成员数据是私有的每个人有自己的成员数据但是成员函数

c++ - sizeof(*this) 和结构继承

假设我有一个如下所示的结构:structParentStruct{virtualvoidXYZ(){getSize(sizeof(*this));}intmemberX;}还有另一个继承父结构的struct:structChildStruct:publicParentStruct{intmemberY;intmemberZ;}假设sizeof(int)==4,是否可以将12的值传递给函数getSize()从子结构调用(我目前得到的值是4)?我宁愿不必覆盖所有子结构中的XYZ(),因为我会有很多子结构。 最佳答案 正如其他人所说,th

c++ - 错误 : passing const xxx as this argument of xxx discards qualifiers

我在将仿函数从Windows移植到Linux时遇到问题。(传递给STL::map以进行严格弱排序的仿函数)原文如下:structstringCompare{//Utilizedasafunctorforstl::mapparameterforstringsbooloperator()(stringlhs,stringrhs){//Returnstrueiflhs由于linux不支持_stricmp而是使用strcasecmp,我将其更改为:structstringCompare{booloperator()(stringlhs,stringrhs){//Returnstrueiflhs