草庐IT

c++ - 从 ‘const char*’ 到 ‘char*’ [-fpermissive] 的无效转换

int::cadenacalculatelenght(constcadena&a,constchar*cad){cadenac;intlenght=0;char*punt;punt=cad;while(*punt){lenght++;punt++;}returnlenght;}我有这个问题,我想计算C字符串的长度而不使用像strlen这样的函数,在我的cadena类的其他方法中我可以,因为它不是constchar*,但现在我不知道该怎么办。 最佳答案 您可以将punt声明为正确的类型:constchar*punt=cad;

C++,从 ‘char’ 到 ‘const char*’ 的无效转换

在C++中我有这个程序:#includeusingnamespacestd;intmain(){stringexpression_input;cout我收到以下错误:prog.cpp:15:error:invalidconversionfrom‘char’to‘constchar*’prog.cpp:15:error:initializingargument2of‘std::basic_string&std::basic_string::insert(typename_Alloc::rebind::other::size_type,const_CharT*)[with_CharT=ch

c++ - char* 不应该隐式转换为 std::string 吗?

这是我的代码:#includeusingnamespacestd;structST{};booloperator==(conststructST*s1,conststring&s2){returntrue;}intmain(){structST*st=newST();constchar*p="abc";if(st==p){return0;}return1;}编译错误:prog.cpp:14:12:error:comparisonbetweendistinctpointertypes‘ST*’and‘constchar*’lacksacast[-fpermissive]if(st==p)

c++ - 让模板通过指定 bitesize 在 char/short/int 之间进行选择?

我有这样的东西:templatestructbin{private:public:struct{int_value:SIZE;};}是否可以根据SIZE更改_value的数据类型?如果SIZE=8且 最佳答案 这不是特别优雅,但是:templatestructbest_integer_type{typedefbest_integer_type::typetype;};templatestructbest_integer_type{typedefchartype;};templatestructbest_integer_type{ty

c++ - 使用 "const char*"和 "char*"参数连接两个第三方模块

我有两个第三方模块,我必须将它们组合起来。首先,我从一个类中获取数据。我会将这些数据提交给一个函数。boolloadLibrary(constchar*strPlugName){HPLUGINtemp=_BASS_PluginLoad(strPlugName,0);returnfalse;}constchar*strPlugName是我从另一个库获得的值。我自己无法更改此值类型。在函数内部我尝试调用BASS库函数。HPLUGINtemp=_BASS_PluginLoad(strPlugName,0);Definition:typedefHPLUGIN(*BASS_PluginLoad_

c++ - 加倍到 Const Char*

如何将double转换为constchar,然后再将其转换回double?我想将double转换为字符串,通过fputs将其写入文件,然后当我读取文件时,需要将其转换回double。我使用的是VisualC++2010ExpressEdition。 最佳答案 如果您只想将double值写入文件,您可以直接写入,无需将它们转换为constchar*。将它们转换为constchar*是多余的。只需使用std::ofstream作为:std::ofstreamfile("output.txt")'doubled=1.989089;file

c++ - 如何将 stringstream 内容放入 char 而不是 string 类型?

大家都知道stringstream.str()需要一个字符串变量类型来存储stringstream.str()的内容。我想将stringstream.str()的内容存储到char变量或char数组或指针中。这有可能吗?请用你的答案写一个简单的例子。 最佳答案 为什么不只是std::strings=stringstream.str();constchar*p=s.c_str();?编辑:请注意,您不能随意提供p在你的函数之外:它的生命周期被绑定(bind)到s的生命周期,所以您可能想要复制它。编辑2:正如@David所暗示的,上面的

c++ - char 数组作为 placement new 的存储

以下是否具有明确定义的行为的合法C++?classmy_class{...};intmain(){charstorage[sizeof(my_class)];new((void*)storage)my_class();}或者由于指针转换/对齐方面的考虑,这是有问题的吗? 最佳答案 是的,这是有问题的。您根本无法保证内存正确对齐。虽然存在各种技巧来获得正确对齐的存储,但您最好使用Boost或C++0x的aligned_storage,它们对您隐藏了这些技巧。那么你只需要://C++0xtypedefstd::aligned_stora

c++ - reinterpret_cast<char *> 是 reinterpret_cast 的唯一有效用法吗?

我最近了解到C++标准包含“严格的别名规则”,它禁止通过不同类型的变量引用相同的内存位置。但是,该标准确实允许char类型合法地别名任何其他类型。这是否意味着reinterpret_cast只能合法地用于转换为char*或char&类型?我相信严格的别名允许在继承层次结构中的类型之间进行转换,但我认为这些情况倾向于使用dynamic_cast?谢谢 最佳答案 reinterpret_cast有许多不同的用途。cppreferencepage列出了11个不同的案例。我猜你只是在询问情况5和6:将T*转换为U*,并将T转换为你&.在这些

C++: "my text"是 std::string、*char 还是 c 字符串?

我刚刚做了看起来是acommonnewbiemistake的事情:首先我们阅读oneofmanytutorials是这样的:#includeintmain(){usingnamespacestd;ifstreaminf("file.txt");//(...)}其次,我们尝试在我们的代码中使用类似的东西,它是这样的:#includeintmain(){usingnamespacestd;std::stringfile="file.txt";//Orgetthenameofthefile//fromafunctionthatreturnsstd::string.ifstreaminf(fi