2015-12-20 48 views
-2

我在C初学者++。我继承的工作。我已经写了代码和编译它,它似乎是工作的罚款。?,我得到预期的输出但是当我编译它,我得到13个类似的警告我不知道是什么问题我如何可以覆盖这些警告,这里是我的代码:充分利用字符串常量警告“过时转换为char *。为什么会收到警告?

#include <iostream> 
#include <string.h> 

using namespace std; 
class Identity 
{ 
protected: 
    char *name; 
    int dob; 
    char* blood_group; 

    Identity(char *iname="", int nlength=1, int idob=0, char *iblood_group="", int blength=2):dob(idob) 
    { 
     name = new char[nlength+1]; 
     strncpy(name,iname,nlength); 
     name[nlength]='\0'; 

     blood_group = new char[blength+1]; 
     strncpy(blood_group,iblood_group,blength); 
     blood_group[blength]='\0'; 
    } 

    ~Identity() 
    { 
     delete[] name; 
     delete[] blood_group; 
    } 
}; 

class Physical 
{ 
protected: 
    double height; 
    double weight; 

    Physical(double pheight = 0.0, double pweight = 0.0):height(pheight),weight(pweight) 
    { 
    } 
}; 

class Registration 
{ 
protected: 
    int policy_number; 
    char* contact_address; 

    Registration(int p_num=0, char* addr="", int alength=1):policy_number(p_num) 
    { 
     contact_address = new char[alength+1]; 
     strncpy(contact_address,addr,alength); 
     contact_address[alength] = '\0'; 
    } 

    ~Registration() 
    { 
     delete[] contact_address; 
    } 
}; 

class Contact:public Identity, public Physical, public Registration 
{ 
private: 
    char* ph_number; 
    char* driver_license; 
public: 
    Contact(char *name ="",int nlength=0,int dob = 0, char* blood = "", int blength = 0,double height = 0, double weight = 0, int pol_num = 0, char* cont_addr="", int alength=10, char *ph="",int plength=10,char* lic="",int llength=10):Identity(name,nlength,dob,blood,blength),Physical(height,weight), Registration(pol_num,cont_addr,alength), ph_number(ph),driver_license(lic) 
    { 
     ph_number = new char[plength+1]; 
     strncpy(ph_number,ph,plength); 
     ph_number[plength] = '\0'; 

     driver_license = new char[llength+1]; 
     strncpy(driver_license,lic,llength); 
     driver_license[llength] = '\0'; 
    } 

    ~Contact() 
    { 
     delete[] ph_number; 
     delete[] driver_license; 
    } 

    char* GetName() 
    { 
     return name; 
    } 

    int GetDob() 
    { 
     return dob; 
    } 

    char* GetBloodGroup() 
    { 
     return blood_group; 
    } 

    double GetHeight() 
    { 
     return height; 
    } 

    double GetWeight() 
    { 
     return weight; 
    } 

    int GetPolicyNum() 
    { 
     return policy_number; 
    } 

    char* GetAddress() 
    { 
     return contact_address; 
    } 

    char* GetPhoneNumber() 
    { 
     return ph_number; 
    } 

    char* GetDriverLicense() 
    { 
     return driver_license; 
    } 
}; 

int main() 
{ 
    using namespace std; 
    Contact kck("MyName",strlen("MyName"),11111111,"A+",strlen("A+"),15.10,651.5,1111,"MyArea",strlen("MyArea"),"1111111111", strlen("1111111111"),"ABCD1234",strlen("ABCD1234")); 

    cout << kck.GetName() << endl; 
    cout << kck.GetDob() << endl; 
    cout << kck.GetBloodGroup() << endl; 
    cout << kck.GetHeight() << endl; 
    cout << kck.GetWeight() << endl; 
    cout << kck.GetPolicyNum() << endl; 
    cout << kck.GetAddress() << endl; 
    cout << kck.GetPhoneNumber() << endl; 
    cout << kck.GetDriverLicense() << endl; 

return 0; 
} 
+2

不要使用'strncpy'。它不会做你认为它做的事。仔细阅读其文档,并在被复制的字符串比可用空间更长时考虑结果。 –

回答

0

在许多你写的东西,如:

char *name = "" 

但是,""有类型const char[1]。这隐含地转换为const char *。但是,然后尝试将其分配给char *,这是试图忽略const限定符。

由于C++ 11这是不允许的。在C++ 11之前,这是允许的,但不推荐使用。编译器警告你,这是一个坏主意。

如果你真的必须使用指针,然后使用char const *的,你可能会指向一个字符串的任何情况。

但是,你会好得多避免使用指针的全部,因为它们复杂的代码,并介绍:如果你没有使用指针会有没有错误的机会。将字符串的长度与字符串分开是非常糟糕的。

例如,使用string来保存所有字符串。作为一名初学者,最简单的技巧应该是最好的,巧合的技巧也是最好的。到目前为止,您编写程序的方式只是让您无缘无故地生活。

相关问题