草庐IT

propagate_const

全部标签

c++ - 错误 : cannot convert 'std::basic_string<char>::iterator ...' to 'const char* for argument ' 1' ...'

我收到以下错误:error:cannotconvert'std::basic_string::iterator{aka__gnu_cxx::__normal_iterator>}'to'constchar*'forargument'1'to'intremove(constchar*)'由于某种原因,当我在Mac上工作时,我的程序可以完美编译......但是一旦我使用Linux机器,这个错误就会在多个地方弹出。这是弹出错误的实例之一:SomeClass::SomeClass(stringt,stringart,Timedur){charchars[]=",";t.erase(std::r

c++ - 错误 : invalid initialization of reference of type 'int&' from expression of type 'const int'

这是一个与thisquestion中的代码无关的问题。,关于以下模板函数。templateclassObject:publicContainer{public:T&object;Object(constT&obj):object(obj){}};这是调用构造函数的代码:templatevoidArray::add_element(constT&element){vec.push_back(newObject(element));}这段代码编译得很好,但是只要我在main中添加一行调用它:Arrayarray;inti=3;array.add_element(i);我收到编译器警告:er

c++ - 错误 : invalid initialization of reference of type 'int&' from expression of type 'const int'

这是一个与thisquestion中的代码无关的问题。,关于以下模板函数。templateclassObject:publicContainer{public:T&object;Object(constT&obj):object(obj){}};这是调用构造函数的代码:templatevoidArray::add_element(constT&element){vec.push_back(newObject(element));}这段代码编译得很好,但是只要我在main中添加一行调用它:Arrayarray;inti=3;array.add_element(i);我收到编译器警告:er

c++ - 如何将 const char* 存储到 char*?

我有thiscode按预期工作:#defineMAX_PARAM_NAME_LEN32constchar*GetName(){return"Testtext";}intmain(){charname[MAX_PARAM_NAME_LEN];strcpy(name,GetName());cout如果我想将结果存储到char*(因为我使用的框架中的某些函数仅使用char*作为输入)而不使用strcpy(为了代码的实用性和可读性,以及学习),我该怎么办?保持在const中,效果很好:constchar*name;name=GetName();但我还有const。尝试只使用char*:char

c++ - 如何将 const char* 存储到 char*?

我有thiscode按预期工作:#defineMAX_PARAM_NAME_LEN32constchar*GetName(){return"Testtext";}intmain(){charname[MAX_PARAM_NAME_LEN];strcpy(name,GetName());cout如果我想将结果存储到char*(因为我使用的框架中的某些函数仅使用char*作为输入)而不使用strcpy(为了代码的实用性和可读性,以及学习),我该怎么办?保持在const中,效果很好:constchar*name;name=GetName();但我还有const。尝试只使用char*:char

c++ - 当我使用 [=] 捕获(使用 Lambda 类)时,为什么变量会变为 'const'?

前提#1:我已经解决了错误,但是我没有深入理解编译器错误的原因。前提#2:这个程序的目标是通过多线程进程将一个图像复制到另一个图像中。也许存在更好的方法,但这不是问题的重点主题(参见前提#1)。我使用OpenCV3.1库编写了一个简单的程序来将图像复制到另一个图像中。它利用CPU的所有内核使用更多线程。代码是:#include#include#include#include#includeusingnamespacecv;usingnamespacestd;#defineIMG_PATH"..\\img\\50a.png"voidcopy_image(constMat&input,Ma

c++ - 当我使用 [=] 捕获(使用 Lambda 类)时,为什么变量会变为 'const'?

前提#1:我已经解决了错误,但是我没有深入理解编译器错误的原因。前提#2:这个程序的目标是通过多线程进程将一个图像复制到另一个图像中。也许存在更好的方法,但这不是问题的重点主题(参见前提#1)。我使用OpenCV3.1库编写了一个简单的程序来将图像复制到另一个图像中。它利用CPU的所有内核使用更多线程。代码是:#include#include#include#include#includeusingnamespacecv;usingnamespacestd;#defineIMG_PATH"..\\img\\50a.png"voidcopy_image(constMat&input,Ma

c++ - 对非 const 对象的 const 引用

在下文中,是否会在将const引用用于非const对象之前创建一个临时对象?constinty=2000;constint&s=y//ok,constreferencetoconstobject.intx=1000;constint&r=x;//anytemporarycopyhere?如果没有,那它是如何工作的?constintz=3000;int&t=z//ok,whycan'tyoudothis? 最佳答案 没有。引用只是现有对象的别名。const由编译器强制执行;它只是检查您没有尝试通过引用r修改对象。*这不需要创建拷贝。鉴

c++ - 对非 const 对象的 const 引用

在下文中,是否会在将const引用用于非const对象之前创建一个临时对象?constinty=2000;constint&s=y//ok,constreferencetoconstobject.intx=1000;constint&r=x;//anytemporarycopyhere?如果没有,那它是如何工作的?constintz=3000;int&t=z//ok,whycan'tyoudothis? 最佳答案 没有。引用只是现有对象的别名。const由编译器强制执行;它只是检查您没有尝试通过引用r修改对象。*这不需要创建拷贝。鉴

c++ - 在头文件中执行 const std::string 的正确方法?

我正在编写一个Cocos2D-X游戏,其中玩家、敌人和其他角色将他们的属性存储在CCMutableDictionary中。,它有点像std::map的装饰器类.可以通过CCMutableDictionary::objectForKey(conststd::string&key)访问字典中的值。方法。现在,在我的许多.cpp文件包含的头文件中,我有几个constchar*const用于访问字典中的值的字符串,如下所示://inConstants.hconstchar*constkAttributeX="x";constchar*constkAttributeY="y";//ina.cpp