I’m working on my undergraduate thesis in mechanical engineering and I’m having trouble plotting data. The project is to use computer vision to automatically generate a high-quality CAD model of a real-world object.
I want to pass processed data to GNUPLOT in order to quickly generate a graph. I’m using temporary files to pass the data back and forth. (Note: if you know of a cleaner way to do this then by all means point it out.)
Every time I attempt to compile the program, though, I get the following error:
/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)'
I don’t understand where this error is coming from. It seems that the compiler is replacing my vector<double> with another, more complex structure
So, in short, what is the matter with the way I’m passing the data to Plotter::plot?
In my program’s main class:
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);
}
In the auxiliary Plotter class:
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;
}
This is a symptom of defining a template function in a source file instead of a header file.
A template isn’t an actual function, it’s just the instructions for building a function. The whole code needs to be available at the point where you call it so that the compiler can generate the proper function for you. If it doesn’t have it, it assumes one is defined somewhere else and leaves the linker to figure it out.