静态模板constexpr错误
static template constexpr error
嗨,我有以下类:"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;
但我得到了以下错误:
- Verification.cpp|16|错误:"this"不是常量表达式|
- Verification.cpp|16|注意:在类型"long int"的模板参数中|
- 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);
相关文章:
- Clang 8 带有静态 constexpr 和数组的链接器错误 - 错误是什么以及如何解决它?
- NVCC 错误:string_view.h:constexpr 函数返回是非常量
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 奇怪的错误 C2131 与 constexpr 声明变量
- 在if constexpr中使用带参数包的概念时,升级到gcc 9后出现编译错误
- 对'if constexpr'的错误理解
- 这是通过初始化 constexpr 变量来标记编译错误的合理跳转
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- emplace_back会导致静态 constexpr 成员上出现链接错误
- 在 Mac OS x 中制造期间出现错误未知类型名称"constexpr"
- 为什么我收到此错误:constexpr' 在这里无效
- 文件中定义的 Constexpr 构造函数.cpp会导致链接错误
- "如果 constexpr",在 lambda 内部,在包扩展内部 - 编译器错误?
- 表达式未评估为常数两个级别的constexpr函数(编译器错误?)
- 错误!Constexpr变量必须通过常数表达式constexpr初始化
- 使用constexpr c-string作为编译器错误消息
- 如何在 constexpr 函数中强制编译错误,而不是让它衰减到非 constexpr 上下文中?
- gcc 和 clang 中 constexpr 静态成员变量的链接器错误
- 为什么我的编译器显示有关我的 constexpr 函数的此错误?
- MSVC 错误,将 constexpr 数组作为模板非类型参数