c++ - 为什么这样写是错误的??
高洛峰
高洛峰 2017-04-17 13:50:36
0
2
530

为什么这样写的话,编译器会提示vecsize is not a type

#include <iostream>
#include <vector>
struct A
{
    static const int vecSize = 20;
    static std::vector<double> vec(vecSize);
};

但如果改成这样就没问题

#include <iostream>
#include <vector>
struct A
{
    static const int vecSize = 20;
    static std::vector<double> vec;
};
std::vector<double> A::vec(vecSize);
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
左手右手慢动作

Because

std::vector<double> vec(vecSize);

is regarded as a function signature, and the return value is std::vector<double>, so an error occurs when parsing vecSize.
In addition, static non-literal type members in a class need to be initialized outside the class, which is stipulated by C++.

左手右手慢动作

The process of constructing the object type member variable vec must be in the constructor and cannot be constructed when it is declared.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template