[英]Function Pointer inside Class (expression preceding parentheses of apparent call must have (pointer-to-) function type)
[英]Expression preceding parentheses must have pointer-to- function type when using functors
因此,正如標題所述,我正在努力為我的游戲服務器地圖類使用仿函數。 我定義了以下模板類來表示扇形3D地圖:
template <typename T>
class matrix3d {
public:
matrix3d(uint16_t XMin, uint16_t XMax, uint16_t YMin, uint16_t YMax, uint8_t ZMin, uint8_t ZMax);
T* operator() (uint16_t x, uint16_t y, uint8_t z);
private:
uint16_t xmin, xmax;
uint16_t ymin, ymax;
uint8_t zmin, zmax;
int16_t dx, dy;
int8_t dz;
T* Entry; // This is an array that I new() in the class constructor.
};
在服務器啟動時,我new
了一個全局實例,該實例將保存映射matrix3d<TSector *> *g_Map
( TSector
是一個結構,其中包含一個二維的32x32瓦片圖塊及其標志和填充物)。
我決定重載()
運算符,以從地圖文件中獲取相應的坐標來檢索TSector *:
template<typename T>
T* matrix3d<T>::operator() (uint16_t x, uint16_t y, uint8_t z) {
uint16_t xx = x - xmin;
uint16_t yy = y - ymin;
uint8_t zz = z - zmin;
if (xx >= 0 && xx < dx
&& yy >= 0 && yy < dy
&& zz >= 0 && zz < dz)
return &Entry[xx + dy * dx * zz + dx * yy];
error("matrix3d::operate: Unexpected Index %d/%d/%d.\n", x, y, z);
return Entry;
}
因此,我的問題在於編譯此函數時: LoadSector(filename, x, y, z)
,每個扇區文件都會被調用(我有大約10.000個文件),並從g_Map
檢索相應的扇區以存儲解析后的圖塊內容:
void LoadSector(const char* FileName, uint16_t x, uint16_t y, uint8_t z) {
TSector* sector = g_Map(x, y, z); // My actual problems is here.
// BEGIN PARSING.
}
VS Code表示:“明顯調用的括號前的表達式必須具有(指針到)函數類型”。 g ++說:g_Map不能用作函數。
g_Map
是指向 matrix3d
的指針 。 為了在那個matrix3d
對象上調用operator()
,您需要首先取消引用指針:
TSector* sector = (*g_Map)(x, y, z);
等效於:
TSector* sector = (*g_Map).operator()(x, y, z);
或者:
TSector* sector = g_Map->operator()(x, y, z);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.