草庐IT

c++ - 操作数类型不兼容 ("char"和 "const char*")

我收到以下错误...Operandtypesareincompatible("char"and"constchar*")...尝试执行if语句时。我假设我不了解输入值的存储方式,尽管我不确定是否可以将其转换为匹配类型?要重现的示例代码是:charuserInput_Text[3];if(userInput_Text[1]=="y"){//Dostuff.}我不确定是什么原因造成的。看起来一种类型是char而另一种是constchar指针,尽管我不确定是什么,作为引用,当我不使用数组时也会发生此错误)。非常感谢提示/反馈。 最佳答案

c++ - Gcc:强制编译器默认使用 unsigned char

由于当unsigned限定符不存在时,C++中char的性质是编译器相关的,是否有一个参数我可以传递给GCC强制将所有char编译为unsigned? 最佳答案 您正在寻找的标志是-funsigned-char。来自thedocumentation:-funsigned-charLetthetypecharbeunsigned,likeunsignedchar.Eachkindofmachinehasadefaultforwhatcharshouldbe.Itiseitherlikeunsignedcharbydefaultorli

C++ "error: passing ' const std::map<int, std::basic_string<char>>' as ' this' argument of ..."

使用以下代码(为简洁起见摘录):颜色.h:classcolor{public:color();enumcolorType{black,blue,green,cyan,red,magenta,brown,lightgray,nocolor};colorTypegetColorType();voidsetColorType(colorTypecColortype);stringgetColorText()const;private:colorTypecColortype=nocolor;mapcolors={{black,"black"},{blue,"blue"},{green,"gre

c++ - C 或 C++。如何比较给定 char * 指针的两个字符串?

我正在以两种方式对我的汽车阵列进行排序。如下所示。和另一个制造商。Make是一个char*当我只有指向它们的指针时如何比较字符串?inti,j;for(i=0;iyear>carArray[j+1]->year){swap(carArray[j],carArray[j+1]);}}}}上述方法适用于int的(年)。我怎样才能使它适用于字符指针? 最佳答案 在几乎任何一种情况下,方式都是调用strcmp.如果你的字符串(出于某种奇怪的原因)没有以NUL结尾,你应该使用strncmp相反。但是,在C++中,如果可以合理避免的话,您真的不

以 char* 为键的 C++ unordered_map

尝试使用以char*为键的容器unordered_map时,我感到筋疲力尽(在Windows上,我使用的是VS2010)。我知道我必须为char*定义我自己的比较函数,它继承自binary_function。以下是示例程序。#include#include#includeusingnamespacestd;templatestructmy_equal_to:publicbinary_function{booloperator()(const_Tp&__x,const_Tp&__y)const{returnstrcmp(__x,__y)==0;}};typedefunordered_ma

c++ - 通过指向 int 的指针为 char 数组起别名合法吗?

我知道标准中明确允许以下内容:intn=0;char*ptr=(char*)&n;cout这个呢?alignas(int)charstorage[sizeof(int)];int*ptr=(int*)&storage[0];*ptr=0;cout本质上,我是在问别名规则是否允许通过指向另一种类型的指针访问一系列字符。如果可能,我希望引用标准中指示某种方式的部分。标准的某些部分让我感到矛盾;(3.10.10)似乎表明它是未定义的行为,前提是storage的动态类型不是int。但是,我不清楚动态类型的定义,std::aligned_storage的存在让我相信这是是可能的。

C++ std::ctype<char>::widen() 的作用是什么?

根据C++标准(C++17草案(N4659)的§30.7.5.2.4),out不会对ch执行扩大操作,如果ch是char和out是std::ostream.这是否意味着std::ctype::widen()(即char->char)是否由标准保证为基本源字符集中所有字符的恒等函数(widen(ch)==ch)?如果是这样,这是否反过来意味着标准要求所有语言环境使用基本源字符集中相同的非宽(或多字节)字符编码?如果不是,好像是out,对于文字的字符编码有特殊选择,可能无法在所有语言环境中工作,即使它在某些语言环境中工作。也就是说,可能无法选择字rune字编码,例如out同时适用于所有语言

c++ - 为什么允许 std::atomic_{char,schar,etc.} typedef 是 std::atomic<T> 基类的类型定义,而不仅仅是 atomic<T>?

C++11[atomics.types.generic]p7:Thereshallbenamedtypescorrespondingtotheintegralspecializationsofatomic,asspecifiedinTable145,andanamedtypeatomic_boolcorrespondingtothespecifiedatomic.Eachnamedtypeisaeithertypedeftothecorrespondingspecializationorabaseclassofthecorrespondingspecialization.Ifitisa

c++ - 不能为 std::basic_string<T> foo = "foo"推导出 T = char?

问题:在下面的代码中,第一个示例的模板参数类型推导似乎失败了,但第二个示例却没有。我不明白为什么第一个样本无法推断出T=char.我会认为T从"foo"转换时可以推导出至std::bacis_string,但即使这不起作用,我也提供了第二个函数参数,我认为它会明确约束T至char.为什么它会失败?Doesnotwork:#include#includetemplatevoidprint(conststd::basic_string&a,conststd::basic_string&b){std::cout错误:string.cpp:14:5:error:nomatchingfuncti

c++ - 在 clang 中强制使用 `const char[]` 字符串文字

编译以下代码voidf(char*,constchar*,...){}voidf(constchar*,...){}intmain(){f("a","b");}用clang给我这个错误:prog.cpp:6:2:error:callto'f'isambiguousf("a","b");^prog.cpp:1:6:note:candidatefunctionvoidf(char*,constchar*,...){}^prog.cpp:2:6:note:candidatefunctionvoidf(constchar*,...){}^AFAIK字符串字面量在C++中是常量,因此重载规则应该