知了博客

集天地之精华,吸日月之灵气

« 这半个月the uml of gof »

理解class与struct申明

出现:
1>Compiling...
1>stdafx.cpp
1>gof.cpp
1>e:\projects\stu\gof\gof\bridge.h(62) : error C2512: 'CCustomerDataObject' : no appropriate default constructor available
1>e:\projects\stu\gof\gof\bridge.h(68) : warning C4150: deletion of pointer to incomplete type 'CDataObject'; no destructor called
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(74) : error C2027: use of undefined type 'CDataObject'
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(74) : error C2227: left of '->NextRecord' must point to class/struct/union/generic type
1>e:\projects\stu\gof\gof\bridge.h(78) : error C2027: use of undefined type 'CDataObject'
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(78) : error C2227: left of '->PriorRecord' must point to class/struct/union/generic type
1>e:\projects\stu\gof\gof\bridge.h(82) : error C2027: use of undefined type 'CDataObject'
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(82) : error C2227: left of '->AddRecord' must point to class/struct/union/generic type
1>e:\projects\stu\gof\gof\bridge.h(86) : error C2027: use of undefined type 'CDataObject'
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(86) : error C2227: left of '->DeleteRecord' must point to class/struct/union/generic type
1>e:\projects\stu\gof\gof\bridge.h(90) : error C2027: use of undefined type 'CDataObject'
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(90) : error C2227: left of '->ShowRecord' must point to class/struct/union/generic type
1>e:\projects\stu\gof\gof\bridge.h(95) : error C2027: use of undefined type 'CDataObject'
1> e:\projects\stu\gof\gof\bridge.h(55) : see declaration of 'CDataObject'
1>e:\projects\stu\gof\gof\bridge.h(95) : error C2227: left of '->ShowAllRecords' must point to class/struct/union/generic type
代码如下:
interface CDataObject;
class CCustomerDataObject;
class CCustomersBase
{
public:
CCustomersBase(string group)//:_dataObject(NULL)
{
_dataObject = new CCustomerDataObject();
this->group = group;
}
virtual~CCustomersBase()
{
if(_dataObject != NULL)
delete _dataObject;
}

public:
virtual void Next()
{
_dataObject->NextRecord();
}
virtual void Prior()
{
_dataObject->PriorRecord();
}
virtual void Add(string customer)
{
_dataObject->AddRecord(customer);
}
virtual void Delete(string customer)
{
_dataObject->DeleteRecord(customer);
}
virtual void Show()
{
_dataObject->ShowRecord();
}
virtual void ShowAll()
{
cout<<"Customer Group: " << group< _dataObject->ShowAllRecords();
}

private:
CDataObject *_dataObject;
protected:
string group;
};
class CCustomerObject:public CCustomersBase
{
public:
CCustomerObject(string group="jsss"):CCustomersBase(group)
{
}
public:
// Add separator lines
virtual void ShowAll()
{
cout<<"------------------------"< //CCustomersBase::ShowAll();
cout<<"------------------------"< }
};
interface CDataObject
{
public:
virtual void NextRecord() = 0;
virtual void PriorRecord() = 0;
virtual void AddRecord(string name) = 0;
virtual void DeleteRecord(string name) = 0;
virtual void ShowRecord() = 0;
virtual void ShowAllRecords() = 0;
};

class CCustomerDataObject:public CDataObject
{
public:
CCustomerDataObject():_current(0)
{
_customers.push_back("Jim Jones");
_customers.push_back("Samual Jackson");
_customers.push_back("Allen Good");
_customers.push_back("Ann Stills");
_customers.push_back("Lisa Giolani");
}
void NextRecord()
{
if (_current <= _customers.size() - 1)
{
_current++;
}
}

void PriorRecord()
{
if (_current > 0)
{
_current--;
}
}

void AddRecord(string customer)
{
_customers.push_back(customer);
}

void DeleteRecord(string customer)
{
_customers.pop_back();
}

void ShowRecord()
{
cout<<_customers[_current]< }

void ShowAllRecords()
{
for(vector::iterator it = _customers.begin(); it != _customers.end(); it++ )
cout<<" "<<*it< }
public:
vector _customers;
int _current;
};

类似为:interface A;
class B;
class C
{
public:
C(){a = new B;//这里编译不通过,应该是class B与interface只是申明
}
A* a;
};
interface A
{
public:
A(){}
};
class B:public A
{
public:
B(){}
};
int main(int argc, char* argv[])
{ C c;c.a = new B;
}
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619 Code detection by Codefense  theme by BokeZhuti

Copyright know blog. Some Rights Reserved.站长(msn):webmaster#webgou.info(#换成@) 粤ICP备09183716号