开发者社区> 问答> 正文

一段c++程序win下正常ununtu上编译报错,求指教!!?报错

有一段代码在windows下编译运行正常   但在ubuntu上却出现了错误 小生百思不得其解!!求大神指教!!



//******************************************

//
//静态函数的调用     C++语言实现
//
//
//
//******************************************
#include<iostream>
#include<string>
using namespace std;
class student{
    public:
        student(char *name1,char *stu_no1,float score1);
        ~student();
        void show();
        void show_count_sum_ave();
    private:
        char *name;
        char* stu_no;
        float score;
        static int count;
        static float sum;
        static float ave;
};
student::student(char *name1,char *stu_no1,float score1)
{
    name=new char[strlen(name1)+1];           //显示strlen作用域有问题
        strcpy(name,name1);
        stu_no=new char[strlen(stu_no1)+1];
    strcpy(stu_no,stu_no1);
    score=score1;
    ++count;
    sum=sum+score;
    ave=sum/count;
}

student::~student()
{
    delete []name;
    delete []stu_no;
    --count;
    sum=sum-score;
}

void student::show()
{
    cout<<"\n\n\n name:"<<name<<endl;
    cout<<"number:"<<stu_no<<endl;
    cout<<"ave"<<score<<endl;
}
void student::show_count_sum_ave()
{
   cout<<"\n all student number"<<count;
   cout<<"\n the average number"<<ave;
}
int student::count=0;
float student::sum=0.0;
float student::ave=0.0;
int main()
{
    student stu1("lim ming","08150201",90);
    stu1.show();
    stu1.show_count_sum_ave();
    cout<<"\n\n--------------------------\n\n";
    student stu2("zhang da wei","09150201",80);
    stu2.show();
    stu2.show_count_sum_ave();
    return 0;

}



zhouyao@ThinkPad-Edge-E530:~$ g++ 5.7a.cpp


5.7a.cpp: 在构造函数‘student::student(char*, char*, float)’中:
5.7a.cpp:27:28: 错误: ‘strlen’在此作用域中尚未声明
5.7a.cpp:28:26: 错误: ‘strcpy’在此作用域中尚未声明
5.7a.cpp: 在函数‘int main()’中:
5.7a.cpp:61:39: 警告: 不建议使用从字符串常量到‘char*’的转换 [-Wwrite-strings]
5.7a.cpp:61:39: 警告: 不建议使用从字符串常量到‘char*’的转换 [-Wwrite-strings]
5.7a.cpp:65:43: 警告: 不建议使用从字符串常量到‘char*’的转换 [-Wwrite-strings]
5.7a.cpp:65:43: 警告: 不建议使用从字符串常量到‘char*’的转换 [-Wwrite-strings]

windows下运行正常难道是编译器有区别???  小生新手多多指教



展开
收起
爱吃鱼的程序员 2020-06-22 19:40:41 588 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    strlen()、strcpy()在C语言中均在string.h中声明,在C++中使用时要加上前缀c去掉.h就好了。

    #include<cstring>

    回复 @周耀1994:Warning已经很明确的告诉你了,是字符串的转换的问题。那个警告是为什么???求指教!!谢谢了!!问题解决了strlen和strcpy这两个函数的头文件是 cstring,你加上试试。
    回复 @周耀1994:怎么解决的啊谢谢!问题解决了你这<title>不利于SEO,先把ununtu改回ubuntu再说.不好意思因该是ubuntu打错了ununtu是啥?不好意思是应该是ubuntu

    引用来自“SErHo”的答案

    strlen和strcpy这两个函数的头文件是 cstring,你加上试试。
    谢谢!!

    警告的问题:

    把char*name1,char*stu_no1改为constchar*name1,constchar*stu_no1试试

    2020-06-22 19:40:56
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载