草庐IT

implicit-conversion

全部标签

c++ - 如果加法表达式的第一个操作数可转换为指针和整数,选择哪种转换?

在下面的例子中,应该调用哪个转换函数?为什么要选择一个而不是另一个?structA{operatorint();operatorint*();};Ax;inti=x+1;编译器选择operatorint()..但为什么呢?以下是C++03中的一些相关引述:来自[expr.add]Foraddition,eitherbothoperandsshallhavearithmeticorenumerationtype,oroneoperandshallbeapointertoacompletelydefinedobjecttypeandtheothershallhaveintegraloren

c++ - 为什么 implicit == on map<<int,MyClass> 不编译?

我在为我的一个类(class)定义==时遇到了一个奇怪的问题。我将此处的代码简化为我在visual2013上测试过的示例;MyClass在命名空间N中定义这确实编译:N::MyClassa,b;booltest=a==b;这也是:constN::MyClassa,b;booltest=a==b;这不编译std::mapa,b;booltest=a==b;供您引用,=​​=运算符声明如下:booloperator==(constN::MyClass&a,constN::MyClass&b);这是我得到的错误:errorC2678:binary'==':nooperatorfoundwhi

c++ - 错误 : definition of implicitly declared copy constructor

我目前正在处理的QtC++项目有问题。这是我要介绍的一个新部分,但我发现它有点令人困惑。我创建了一些由股票、债券和储蓄类继承的Assets类。这一切都很好。然后我创建了一个名为AssetList的类,它派生了QList,这个类是我发现问题的地方。这是我目前的代码。资源列表.h#ifndefASSET_LIST_H#defineASSET_LIST_H#include"Asset.h"#includeclassAssetList:publicQList{public:AssetList(){}~AssetList();booladdAsset(Asset*);Asset*findAsse

c++ - C++中的整数提升和整数转换有什么区别

C++标准的第4.5节(整数提升)讨论了将整数类型转换为具有更高级别的类型的具体情况。C++标准的第4.7节(整数转换)以(项目符号4.7.1)开头:Anrvalueofanintegertypecanbeconvertedtoanrvalueofanotherintegertype.Anrvalueofanenumerationtypecanbeconvertedtoanrvalueofanintegertype.据我了解,4.5中描述的转换(可能除了项目符号4.5.3(枚举))可以单独使用4.7部分中的技术来执行:4.5.1和4.5.2完全包含在4.7中。1;4.5.4包含在4.7

c++ - 了解 2^31 和 -2^31 整数提升

#includeintmain(){printf("sizeof(int):%zu\n",sizeof(int));printf("%d\n",2147483648u>-2147483648);printf("%d\n",((unsignedint)2147483648u)>((int)-2147483648));printf("%d\n",2147483648u!=-2147483648);printf("%d\n",((unsignedint)2147483648u)!=((int)-2147483648));return0;}这段代码在C和C++中的输出,在cygwin64和带有

C++ 错误 : definition of implicitly-declared

我正在用C++编写这个链表程序当我测试程序时,我得到了错误linkedlist.cpp:5:24:error:definitionofimplicitly-declared'constexprLinkedList::LinkedList()'LinkedList::LinkedList(){这是代码链表.h文件:#include"node.h"usingnamespacestd;classLinkedList{Node*head=nullptr;intlength=0;public:voidadd(int);boolremove(int);intfind(int);intcount(i

C++11: "narrowing conversion inside { }"带模数

我尝试在启用gcc和C++11的情况下编译以下代码:unsignedintid=100;unsignedchararray[]={id%3,id%5};我收到这些警告:narrowingconversionof‘(id%3u)’from‘unsignedint’to‘unsignedchar’inside{}[-Wnarrowing]seedemoonline有没有办法帮助编译器发现id%3的结果适合unsignedchar? 最佳答案 在这种特定情况下,使idconst或constexpr将解决问题:constexprunsign

c++ - 在 C++ 中构造对象时进行两次隐式转换是否有效?

给定以下两个构造函数签名,是否可以使用Couple("George","Nora")构造一个Couple?我的编译器提示如下所示的错误。如果我用Couple(std::string("George"),std::string("Nora"))调用它,它编译正常。我猜隐式转换存在问题,这让我感到惊讶,因为我认为将char*转换为字符串会很好。classPerson{public:Person(conststd::string&name);};classCouple{public:Coordinate(constPerson&p1,constPerson&p2,constOptional&

c++ - 海湾合作委员会错误 : invalid conversion from double* to const double

我使用的是gcc4.5.0版。使用下面的简单示例,我假设得到一个错误invalidconversionfromdouble*toconstdouble*#includeusingnamespacestd;voidfoo(constdouble*a){cout为什么编译没有错误?类似的反例如下:#includeusingnamespacestd;voidfoo(constdouble**a){cout(第二个示例的解决方案:将foo定义为foo(constdouble*const*a)。感谢JackEdmonds的评论,这解释了错误消息) 最佳答案

c++ - 停止函数隐式转换

我今天遇到了一个奇怪的情况,我需要一个函数来不隐式转换值。在谷歌上搜索后,我找到了这个http://www.devx.com/cplus/10MinuteSolution/37078/1954但我认为对我想阻止的所有其他类型使用函数重载有点愚蠢,所以我改为这样做。voidfunction(int&ints_only_please){}intmain(){chara=0;intb=0;function(a);function(b);}我把代码给一个friend看,他建议我在int之前添加const,这样变量就不可编辑了,但是当我开始编译时很好,但它不应该,请看下面看看我的意思voidfu