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

[求助]为什么string 定义字符串不行!!

HaPpY随心 发布于 2007-10-28 18:16, 1868 次点击
书上说,可以用string 可以定义字符串;
请问需要包含什么文件
18 回复
#2
eagleboycn2007-10-28 18:17
#include <string>?

[此贴子已经被作者于2007-10-28 18:18:42编辑过]


#3
HaPpY随心2007-10-28 18:21
回复:(eagleboycn)#include ?[ali...
我在c-free4.0,vc++6.0里用了#include <string>编译时,提示错误 string 那出错
#4
HaPpY随心2007-10-28 18:31
回复:(eagleboycn)#include ?[ali...
c-free错误提示是string name;中的string;
vc++里的错误提示是 iostream
#5
hegj2007-10-28 21:16
string 是在 std 里的。using std::string写了吗?
#6
HaPpY随心2007-10-29 08:25
回复:(hegj)string 是在 std 里的。using std::str...
没写,我去试试
#7
HaPpY随心2007-10-29 08:44
还是不行,以下是我写,求大家看看
//student_.h
#include <string>
using namespace std;
using std::string;
class Student
{
private:
int num;
string name;
string sex;
int scoure[5];
int total_scoure;
public:
void getin();
void serach();
void putout();
}
//student_.cpp
#include "iostream"
#include "student_.h"
#include "string"
using namespace std;
using std::string;
void STUDENT::getin(Student&s)
{
int i;
cout <<"请输入学号:";
cin >>s.num;
cout <<"请输入:";
cin >>s.name;
cout <<"请输入性别:";
cin >>s.sex;
for(i=0;i<5;i++) {cout <<"请输入第"<<i+1<<"科成绩:"cin >>s.score[i];}
cout <<endl;
for(i=0,s.total_score=0;i<5;i++) s.total_score+=s.score[i];
}
int serach(Student&s)
{
return s.num;
}
void putout(Student&s)
{
cout <<"name:"<<name<<'\t';
cout <<"sex:"<<sex<<'\t';
cout <<"total_score:"<<total_score<<endl;
}
int main()
{
Student arry[8];
int i,number;
for(i=0;i<8;i++) getin(arry[i]);
cout <<"请输入你想查的学号:";
cin >>number;
for(i=0;i<8;i++)
{
if(number==serach(arry[i])) putout(arrt[i]);
else cout <<"No the number!"<<endl;
}
return 1;
}
#8
随心2007-10-29 16:29
你的类后边没有分号
#9
忘记喧嚣2007-10-29 18:41
class Student
{
private:
int num;
string name;
string sex;
int scoure[5];
int total_scoure;
public:
void getin();
void serach();
void putout();
} ; //这里少个分号
#10
ybw0012007-10-29 21:18

一般开头都有
#include<iostream>
using namespace std;

若用字符串
#include<string>

[此贴子已经被作者于2007-10-29 21:20:16编辑过]

#11
eagleboycn2007-10-30 08:31

#include 后面的标准库文件,不是用< >的吗?
这个有没有关系啊

#12
HaPpY随心2007-10-30 10:23
不好意思,我发现错误了
我定义的CLASS类后面少了“;”
所以才会出现那写错误
#13
随心2007-10-30 10:53
下回可要注意了,这样的错误在刚开始时几乎都会出现,有时还会不止一次。

[此贴子已经被作者于2007-10-30 10:53:52编辑过]


#14
HaPpY随心2007-11-01 12:48
恩,注意到了
#15
pangneng2007-11-10 03:02
我一看就知道是小分号啦,可是我加分号进去运行还是不对呢.
还有我有点不明白主函数里面是怎么调用类里面的函数成员的啊?有点不明白
我也是刚学C++的所以需要多问 高手多多指教哦
#16
小飞丫2007-11-10 14:27
#include&lt;string.h&gt;
#17
kakawei2007-11-14 10:36
用了using namespace std; 就不需再using std::string;
#18
nuciewth2007-11-14 11:24
typedef char* string ;
#19
HaPpY随心2007-11-14 22:20
学习了
1