问下这个问题

smcao 2007-03-17 01:29:43
#include<iostream>
using namespace std;

class student
{
private:
char xh[10],name[20];
float cj;
public:
void cx(char xh)
{int i,j;

for(i=1;i<10;i++)
if( xh==xs[i].xh) // 这里是错在哪里???
{
cout<<"xue sheng xin xi:"<<endl<<"xh name cj "<<endl<<xs[i].xh<<" "<<xs[i].name<<" "<<xs[i].cj<<" ";
break;

}
}
void set()
{
cout<<"shu ru xue sheng qi ben xin xi:\n";
for(i=1;i<10;i++)
{
cout"xh name cj";
cin>>xs[i].xh>>xs[i].name>>xs[i].cj;
}





}

};
class xs[10];


int main()
{
char xhao[10];
set();
cout<<"shu ru ni yao cha xu de xh:\n";
cin>>xh;
cx(xhao);
return 0;




}



错误信息:
--------------------Configuration: Cppfdf1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
D:\zy\C++编程作业\my空间\Cpp1.cpp(14) : error C2065: 'xs' : undeclared identifier
D:\zy\C++编程作业\my空间\Cpp1.cpp(14) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(14) : error C2228: left of '.xh' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(16) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(16) : error C2228: left of '.xh' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(16) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(16) : error C2228: left of '.name' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(16) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(16) : error C2228: left of '.cj' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(24) : error C2065: 'i' : undeclared identifier
D:\zy\C++编程作业\my空间\Cpp1.cpp(26) : error C2143: syntax error : missing ';' before 'string'
D:\zy\C++编程作业\my空间\Cpp1.cpp(27) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(27) : error C2228: left of '.xh' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(27) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(27) : error C2228: left of '.name' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(27) : error C2109: subscript requires array or pointer type
D:\zy\C++编程作业\my空间\Cpp1.cpp(27) : error C2228: left of '.cj' must have class/struct/union type
D:\zy\C++编程作业\my空间\Cpp1.cpp(37) : error C2143: syntax error : missing ';' before '['
D:\zy\C++编程作业\my空间\Cpp1.cpp(37) : error C2143: syntax error : missing ';' before '['
D:\zy\C++编程作业\my空间\Cpp1.cpp(43) : error C2065: 'set' : undeclared identifier
D:\zy\C++编程作业\my空间\Cpp1.cpp(46) : error C2065: 'cx' : undeclared identifier
执行 cl.exe 时出错.

Cpp1.obj - 1 error(s), 0 warning(s)
...全文
238 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
web518 2007-03-17
  • 打赏
  • 举报
回复
主要的几个错误是:
char* 即字符数组不能直接比较,要使用strcmp
类定义对像时不是用class,而是用类名
对在类成员函数里边访问类成员变量(应该不能访问其它类对像吧),因为类定义对像后。每个类对像都有对类成员变量的一份拷贝。你直接在类成员函数中对对像遍历我总觉的不妥)
web518 2007-03-17
  • 打赏
  • 举报
回复
不知道你要干什么,但是在类里边访问类的对像好像不妥吧,类里访问类成员。好久没搞C++了。你的程序我修改了一下。看是不是和你的要求一样。
当然这个查询和输入有更好的算法。为了尽量不修改你的程序。所以做成了这样。

#include<iostream>
#include <string.h>
using namespace std;

class student
{
private:
char xh[10],name[20];
float cj;
public:
void cx(char *xhcx)
{
if(strcmp(xh,xhcx) == 0) // 这里是错在哪里???
{
cout<<"xue sheng xin xi:"<<endl<<"xh name cj "<<endl<<xh<<" "<<name<<" "<<cj<<" ";
}
}
void set()
{
cout<< "shu ru xue sheng qi ben xin xi:\n";
cout<<"xh name cj";
cin>>xh>>name>>cj;

}

};
student xs[10];


int main()
{
int i;
char xhao[10];

for(i=1;i<10;i++)
xs[i].set();

cout<<"shu ru ni yao cha xu de xh:\n";
cin>>xhao;
for(i=1;i<10;i++)
xs[i].cx(xhao);
return 0;
}

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