g++编译warning: deprecated conversion from string constant to ‘char*’ /tmp/ccSf0DmE.o: In function `ma

我用的ubuntu操作系统,以下是c++Primer Plus(第五版)的一段代码,结果出现了标题所示的警告,请教哪位大神帮忙解解,谢谢。

完整警告如下:

 g++ strgfun.cpp -o strgfun
strgfun.cpp: In function ‘int main()’:
strgfun.cpp:6: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccSf0DmE.o: In function `main':
strgfun.cpp:(.text+0x4b): undefined reference to `c_in_str(char const*, char)'
strgfun.cpp:(.text+0x5f): undefined reference to `c_in_str(char const*, char)'
collect2: ld returned 1 exit status


源码如下:
#include
int c_in_str(const char* str, char ch);
int main(){
        using namespace std;
        char mmm[15] = "minimum";
        char *wail = "ululate";
        int ms = c_in_str(mmm, 'm');
        int us = c_in_str(wail, 'u');
        cout << ms << " m characters in " << mmm << endl;
        cout << us << " u characters in " << wail << endl;
        return 0;
}

int c_int_str(const char* str, char ch){
        int count = 0;

        while(*str){
                if(*str == ch){
                        count++;
                        str++;
                }
        }
        return count;

}

我尝试了把函数原型换为    int c_int_str( char* str, char ch)       和      int c_int_str( char* const str, char ch)     仍然出现了一样的错误。                                       




你可能感兴趣的:(c/c++,deprecated,function,reference)