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

C++类实现中参数如何表示字符串?

美珊 发布于 2013-10-04 21:32, 782 次点击
3-44.cpp
#include<iostream.h>

#include"3-44.h"

void CPerson::get(char a,int b,char c,int d)
{
    name=a;
    num=b;
    sex=c;
    age=d;
};

void CPerson::output()
{
    cout<<name<<' '<<num<<' '<<sex<<' '<<age<<endl;
};

void main()
{
    CPerson o;
    o.get('c',3,'m',5);
    o.output();
}
3-44.h
class CPerson
{
private:
    char name;
    int num;
    char sex;
    int age;

public:
    void get(char,int,char,int);
    void output();
};
这个函数是将每个学生的信息进行初始化并输出来,我想把name从字符形式改为字符串形式,如何改?
10 回复
#2
qunxingw2013-10-05 05:28
函数的定义花括号外怎有;
#3
blueskiner2013-10-05 08:03
用字符数组保存字符串吧。char name[128]
#4
3037709572013-10-05 09:48
添加:using namespace std;
然后将char类型改成string类型,代码如下:

#include<iostream.h>
using namespace std;
class CPerson
{
private:
    string name;
    int num;
    string sex;
    int age;

public:
    void get(string,int,string,int);
    void output();
};
void CPerson::get(string a,int b,string c,int d)
{
    name=a;
    num=b;
    sex=c;
    age=d;
};
void CPerson::output()
{
    cout<<name<<' '<<num<<' '<<sex<<' '<<age<<endl;
};
int main()
{
    CPerson o;
    o.get("姓名: 苏巧儿 \n编号:",3,"\n性别: 女 \n年龄:",25);
    o.output();
    return 0;
}
运行结果如下:
只有本站会员才能查看附件,请 登录

#5
IT男year2013-10-05 10:00
#include<iostream>
#include<string>
using namespace std;
class CPerson
{
private:
     string name;
     int num;
     string sex;
     int age;
 
public:
     void get(string,int,string,int);
     void output();
};
void CPerson::get(string a,int b,string c,int d)
{
     name=a;
     num=b;
     sex=c;
     age=d;
}
 
void CPerson::output()
{
     cout<<name<<' '<<num<<' '<<sex<<' '<<age<<endl;
}
 
void main()
{
     CPerson o;
     o.get("c",3,"m",5);
     o.output();
}
把char该成字符串类型即string,再加一个头文件(#include<string>)就好了。
#6
美珊2013-10-05 12:58
回复 2楼 qunxingw
哦,那个好像是编译时候在3-44.h中定义类的时候顺便在3-44.cpp中多添加了,应该要去掉的,谢谢哈。
#7
美珊2013-10-05 12:59
回复 4楼 303770957
恩恩,谢谢你哈,我懂了。
#8
美珊2013-10-05 13:22
回复 4楼 303770957
可是为什么我在VC6.0中编译会出错?矛头指向using namespace std?

D:\Program Files\C++\VC习题第三章\3-44(1).cpp(2) : error C2871: 'std' : does not exist or is not a namespace
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(6) : error C2146: syntax error : missing ';' before identifier 'name'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(6) : error C2501: 'string' : missing storage-class or type specifiers
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(6) : error C2501: 'name' : missing storage-class or type specifiers
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(8) : error C2146: syntax error : missing ';' before identifier 'sex'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(8) : error C2501: 'string' : missing storage-class or type specifiers
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(8) : error C2501: 'sex' : missing storage-class or type specifiers
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(12) : error C2061: syntax error : identifier 'string'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(15) : error C2065: 'string' : undeclared identifier
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(15) : error C2146: syntax error : missing ')' before identifier 'a'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(15) : error C2182: 'get' : illegal use of type 'void'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(15) : error C2350: 'CPerson::get' is not a static member
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(15) : error C2059: syntax error : ')'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(16) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(16) : error C2447: missing function header (old-style formal list?)
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(24) : error C2065: 'name' : undeclared identifier
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(24) : error C2065: 'sex' : undeclared identifier
D:\Program Files\C++\VC习题第三章\3-44(1).cpp(29) : error C2660: 'get' : function does not take 4 parameters
执行 cl.exe 时出错.

3-44(1).exe - 1 error(s), 0 warning(s)

求教。。
#9
美珊2013-10-05 13:25
回复 5楼 IT男year
好,谢谢你。
#10
美珊2013-10-05 13:32
回复 8楼 美珊
哦,那个刚才我看了一下9楼的代码
#include<iostream>
#include<string>
using namespace std;
发现iostream和iostream.h的不同,还有就是要添加#include<string>哈,那个iostream.h好像不能用using namespace std;
恩,还是很谢谢你,我解决了问题了。
#11
blueskiner2013-10-05 16:49
回复 10楼 美珊
iostream.h不是标准头文件。iostream才是。using namespace std;也是标准的用法。
1