作为类成员和方法中的不同向量初始化背后的原因是什么? 我试图在C++设计背后的逻辑下进行尝试。这是我的问题。 在以下代码中,为什么我不能将其作为类成员启动

作为类成员和方法中的不同向量初始化背后的原因是什么? 我试图在C++设计背后的逻辑下进行尝试。这是我的问题。 在以下代码中,为什么我不能将其作为类成员启动,c++,C++,矢量fhd(10) 但我可以用一种方法开始 载体abc(10) 第一种方法会不会给编译器带来混乱 这是完整的代码 #include <iostream> #include <vector> using namespace std; class testVector{ private: int vl; //error reported below vector<int> fhd(10); public: testVector(in

矢量fhd(10)

但我可以用一种方法开始

载体abc(10)

第一种方法会不会给编译器带来混乱

这是完整的代码

#include <iostream>
#include <vector>

using namespace std;

class testVector{

private:
    int vl;
 //error reported below
    vector<int> fhd(10);
public:
    testVector(int vl){
        this->vl=vl;
    }

    vector<int> assVal(){
        //OK below
        vector<int> abc(10);

        cout << "haha" << endl;
        for(int iter=0; iter < vl; iter++){
            fhd.push_back(iter);
        }
        return fhd;
    }

    void showVec(){
        for(int iter=0; iter < fhd.size(); iter++){
            cout<< fhd[iter] << endl;
        }
    }


};

int main() {


    std::cout << "Hello, World!" << std::endl;

    testVector cj(5);

    cj.assVal();
    cj.showVec();
    return 0;
}
#包括
#包括
使用名称空间std;
类测试向量{
私人:
int vl;
//错误报告如下
矢量fhd(10);
公众:
测试向量(int vl){
这->vl=vl;
}
向量assVal(){
//好的
载体abc(10);

你想让向量的大小固定为10吗?如果是这样,你可能会更喜欢

std::array<int, 10> fhd;
std::阵列fhd;
如果希望它是一个向量,那么可以使用如下语法为它指定一个默认值

vector<int> fhd = std::vector<int>(10);
向量fhd=std::向量(10);
您可以使用
向量fhd=vector(10)但是,为什么C++不允许在一个方法中启动它?C++开发背后的逻辑是什么?为了避免任何可能产生最令人烦恼的解析失败的可能性。在那里有重复的东西,但是我现在似乎找不到它们。我理解这一点。但是为什么它不允许像智慧那样启动它。一个方法?C++开发背后的逻辑是什么?多谢!
vector<int> fhd = std::vector<int>(10);