Search code examples
c++templatesvectorinitializer

c++ compile error:expected initializer before ‘<’ token


I'm trying to compile a c++ program that i wrote myself. And I'm having trouble compiling it.

The quicksort.hpp file is:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include "cv.h"
#include "cv.hpp"
#include "highgui.h"

    void print<CvPoint3D32f>(vector<CvPoint3D32f>& input)
    {
            for ( int i = 0; i < input.size(); i++)
            {
            std::cout << input[i].y << " ";
            }
            std::cout << std::endl;

    }

And test.cpp is:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include "cv.h"
#include "highgui.h"
#include "quicksort.hpp"



    int main()
    {

        vector<CvPoint3D32f> input;
        for(int r = 0; r <= 9;r++)
        {
             input.push_back(cvPoint3D32f(2.0f+r,2.1f+r,3.1f+r)); 
        }
        std::cout << "Input: ";
        print(input);

    return 0;
    }

But I'm getting error like this:

quicksort.hpp:4: error: expected initializer before ‘<’ token
test.cpp: In function ‘int main()’:
test.cpp:22: error: ‘print’ was not declared in this scope
test.cpp:22: error: expected primary-expression before ‘>’ token

Is it possible to kindly help me figure out why I'm getting this error?

I'm using Debian Etch(Linux), g++(gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) and opencv 0.9.7-4


Solution

  • Just say:

    void print(vector<CvPoint3D32f>& points){
    

    This would solve the problem. If not you need to declare a template, and if really necessary look into template specialization for your CvPoint3D32f, but this would be overkill.