不明白这个错误:初始化"类"的参数 1 [使用 T = 无符号 int]' [-允许]

Don't understand this error: initializing argument 1 of 'class' [with T = unsigned int]' [-fpermissive]

本文关键字:无符号 允许 int 使用 错误 明白 初始化 参数      更新时间:2023-10-16

这是我在 Eclipse 中构建时出现的 3 个错误中的第一个。

在 main 函数中,我只是实例化类 multidimensional_vector<1,无符号 int>这是一个模板。

奇怪的是,当我将第 14 行 push_back() 中的参数 w 更改为 *w 时,错误消失了。但我刚刚读到push_back指针是不行的,所以我想知道是否可以使用 *w 作为 push_back() 的参数?

代码:

#include <vector>
#include <stdlib.h>
using std::vector;
enum Foo { length = 4, heigth = 4};
template<unsigned int dimcount, typename T>
class multidimensional_vector
{
    private: vector< multidimensional_vector<dimcount-1, T> > wector;
    public:
    multidimensional_vector() {}
    multidimensional_vector(T a) {
        for (int var = 0; var <= heigth; ++var) {
            multidimensional_vector<dimcount-1,  T> *w = new multidimensional_vector<dimcount-1,  T>(a);
            wector.push_back(w);
        }
    }
    T getValue(vector<unsigned int> v){
        return wector[v[dimcount]].getValue(v);
    }
    void setValue(vector<unsigned int> v, T value){
        wector[v[dimcount]].setValue(v, value);
    }
};
template<typename T>
class multidimensional_vector<0,T>
{
    private: T value;
    public:
    multidimensional_vector() {}
    multidimensional_vector(T a) {
        value = a;
    }
    T getValue(vector<unsigned int> v){
        return value;
    }
    void setValue(vector<unsigned int> v, T value){
        this->value = value;
    }
};

int main() {
    multidimensional_vector<1,  unsigned int> *nimarray = new multidimensional_vector<1,  unsigned int>(0);
    return 0;
}

当尝试从问题中编译代码片段时,gcc 5.3.1 报告的唯一错误是:

t.C:12:34: error: ‘heigth’ was not declared in this scope
         for (int var = 0; var <= heigth; ++var) {

GCC 不会报告您询问的任何错误。

手动将此变量的声明添加到类后,给定的代码编译没有任何问题。

除了缺少类成员声明之外,给定的代码绝对没有问题。