所有
下面是我的头文件以及.cpp文件。我在Visual Studio 2012中构建项目时收到此错误。
“错误C2065:'model':未声明的标识符”
这是我的头文件:
// ObserverPattern.h
//
#include <iostream>
#include <vector>
using namespace std;
#ifndef _OBSERVER_PATTERN_
#define _OBSERVER_PATTERN_
class Subject {
private:
vector < class Observer * > views;
int value;
public:
__declspec(dllexport) void attach(Observer *obs);
__declspec(dllexport) void setVal(int val);
__declspec(dllexport) int getVal();
__declspec(dllexport) void notify();
};
class Observer {
public:
Subject *model;
int denom;
protected:
__declspec(dllexport) Subject *getSubject();
__declspec(dllexport) int getDivisor();
public:
__declspec(dllexport) Observer(Subject *mod, int div);
__declspec(dllexport) virtual void update();
};
#endif
此外,这是我的.cpp文件:
// ObserverPattern.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
#include "ObserverPattern.h"
using namespace std;
void Subject::attach(Observer *obs) {
views.push_back(obs);
}
void Subject::setVal(int val) {
value = val;
notify();
}
int Subject::getVal() {
return value;
}
Observer::Observer(Subject *mod, int div) {
model = mod;
denom = div;
model->attach(this);
}
Subject Observer::*getSubject() {
return model;
}
int Observer::getDivisor() {
return denom;
}
void Subject::notify() {
for (int i = 0; i < views.size(); i++)
views[i]->update();
}
class DivObserver: public Observer {
public:
DivObserver(Subject *mod, int div): Observer(mod, div){}
void update() {
int v = getSubject()->getVal(), d = getDivisor();
cout << v << " div " << d << " is " << v / d << '\n';
}
};
class ModObserver: public Observer {
public:
ModObserver(Subject *mod, int div): Observer(mod, div){}
void update() {
int v = getSubject()->getVal(), d = getDivisor();
cout << v << " mod " << d << " is " << v % d << '\n';
}
};
int main() {
Subject subj;
DivObserver divObs1(&subj, 4);
DivObserver divObs2(&subj, 3);
ModObserver modObs3(&subj, 3);
subj.setVal(14);
system ("pause");
}
有人可以帮我解决“模型”变量的“未声明标识符”问题吗?