vector <double>类型错误</double>

时间:2012-11-18 19:05:33

标签: c++ vector

我正在攻读机械工程的本科论文,而且我在绘制数据方面遇到了麻烦。该项目是利用计算机视觉自动生成真实世界物体的高质量CAD模型。

我想将处理过的数据传递给GNUPLOT,以便快速生成图表。我正在使用临时文件来回传递数据。 (注意:如果您知道更简洁的方法,那么请务必指出它。)

每次我尝试编译程序时,都会收到以下错误:

/home/ryan/Code/FullyReversed/fullyreversed.cpp:-1: error: undefined reference 
to `QImage fr::Plotter::plot<double>(std::vector<double, std::allocator<double> >,
unsigned int, unsigned int)'

我不明白这个错误的来源。似乎编译器正在用另一个更复杂的结构替换我的vector<double>

简而言之,我将数据传递给Plotter::plot的方式有什么问题?

在我的课程主要课程中:

void MainWindow::plotData()
{
    double i;
    vector<double> intensity;
    static QImage plot;

    for(i=-10;i<10;i+=.1){
        intensity.push_back(1/(i*i+1));
    }

    plot = Plotter::plot(intensity,800,600);
    showQ(plot);
}

在辅助Plotter类中:

template <typename T>
QImage Plotter::plot(vector<T, allocator<T> > data, unsigned int width, unsigned int height){

    // for creating the filename
    char buffer[256];

    // the file we'll be writing to
    ofstream file;

    // loop counter
    unsigned int i;

    // time file generated
    time_t ftime = time(NULL);

    // generate the filename
    sprintf(buffer,"%d.dat",ftime);

    // open the file
    file.open(buffer);

    // write the data to the file
    for(i=0;i<data.size();i++){
        file << i << " " << data.at(i) << endl;
    }

    //generate the command
    sprintf(buffer,"gnuplot -e \"set terminal png size %d, %d;set output '%d.png';plot sin(x);\"",width,height,ftime);

    // call GNUPLOT
    system(buffer);

    // load the image
    sprintf(buffer,"%d.png",ftime);
    QImage out = QImage(buffer);

    return out;
}

2 个答案:

答案 0 :(得分:0)

这是在源文件中定义模板函数而不是头文件的症状。

模板不是实际的功能,它只是构建功能的说明。整个代码需要在您调用它时可用,以便编译器可以为您生成适当的函数。如果它没有它,则假定在其他地方定义了一个并留下链接器来计算它。

答案 1 :(得分:0)

您似乎在询问一个经常回答的问题:您在翻译单元中定义了模板,当您实际实例化模板时,它对编译器是不可见的。只要您意识到如果编译器无法为隐式实例化找到其定义,则需要显式实例化模板,这是可以的。

显式实例化看起来像这样:

template QImage Plotter::plot(vector<double> data, unsigned int width, unsigned int height);
相关问题