静态模板constexpr错误

static template constexpr error

本文关键字:错误 constexpr 静态      更新时间:2023-10-16

嗨,我有以下类:"Verification.h"


#ifndef VERIFICATION_H
#define VERIFICATION_H
#include <vector>
#include <string>
#include <dlib/svm.h>
using namespace dlib;
using namespace std;

class Verification
{
    public:
        Verification(std::string, std::vector<string>,const int);
        virtual ~Verification();
        void Verify();
    private:
        std::vector<std::string> groundTruth;
        std::string path;
        const int rows;
};
#endif // VERIFICATION_H

验证.cpp

#include "Verification.h"
Verification::Verification(string p, std::vector<string> gt,const int r):path(p),groundTruth(gt),rows(r)
{
}
Verification::~Verification()
{
    //dtor
}
void Verification::Verify()
{
    //Load ground truth and build matrix
    typedef matrix<double, rows, 9> sample_type;
    typedef radial_basis_kernel<sample_type> kernel_type;

}

问题:我正在尝试初始化:

 typedef matrix<double, 9,1> sample_type;

但我得到了以下错误:

  1. Verification.cpp|16|错误:"this"不是常量表达式|
  2. Verification.cpp|16|注意:在类型"long int"的模板参数中|
  3. Verification.cpp|16|错误:";"之前的声明中的类型无效代币|

我该怎么解决?

谢谢。


基于MM:答案的编辑

  //Load ground truth and build matrix
    typedef matrix<double> data;
    data data_type;
    data_type.set_size(9,1);

变量rows在编译时应该是已知的。

如果您想使用常数而不是数字,可以将其设为static constexpr,例如:

static constexpr int rows = 1;

 

typedef matrix<double> data;

它声明了一个名为data的新类型,类型为matrix<double>,而不是对象。试试这个:

typedef matrix<double> data_type;
data_type data;
data.set_size(rows,9);

此行

typedef matrix<double> data;

定义一个名为data的类型(这是一个非常糟糕的类型名称)。

很可能您只想删除typedef:

matrix<double> data;
data.set_size(rows,9);