有关vector ,实在不知道什么问题了!!请指教!
//原码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Observer;
//////////////////////////////////////////////////////////////////////////
// Class Customer
class Customer {
public:
static void Attach(Observer *ob);
static void Detach(Observer *ob);
string GetState();
private:
vector <Observer *> m_myObs;
public:
void NotifyObs();
};
void Customer::Attach(Observer *ob)
{
m_myObs.push_back(ob);
//m_myObs.push_back(ob);
}
void Customer::Detach(Observer *ob)
{
vector <Observer *> ::iterator iter =
find(m_myObs.begin(), m_myObs.end(), ob);
if (iter != m_myObs.end())
m_myObs.erase(iter);
}
void Customer::NotifyObs()
{
for (vector <Observer *> ::iterator iter = m_myObs.begin();
iter != m_myObs.end(); ++iter)
{
iter-> Update(this);
}
}
string Customer::GetState()
{
string str;
cout < < "GetState()............ " < < endl;
return str;
}
//////////////////////////////////////////////////////////////////////////
// class Observer
class Observer {
public:
Observer();
virtual void Update(Customer *mycust) = 0;
};
Observer::Observer()
{
Customer::Attach(this);
}
//////////////////////////////////////////////////////////////////////////
//class AddVerification
class AddVerification : public Observer {
public:
AddVerification();
void Update(Customer *mycust);
};
AddVerification::AddVerification()
{
}
void AddVerification::Update(Customer *mycust)
{
cout < < "Do Address verification stuff here...... " < < endl;
}
//////////////////////////////////////////////////////////////////////////
//WelcomeLetter
class WelcomeLetter : public Observer {
public:
WelcomeLetter();
void Update(Customer *mycust);
};
WelcomeLetter::WelcomeLetter()
{
}
void WelcomeLetter::Update(Customer *mycust)
{
cout < < "Do Welcome Letter stuff here...... " < < endl;
}
//////////////////////////////////////////////////////////////////////////
void main()
{
WelcomeLetter letter;
AddVerification verify;
Customer cust1;
cust1.NotifyObs();
}
编译错误:
:\myproject\design pattern\observer\main.cpp(26) : error C2228: left of '.push_back ' must have class/struct/union type
d:\myproject\design pattern\observer\main.cpp(33) : error C2228: left of '.begin ' must have class/struct/union type
d:\myproject\design pattern\observer\main.cpp(33) : error C2228: left of '.end ' must have class/struct/union type
d:\myproject\design pattern\observer\main.cpp(34) : error C2228: left of '.end ' must have class/struct/union type
d:\myproject\design pattern\observer\main.cpp(35) : error C2228: left of '.erase ' must have class/struct/union type
d:\myproject\design pattern\observer\main.cpp(43) : error C2227: left of '-> Update ' must point to class/struct/union
Error executing cl.exe.
各位帮忙啊,谢谢!
[解决办法]
public:
static void Attach(Observer *ob);
static void Detach(Observer *ob);
string GetState();
private:
vector <Observer *> m_myObs;
静态成员函数访问实例对象??
让m_myObs也是静态的吧
static vector <Observer *> m_myObs;
类外
vector <Observer*> Customer::m_myObs;
[解决办法]
1.
void Customer::Attach(Observer *ob)
{
m_myObs.push_back(ob);
.............................
void Customer::Detach(Observer *ob)
{
//以上是静态成员函数, 不能访问非静态成员变量m_myObs
2.
void Customer::NotifyObs()
{
............................
iter-> Update(this); //应该是(*iter)-> Update(this)
..............................
}
[解决办法]
另外我感觉你的类声明组织的也有问题,最好把声明都放在最前面
class Observer;
class Customer {
public:
static void Attach(Observer *ob);
static void Detach(Observer *ob);
string GetState();
private:
vector <Observer *> m_myObs;
public:
void NotifyObs();
};
class Observer {
public:
Observer();
virtual void Update(Customer *mycust) = 0;
};
class AddVerification : public Observer {
public:
AddVerification();
void Update(Customer *mycust);
};
class WelcomeLetter : public Observer {
public:
WelcomeLetter();
void Update(Customer *mycust);
};
[解决办法]
void Customer::NotifyObs()
{
............................
iter-> Update(this); //应该是(*iter)-> Update(this)
..............................
}