【发布时间】:2014-01-31 04:01:05
【问题描述】:
我正在尝试构建一个 MapV2 类。在课堂上,我希望有一个 Cell 对象数组作为私有成员(Cell 是另一个类)。我正在尝试获取它,以便地图的大小由与构造函数一起使用的模板参数分配。即,我正在尝试获得类似于以下内容的内容:
const size_t arraySize = 12;
MapV2<arraySize> myMapV2;
这是我的文件 Map.h:
#pragma once
#include <iostream>
#include "Cell.h"
template<size_t M, size_t N>
class MapV2
{
public:
MapV2();
~MapV2();
private:
Cell myMapV2[M*N];
};
这里是 Map.cpp:
#include <iostream>
#include "MapV2.h"
MapV2<size_t M, size_t N>::MapV2()
{
}
MapV2::~MapV2()
{
}
这里是主要功能:
int main()
{
const size_t xLength = 6;
const size_t yLength = 8;
MapV2 <xLength, yLength> Example;
return 0;
}
但是当我尝试编译时,我得到以下一团糟的错误:
Compiling: MapV2.cpp
D:\Users\Vik\ModSim1a\MapV2.cpp:4: error: wrong number of template arguments (1, should be 2)
D:\Users\Vik\ModSim1a\MapV2.h:7: error: provided for 'template<unsigned int M, unsigned int N> class MapV2'
D:\Users\Vik\ModSim1a\MapV2.cpp: In function 'int MapV2()':
D:\Users\Vik\ModSim1a\MapV2.cpp:4: error: 'int MapV2()' redeclared as different kind of symbol
D:\Users\Vik\ModSim1a\MapV2.h:7: error: previous declaration of 'template<unsigned int M, unsigned int N> class MapV2'
D:\Users\Vik\ModSim1a\MapV2.cpp:7: warning: no return statement in function returning non-void
D:\Users\Vik\ModSim1a\MapV2.cpp: At global scope:
D:\Users\Vik\ModSim1a\MapV2.cpp:9: error: expected constructor, destructor, or type conversion before '::' token
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 1 warnings
我已经用谷歌搜索过这个问题,并花了一些时间尝试遵循类似 StackOverflow 帖子中给出的建议,但是没有一个示例说明在代码中为实际构造函数(即 MapV2.cpp 文件)做什么来获取这工作。我觉得这些错误很容易解决。非常感谢任何帮助。
【问题讨论】: