读书人

问下这个有关问题

发布时间: 2012-02-12 17:16:34 作者: rapoo

问下这个问题
#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)


[解决办法]
不知道你要干什么,但是在类里边访问类的对像好像不妥吧,类里访问类成员。好久没搞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;
}
[解决办法]
主要的几个错误是:
char* 即字符数组不能直接比较,要使用strcmp
类定义对像时不是用class,而是用类名
对在类成员函数里边访问类成员变量(应该不能访问其它类对像吧),因为类定义对像后。每个类对像都有对类成员变量的一份拷贝。你直接在类成员函数中对对像遍历我总觉的不妥)

读书人网 >C++

热点推荐