注册 登录
编程论坛 C++教室

关于string类型的问题,在构造Employee类时的错误不明白

zoufengrui 发布于 2012-12-24 16:56, 454 次点击
#include<iostream>
#include<cstring>
using namespace std;
class Employee{
public:
    Employee(string* Name,string* City,string* Address,string* Postcode){
        strcpy(Name,N);
        strcpy(Address,A);
        strcpy(City,C);
        strcpy(Postcode,P);;
    ~Employee(){};
    void display(){};
    void chang_name(string* Name,string* City,string* Address,string* Postcode){};
private:
    string Name;
    string City;
    string Address;
    char Postcode;
};
void Employee::display(){
    cout<<"Name:  "<<Name<<"    City:  "<<City<<"    Address:  "<<    Postcode:  "<<endl;
}
void Employee::chang_name(string Name,string City,string Address,string Postcode){
    cout<<"Please enter the name:"<<endl;
    cin>>Name;
    cout<<"Please enter the city:"<<endl;
    cin>>City;
    cout<<"Please enter the address:"<<endl;
    cin>>Address;
    cout<<"Please enter the postcode:"<<endl;
}
int main(){
    int i;
    Employee *people=new Employee[5];
    for(i=0;i<6;i++){
        cout<<"Please inter the "<<(i+1)<<"'s information"<<endl;
        Employee[i].change_name();
    }
    for(i=;i<6;i++){
        cout<<"the result is:"<<endl;
        Employee[i].display();
    }
    return 0;
}
机器在编译时在display()函数处报错,说是newline in constant,这是什么情况??还有,我这样写程序可以么???
7 回复
#2
zoufengrui2012-12-24 17:08
嗯嗯嗯,错了,是
#include<iostream>
#include<cstring>
using namespace std;
class Employee{
public:
    Employee(string* Name,string* City,string* Address,string* Postcode){
        strcpy(Name,N);
        strcpy(Address,A);
        strcpy(City,C);
        strcpy(Postcode,P);;
    ~Employee(){};
    void display(){};
    void chang_name(string* Name,string* City,string* Address,string* Postcode){};
private:
    string Name;
    string City;
    string Address;
    char Postcode;
};
void Employee::display(){
    cout<<"Name:  "<<Name<<"    City:  "<<City<<"    Address:  "<<"    Postcode:  "<<endl;
}
void Employee::chang_name(string Name,string City,string Address,string Postcode){
    cout<<"Please enter the name:"<<endl;
    cin>>Name;
    cout<<"Please enter the city:"<<endl;
    cin>>City;
    cout<<"Please enter the address:"<<endl;
    cin>>Address;
    cout<<"Please enter the postcode:"<<endl;
}
int main(){
    int i;
    Employee *people=new Employee[5];
    for(i=0;i<6;i++){
        cout<<"Please inter the "<<(i+1)<<"'s information"<<endl;
        Employee[i].change_name();
    }
    for(i=;i<6;i++){
        cout<<"the result is:"<<endl;
        Employee[i].display();
    }
    return 0;
}
这里报错::
 error C2664: 'strcpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
#3
zoufengrui2012-12-24 17:10
补充一下,是在实现函数display()时报错的
#4
peach54602012-12-24 17:18
Postcode:  "<<endl;
#5
peach54602012-12-24 17:18
你这里面错误很多,慢慢改吧
#6
peach54602012-12-24 17:27
回复 3楼 zoufengrui
为什么要用指针呢,唉...

extern char *strcpy(char *dest,const char *src);
std::string的实现不是拿char*实现的
cstring才是

[ 本帖最后由 peach5460 于 2012-12-24 17:29 编辑 ]
#7
peach54602012-12-24 17:28
Employee *people=new Employee[5];

这句是new不出来的,没有无参构造
#8
zoufengrui2012-12-24 21:08
能不能帮忙写个用string 类型的成果程序,我已经纠结了一晚上了,其他方法我是做出来了 ,我主要是想看看string类的用法
1