未解析的外部符号结构

unresolved external symbol structs

本文关键字:符号 结构 外部      更新时间:2023-10-16

错误LNK2001:未解析的外部符号"public: static int WrappedVector::_N" (?_N@WrappedVector@@2HA)

标题.h

struct WrappedVector
{
    static int _N;
    double *_x;
};

主.cpp

const int WrappedVector::_N = 3;

我不明白怎么了

只需更改定义

 int WrappedVector::_N = 3; // Note no const

查看现场演示1

或声明

 struct WrappedVector {
    static const int _N;
        // ^^^^^
    double *_x;
 };

查看现场演示2

一贯。

如果您需要后一种形式(static const int),您也可以直接在声明中初始化它:

 struct WrappedVector {
    static const int _N = 3;
                     // ^^^
    double *_x;
 };

查看现场演示3