求救···VC++。NET中类的组合问题!
#include"stdafx.h"#ifndef HYBCLASS_H
#define HYBCLASS_H
#include <iostream>
using namespace std;
class Component
{
private:
int i;
public:
Component() : i(1) {cout<<"调用Component缺醒构造函数"<<endl;}
Component(int N) : i(N) {cout<<"调用Component构造函数"<<endl;}
~Component() {cout<<"调用Componen析构函数"<<endl;}
int Get() const {return i;}
void Double() {i*=2;}
};
class Base
{
private:
int j;
public:
Base(): j(3) {cout<<"调用Base缺醒构造函数"<<endl;}
Base(int N): j(N) {cout<<"调用Base构造函数"<<endl;}
~Base() {cout<<"调用Base析构函数"<<endl;}
int Set(int N) {j=N;}
int Get() const {return j;}
void Double() {j*=2;}
};
class Hybird : public Base
{
private:
int k;
Component C1,C2;
public:
Hybrid(): k(1),C1(3),C2(4),Base(2) {cout<<"调用Hybrid缺醒构造函数"<<endl;}
Hybrid(int L,int M,int N,int P) : k(L),Base(M),C1(N),C2(P) {cout<<"调用Hybrid构造函数"<<endl;}
~Hybrid() {cout<<"调用Hybrid析构函数"<<endl;}
int Get() const {return k;}
void Double() {k*=2;}
void DoubleBase() {Base::Double();}
void DoubleComp() {C1.Double();C2.Double();}
void GetBase() {return Base::Get();}
int GetC1() {return C1.Get();}
int GetC2() {return C2.Get();}
};
#endif
//---主程序----
#include"stdafx.h"
#include "HyBClass.h"
#include<iostream>
int main()
{
cout<<"sizeof(Component)/sizeof(int): "<<sizeof(Component)/sizeof(int) <<endl;
cout<<"sizeof(Base)/sizeof((int): "<<sizeof(Base)/sizeof(int) <<endl;
cout<<"sizeof(Hybrid)/sizeof(int): "<<sizeof(Hybrid)/sizeof(int) <<endl;
cout<<"执行Hybrid x之后:"<<endl;
Hybrid x;
cout<<"x.Get() : "<<x.Get() <<endl;
cout<<"x.GetBase() : "<<x.GetBase() <<endl;
cout<<"x.GetC1() : "<<x.GetC1() <<endl;
cout<<"x.GetC2() : "<<x.GetC2() <<endl;
cout <<"执行x.Double()之后:"<<endl;
x.Double();
cout<<"x.Get() : "<<x.Get() <<endl;
cout<<"x.GetBase() : "<<x.GetBase() <<endl;
cout<<"x.GetC1() : "<<x.GetC1() <<endl;
cout<<"x.GetC2() : "<<x.GetC2() <<endl;
cout <<"执行x.DoubleBase()之后:"<<endl;
x.DoubleBase();
cout<<"x.Get() : "<<x.Get() <<endl;
cout<<"x.GetBase() : "<<x.GetBase() <<endl;
cout<<"x.GetC1() : "<<x.GetC1() <<endl;
cout<<"x.GetC2() : "<<x.GetC2() <<endl;
cout <<"执行x.DoubleComp()之后:"<<endl;
x.DoubleComp();
cout<<"x.Get() : "<<x.Get() <<endl;
cout<<"x.GetBase() : "<<x.GetBase() <<endl;
cout<<"x.GetC1() : "<<x.GetC1() <<endl;
cout<<"x.GetC2() : "<<x.GetC2() <<endl;
cout <<"执行Hybrid(7,8,9,10)之后:"<<endl;
cout<<"y.Get() : "<<y.Get() <<endl;
cout<<"y.GetBase() : "<<y.GetBase() <<endl;
cout<<"y.GetC1() : "<<y.GetC1() <<endl;
cout<<"y.GetC2() : "<<y.GetC2() <<endl;
return 0;
}
显示的错误是:
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : error C2327: “Hybird::k”: 不是类型名称、静态或枚举数
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : error C3861: “k”: 找不到标识符
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : error C2656: “Hybird::Hybrid”: 函数不能作为位域
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : warning C4183: “Hybrid”: 缺少返回类型;假定为返回“int”的成员函数
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\hybclass.h(35) : error C2334: “{”的前面有意外标记;跳过明显的函数体
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\组合1\组合1\组合1.cpp(41) : fatal error C1004: 发现意外的文件尾
望各位高手能指点指点啊!···