草庐IT

c++ - 哪些提升类型用于 switch-case 表达式比较?

以下程序在使用不同的编译器编译时打印“unknown”。为什么会这样?#include"stdio.h"constcharOPTION=(char)(unsignedchar)253;intmain(intargc,char*argv[]){unsignedcharc=253;switch(c){caseOPTION:printf("option\n");break;default:printf("unknown\n");break;}return0;}在查看C++标准(N36902013-05-05)时,我看到了switch的子句:6.4.2Theswitchstatement2Th

c++ - 全局函数和不明确的参数 NULL 与 char* 在 vs 2013 和 GCC 之间

我试图了解当前C++14标准关于解决模糊函数调用的内容,主要是因为我看到GCC4.9.1和VisualStudio2013更新3之间的差异这是代码(MS和GCC完全相同):#includeusingnamespacestd;voidf(char*str,intchars){coutVisualStudio使用默认vs标志调用f(char*,int)GCC给了我一个编译器错误:重载'f(char*&,NULL)'的调用不明确。gcc仅使用-std=c++11标志。 最佳答案 这取决于NULL的定义。MSVC将其定义为0,因此int重载

C++ --- 错误 C2664 : 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'

我是C++的新手,我正在尝试构建这个非常简单的代码,但我不明白为什么会出现此错误:Error1errorC2664:'intscanf(constchar*,...)':cannotconvertargument1from'int'to'constchar*'代码如下://lab.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#includeintmain(intargc,char*argv[]){introw=0;printf("Pleaseenterthenumberofrows:");sc

c++ - 错误 : no matching function for call to ‘to_string(std::basic_string<char>&)’

即使在模板中我可以有任何类型,函数to_string对基本字符串不起作用:例如:std::stringstr("mystring");my_class(str);用这个仿函数定义:templatevoidoperator()(valuetypevalue){...private_string_field=std::to_string(value);不起作用。这是错误:error:nomatchingfunctionforcallto‘to_string(std::basic_string&)’避免它的最佳方法是什么。事先,我要求避免仅仅因为一些常见的关键字就链接到不相关的问题。

c++ - std::vector<int, std::allocator<char>> 有效吗?

标准没有说明std::vector的分配器但只需要分配器满足Allocator概念。没有关于分配器的value_type,没有reference_type,什么都没有。我以为std::vector内部重新绑定(bind)A到T的分配器,所以我给了一个vectorstd::allocator它按预期工作。但是,如果std::allocator,GCC会生成错误给出,如下:/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/ext/alloc_traits.h:Ininstantiationof‘struct__gnu_cxx::__

c++ - 在没有 C++ 11 的情况下使用 char16_t、char32_t 等?

我想要固定宽度的类型,包括字符类型。提供整数类型,但不提供字符类型,除非在使用C++11时,这是我做不到的。是否有一种干净的方法来定义这些类型(char16_t、char32_t等)而不与C++11定义的类型发生冲突,以防源代码与C++11混合?谢谢你:) 最佳答案 我认为,检查是否支持这种类型是平台相关的事情。例如,GCC定义:__CHAR16_TYPE__和__CHAR32_TYPE__如果提供了这些类型(需要ISOC11或C++11支持)。但是,您不能直接检查它们的存在,因为它们是基本类型,而不是宏:InC++,char16_

c++ - 在编译时或运行时将 const char * 映射到 duck-typed T

我有很多类A、B、C、D等,它们都是鸭子类型的,因此具有相同的方法和接口(interface),但不是从同一个类继承的。例如classA{public:voidfoo();voidbar();}classB{public:voidfoo();voidbar();}classC{public:voidfoo();voidbar();}我想在运行时将constchar*映射到这些类之一的相应实例,例如“A”->Aa“B”->Bb这里a是类A的一些实例。或在编译时将'constchar*`映射到相应的类型,例如“A”->A我需要在其他一些函数调用中使用该对象的实例(即调用foo()或bar(

python - 如何使用 boost.python 将预填充的 "unsigned char*"缓冲区传递给 C++ 方法?

我有一个C++类,它有一个成员函数,它接受一个unsignedchar*缓冲区和一个unsignedint长度作为参数并对它们进行操作。我已经用Boost::Python包装了这个类,并希望将一个预填充的缓冲区从Python脚本传递给这个类。Python端缓冲区是使用struct.pack创建的。我不知道如何使参数类型匹配并不断收到Boost.Python.ArgumentError。include/Example.h#ifndefEXAMPLECLASS_H_#defineEXAMPLECLASS_H_#includeclassExampleClass{public:ExampleC

C++ 返回整个空分隔字符串

我需要为函数提供一个以空字符结尾的字符序列,但我无法弄清楚如何从字符串文字最终变成一个字符指针。问题在这里演示:#include#includeusingnamespacestd;intmain(){std::stringstr("this\0isa\0nullseparated\0string");std::cout我当前的代码有效..std::stringtmp=g_apidefs[i].ret_val+'.'+g_apidefs[i].parm_types+'.'+g_apidefs[i].parm_names+'.'+g_apidefs[i].html_help;size_tl

c++ - 为什么我不允许将返回 const char* 的函数的结果分配给 char*,但可以将字符串文字(常量)分配给 char*?

这个问题在这里已经有了答案:WhatisthetypeofstringliteralsinCandC++?(4个答案)关闭6年前。返回constchar*的函数不能分配给char*constchar*func(){return"Thisisaconststringtwo";}但是char*直接在main中赋值了一个常量字符串:intmain(){char*d="thisisaconststringone";//worksfinechar*e=func();//errorcannotconvertfrom'constchar*'to'char*'return1;}为什么矛盾?