C++ 模板类中变量类型的向量容器

C++ 模板类中变量类型的向量容器,c++,templates,C++,Templates,我有一个带有一个变量数据类型的模板类template。 在类中,我有一个私有变量vectoroutputData。 因此,输出意味着复杂或向量。我有一个构造函数,其中一个参数是intn,给出了outputData向量的大小。在这个构造函数中,我初始化outputData如下: ouputData.resize(N); 当我使用选项complex创建类的实例时,一切都很好。 但是,如果我使用选项vector创建一个实例,并尝试调用ooutputData的元素(从公共函数getOutpuData(

我有一个带有一个变量数据类型的模板类
template
。 在类中,我有一个私有变量
vectoroutputData
。 因此,
输出
意味着
复杂
向量
。我有一个构造函数,其中一个参数是
intn
,给出了
outputData
向量的大小。在这个构造函数中,我初始化
outputData
如下:

ouputData.resize(N);
当我使用选项
complex
创建类的实例时,一切都很好。 但是,如果我使用选项
vector
创建一个实例,并尝试调用o
outputData
的元素(从公共函数
getOutpuData()
),它会给我
分段错误。显然,数据的内存存储有问题,但问题是什么?
此外,当模板使用
向量
时,此类输入向量的大小为2。 我猜我的初始化
ouputData.resize(N)不正确,但不确定!
好的,这是我的课:

template <class inputT, class outputT>
class LubichInverse {
    public:
        LubichInverse(inputT (*fofs_In)(complex<double>), double R_In,
                      double dt_In, int N_In, int L_In, int M_In = 1):
                      fofs(fofs_In), R(R_In), dt(dt_In), N(N_In), L(L_In), M(M_In) {

            outputData.resize(N);
        }

        void calculateInverse() {
            // do some stuff here to calculate outputData, e.g.:
            for( int n=0; n<N; n++) {
                complex<double> s( n, 2.0*n );
                outputData[n] = fofs(s);  // this one could be a source of memory bug!
            }
        }

        vector< outputT > getOutputData() {
            return outputData;
        }

    private:
        inputT (*fofs)(complex<double>);
        vector< outputT > outputData;
        double R;
        double dt;
        int N;
        int L;
        int M;
};

请原谅我没有给出原始函数,但我也必须解释重载运算符,而且我没想到这会是问题所在。不过,谢谢你,我知道做一个更简单的测试对定位问题非常有帮助。

一张图片抵得上千言万语。20行代码比描述它的20段文字更有价值。帖子a.,不是问题,但是:不要将
N
作为成员冗余存储-您可以通过
outputData.size()
将其取回(当然,除非向量和
N
的大小可以独立更改,以便将来这些值可能会有所不同)。这里也没有问题。使用clang和g++编译,一切正常。@Pekov只是为了理解:
outputData.resize(N)
在向量实例化中调整成员向量的大小,为每个包含的向量调用默认构造函数,即。E每个内部向量初始化为空。如果在编辑之前确实有一个空的
calculateInverse
函数,那么
vec_02[0]
返回一个对空向量的引用,并且您在访问这个向量时得到了超出范围的访问冲突(
vec_02[0]
[1])。
// this is a function to test the complex<double> version
complex<double> testScalar(complex<double> s_In) {
    return (1.0/(s_In-3.75));
}

// this is a function to test the vector< complex<double> > version
vector< complex<double> > testVector(complex<double> s_In) {
    vector< complex<double> > tmp(2);
    tmp[0] = 1.0/s_In;
    tmp[1] = 1.0/(s_In-3.75);
    return tmp;  // Probably this one returns dangling pointer, namely tmp?
}

int main() {
// this one works fine!
LubichInverse< complex<double>, complex<double> > tmp_01(testScalar, 0.32, 0.001, 1000, 1000);
tmp_01.calculateInverse();
vector< complex<double> > vec_01(tmp_01.getOutputData());
cout << vec_01[2] << endl;

// this one is the problem
LubichInverse< vector< complex<double> >, vector< complex<double> > > tmp_02(testVector, 0.32, 0.001, 1000, 1000);
tmp_02.calculateInverse();
vector< vector< complex<double> > > vec_02(tmp_02.getOutputData());
cout << vec_02[0][1] << endl; // namely here is the segmentation fault problem, if remove this line it does not complain!

return 0;
}
void calculateInverse() {
    double tmp_00(pid2/L);
    for( int n=0; n < N; n++) {
        double tmp_01(-n*tmp_00);
        double tmp_02(pow(R,-n)/L);
        // initialize outputData for the case l=0, this one was the problem
     // outputData[n] = fofs( BDF2(R/dt) ); // this one is correct
     // for ( int l=1; l<L; l++ ) {         // this one is correct
        for( int l=0; l < L; l++) { // here in the case l=0 is the issue
            complex<double> tmp_10(0.0,tmp_00*l);
            complex<double> s( BDF2(R*exp(tmp_10)) / dt );
            complex<double> tmp_11(0,tmp_01*l);
            outputData[n] += (fofs(s)*exp(tmp_11));   // here the left argument
            // of overloaded operator+= is a vector< complex<double> >
            // as is initialized in the constructor (just a pointer to null, I think)
            // while the right argument is a vector< complex<double> >
            // with size 2. So it is like adding vectors with zero size and size 2.
        }
        outputData[n] *= (tmp_02/dt);
    }
}