C++ ';std::超出范围';在虚拟功能中

C++ ';std::超出范围';在虚拟功能中,c++,C++,我试图访问一个虚拟函数的元素,该函数在类1中声明,然后在类2中定义。我知道std::out_of_range错误是内存访问问题,但我不理解代码main()中访问值的问题。 调用函数**m->function(t,j)**时,我无法访问*parmem*的元素,但如果我直接调用函数的输出,它就会工作:**parmem.at(1)。伽马**。代码如下: 第1类: #include <armadillo> #include <iostream> using namespace s

我试图访问一个虚拟函数的元素,该函数在类1中声明,然后在类2中定义。我知道std::out_of_range错误是内存访问问题,但我不理解代码main()中访问值的问题。 调用函数**m->function(t,j)**时,我无法访问*parmem*的元素,但如果我直接调用函数的输出,它就会工作:**parmem.at(1)。伽马**。代码如下:

第1类:

#include <armadillo>
#include <iostream>
using namespace std;
using namespace arma;

class Class1
{
public:
mat Y;        

struct Par
{
mat gamma;
} par;
std::vector<Par> parmem ;  
virtual double function( const int t, const int j ) = 0;  
};
#包括
#包括
使用名称空间std;
使用arma;
一班
{
公众:
材料Y;
结构参数
{
matγ;
};
std::向量parmem;
虚双函数(常数int t,常数int j)=0;
};
第2类:

class Class2 : public Class1
{
public:

virtual double function( const int t, const int j );   
};


double Class2::function( const int t, const int j )
{
    cout << parmem.at(t).gamma << endl;
    return j+t;
}
类别2:公共类别1
{
公众:
虚双函数(const int t,const int j);
};
双类2::函数(常量int t,常量int j)
{

cout在以下几行中,您可以创建独立的对象:

Class2 *m = new Class2;

std::vector<Class1::Par> parmem {
    {Y},
    {2*Y}
    };

你能把你的问题转换成英语吗?我很确定你的问题是'm'的'parmem'中没有包含任何值,因此'at'函数失败。你在哪里设置
m
parmem
成员的值?我看不到任何地方。另外,请在代码中使用缩进,以便我们可以轻松地阅读它很好。
Class2 *m = new Class2;

std::vector<Class1::Par> parmem {
    {Y},
    {2*Y}
    };
int main()
{
mat Y=randu<mat>(3,3);

int t=1;
int j=1; 

Class2 *m = new Class2;

std::vector<Class1::Par> parmem {
    {Y},
    {2*Y}
    };

m->parmem = paramen;

cout << parmem.at(1).gamma << endl; //funciona
cout << m->function(t,j) << endl;  //no funciona
return 0; 
}