Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么字符串在传递到printf时必须转换为c_str(c字符串)? 我是C++新手 我知道我不应该在C++中使用Prtff,而我有CUT,但这是为了实验。 我的问题是,为什么我们必须把一个字符串转换成cStrr(C字符串),而在C++中传递给Prtff,而它在CUT中没有转换就可以工作。 下面是我的代码 #include<iostream> #include<stdio.h> using namespace std; class A{ int i; string str; public: A (int value, const string & s) : i(value), str(s){};// constructor // setters void setvalue(int value) {i = value;} void setstr(const string & s) {str = s;} //geters int get_value() {return i;} string get_str() {return str;} const char *get_str_cstr() {return str.c_str();}// I didn't get why we have to declare constant }; int main(){ // new code A obj1 = {11, "Jill"}; cout<<"value is : "<<obj1.get_value()<<" string is "<<obj1.get_str()<<endl; // Now we wil change the values in A obj1.setvalue(2); obj1.setstr("Jack"); cout<<"value after change is : "<<obj1.get_value()<<" string after change is "<<obj1.get_str()<<endl; // now we will use printf where get_str dosen't not work //Error: for below commented printf function /*In function 'int main()':| error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'| ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| */ //printf("Value is %d and String is %s",obj1.get_value(),obj1.get_str()); // hence we declare a new char * get_str_cstr to make it work in printf; printf("Value is %d and String is %s",obj1.get_value(),obj1.get_str_cstr()); return 0;} #包括 #包括 使用名称空间std; 甲级{ int i; 字符串str; 公众: A(int-value,const-string&s):i(value),str(s){};//构造函数 //二传手 void setvalue(int值){i=value;} void setstr(常量字符串&s){str=s;} //格特斯 int get_value(){return i;} 字符串get_str(){return str;} const char*get_str_cstr(){return str.c_str()}//我不明白为什么必须声明常量 }; int main(){ //新代码 A obj1={11,“Jill”}; cout_C++ - Fatal编程技术网

为什么字符串在传递到printf时必须转换为c_str(c字符串)? 我是C++新手 我知道我不应该在C++中使用Prtff,而我有CUT,但这是为了实验。 我的问题是,为什么我们必须把一个字符串转换成cStrr(C字符串),而在C++中传递给Prtff,而它在CUT中没有转换就可以工作。 下面是我的代码 #include<iostream> #include<stdio.h> using namespace std; class A{ int i; string str; public: A (int value, const string & s) : i(value), str(s){};// constructor // setters void setvalue(int value) {i = value;} void setstr(const string & s) {str = s;} //geters int get_value() {return i;} string get_str() {return str;} const char *get_str_cstr() {return str.c_str();}// I didn't get why we have to declare constant }; int main(){ // new code A obj1 = {11, "Jill"}; cout<<"value is : "<<obj1.get_value()<<" string is "<<obj1.get_str()<<endl; // Now we wil change the values in A obj1.setvalue(2); obj1.setstr("Jack"); cout<<"value after change is : "<<obj1.get_value()<<" string after change is "<<obj1.get_str()<<endl; // now we will use printf where get_str dosen't not work //Error: for below commented printf function /*In function 'int main()':| error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'| ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| */ //printf("Value is %d and String is %s",obj1.get_value(),obj1.get_str()); // hence we declare a new char * get_str_cstr to make it work in printf; printf("Value is %d and String is %s",obj1.get_value(),obj1.get_str_cstr()); return 0;} #包括 #包括 使用名称空间std; 甲级{ int i; 字符串str; 公众: A(int-value,const-string&s):i(value),str(s){};//构造函数 //二传手 void setvalue(int值){i=value;} void setstr(常量字符串&s){str=s;} //格特斯 int get_value(){return i;} 字符串get_str(){return str;} const char*get_str_cstr(){return str.c_str()}//我不明白为什么必须声明常量 }; int main(){ //新代码 A obj1={11,“Jill”}; cout

