#include <stdio.h>
using namespace std;
class Trace{
public :
void C_Printf( char* s){ printf("%s",s); }
};
int main()
{
Trace t;
t.C_Printf("begin main()\n");
//main 函数主体
t.C_Printf("end main()\n");
}
如上程序在linux中运行 没看出啥错误吧,g++的时候出现下面报错:
CMain.cpp: In function ‘int main()’:
CMain.cpp:11:31: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
t.C_Printf("begin main()\n");
^
CMain.cpp:13:29: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
t.C_Printf("end main()\n");
^
正是t.C_Printf()这个函数的原因,char* 作为参数是希望系统给传入一个字符变脸,而我们给的是一串常量字符串,所以报错。
在参数前加const可解决此报警: void C_Printf(const char* s){ printf("%s",s); }
运行结果如下:
begin main()
end main()