我试图从类View中的方法调用静态函数AreaMap :: staticInitialize(Model *)。它在我首先定义类AreaMap时进行编译,但是当我尝试首先声明View时,即使我转发声明的AreaMap,我也会得到错误。有人知道如何将View的定义保持在最顶层吗?
#ifndef VIEW_H
#define VIEW_H
#include "Model.h"
class AreaMap;
class View {
public:
void linkMvc(Model * m) {
model = m;
AreaMap::staticInitialize(m);
}
Model * model;
};
class AreaMap {
public:
void static staticInitialize(Model * m) {
model = m;
}
Model * model;
};
#endif
错误:
inc/View.hpp: In member function ‘void View::linkMvc(Model*, Controller*)’:
inc/View.hpp:36:7: error: incomplete type ‘AreaMap’ used in nested name specifier
AreaMap::staticInitialize(m);
答案 0 :(得分:0)
#ifndef VIEW_H
#define VIEW_H
class AreaMap;
class View {
public:
void linkMvc(Model * m);
Model * model;
};
class AreaMap {
public:
void static staticInitialize(Model * m) {
model = m;
}
Model * model;
};
void View::linkMvc(Model * m) {
model = m;
AreaMap::staticInitialize(m);
}
#endif
然而,这仍然不应该编译。 staticInitialize()
尝试修改非静态成员变量。
答案 1 :(得分:0)
class AreaMap; <----- This won't suffice.
class View {
public:
void linkMvc(Model * m) { <----- What's Model?
model = m;
AreaMap::staticInitialize(m); <----- What's this?
}
Model * model;
};
这里有两个问题:
Model
?你应该向前宣布它。AreaMap
的前瞻声明不足以致电AreaMap::staticInitialize
。您需要在引用之前声明该函数。