首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么声明一个对象数组为全局变量

2013-07-09 
如何声明一个对象数组为全局变量#include iostream#include fstream#include algorithmusing namesp

如何声明一个对象数组为全局变量

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
B k[2];
class A
{
public:
A(int a,int b)
{
x=a;y=b;
}
A(){x=1;y=1;}
A(A &i)
{
x=i.x;
y=i.y;
}
~A(){}

/* data */
int x;
int y;
};

class B:public A
{
public:
B(int a,int b,int c):A(a,b),e(c){}
B():A(),e(2){}
B(int a,int b):A(),e(2){}
B(A &s)
{
x=s.x;
y=s.y;
e=2; 
}
~B(){}

/* data */
int e;
};

void readFile(char *filename)
{
fstream binaryIO;
binaryIO.open(filename,ios::in|ios::binary);
binaryIO.read(reinterpret_cast<char *>(&k[0]),sizeof(A));
binaryIO.read(reinterpret_cast<char *>(&k[1]),sizeof(A));
binaryIO.close();

cout<<k[0].x<<" "<<k[0].y<<" "<<k[0].e<<endl;
cout<<k[1].x<<" "<<k[1].y<<" "<<k[1].e<<endl;
}

int main()
{
A a(10,14);
A c(12,13);
A d(56,34);

fstream binaryIO;
//从文件读入数据
binaryIO.open("A.dat",ios::out|ios::binary);
binaryIO.write(reinterpret_cast<char *>(&a),sizeof(A));
binaryIO.write(reinterpret_cast<char *>(&c),sizeof(A));
binaryIO.write(reinterpret_cast<char *>(&d),sizeof(A));
binaryIO.close();

readFile("A.dat");
system("pause");
return 0;
}

编译器提示:`B' does not name a type。求解 全局变量 对象
[解决办法]
把B k[2];放在第43行后
[解决办法]
class StudentMIS:public First,public Second
{
public:
    StudentMIS();
    void prinfScore(int num);
    void writeGood(char *filename);
    void writeFail(char *filename);


};//这里少个分号

热点排行