为什么字符串在传递到printf时必须转换为c_str(c字符串)? 我是C++新手 我知道我不应该在C++中使用Prtff,而我有CUT,但这是为了实验。 我的问题是,为什么我们必须把一个字符串转换成cStrr(C字符串),而在C++中传递给Prtff,而它在CUT中没有转换就可以工作。 下面是我的代码 #include<iostream> #include<stdio.h> using namespace std; class A{ int i; string str; public: A (int value, const string & s) : i(value), str(s){};// constructor // setters void setvalue(int value) {i = value;} void setstr(const string & s) {str = s;} //geters int get_value() {return i;} string get_str() {return str;} const char *get_str_cstr() {return str.c_str();}// I didn't get why we have to declare constant }; int main(){ // new code A obj1 = {11, "Jill"}; cout<<"value is : "<<obj1.get_value()<<" string is "<<obj1.get_str()<<endl; // Now we wil change the values in A obj1.setvalue(2); obj1.setstr("Jack"); cout<<"value after change is : "<<obj1.get_value()<<" string after change is "<<obj1.get_str()<<endl; // now we will use printf where get_str dosen't not work //Error: for below commented printf function /*In function 'int main()':| error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'| ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| */ //printf("Value is %d and String is %s",obj1.get_value(),obj1.get_str()); // hence we declare a new char * get_str_cstr to make it work in printf; printf("Value is %d and String is %s",obj1.get_value(),obj1.get_str_cstr()); return 0;} #包括 #包括 使用名称空间std; 甲级{ int i; 字符串str; 公众: A(int-value,const-string&s):i(value),str(s){};//构造函数 //二传手 void setvalue(int值){i=value;} void setstr(常量字符串&s){str=s;} //格特斯 int get_value(){return i;} 字符串get_str(){return str;} const char*get_str_cstr(){return str.c_str()}//我不明白为什么必须声明常量 }; int main(){ //新代码 A obj1={11,“Jill”}; cout,c++,C++,最初来自C,它没有std::string,因此类似的参数类型是const char*,这是调用时得到的 std::cout使用std::string的原因是因为为该类定义了。因为不知道a是什么。printf()的protype是 int printf( const char* format, ... ); int fprintf( std::FILE* stream, const char* format, ... ); int sprintf( char* buffer, const char

最初来自
C
,它没有
std::string
,因此类似的参数类型是
const char*
,这是调用
时得到的

std::cout
使用
std::string
的原因是因为为该类定义了。

因为不知道a是什么。
printf()的protype是

int printf( const char* format, ... );
int fprintf( std::FILE* stream, const char* format, ... );
int sprintf( char* buffer, const char* format, ... );
int snprintf( char* buffer, std::size_t buf_size, const char* format, ... );

std::string
cout
一起工作的原因是
std::string
提供了与
cout
一起工作的
printf
来自C库,它早于对象、模板和函数重载。当您指定
%s
格式时,函数采用以null结尾的ch的地址字符序列,并将其打印。
printf
不知道字符串来自何处。事实上,它不知道其参数类型,因为它使用可变长度参数列表功能

<> >代码> STD::String 是一个C++字符串。调用“代码> CyString())/c>它产生一个指向C字符串开头的指针,它适合传递给<代码> Prtff<代码>和其他函数,期望C字符串。


<代码> CUT另一方面,已经用类和重载来构建。对于代码>操作符字符串有一个特殊的重载,是C++ STL中的一个类。 c_str()是string类的成员函数

签名为:

常量图表*c_str()常量{return\M_start;}

现在,来到printf,它的签名是:

int printf(常量字符*格式,…)


现在,只要您给它一个符合const char*格式的参数,它就会接受它。

printf
被指定为能够处理字符指针。它不是被指定为处理大象、抵押贷款危机或
std::string
。顺便说一句,它是如何将对象(如
std::string
)传递给省略号的实现定义(
printf
..
)。