views:

119

answers:

4

why in the first line of this code:

template <typename VectorType>
std::string repr_vector_dynamic(const VectorType* vect)
{
    std::stringstream strs;
    strs << "(";
    for (int i = 0; i < vect->size(); ++i) {
        if (0 != i) {
            strs << ", ";
        }
        strs << (*vect)[i];
    }
    strs << ")";
    return strs.str();
}

I always get thsese errors at the first line: (MinGW gcc 4.5)

expected ';' before 'template'

expected primary-expression before 'template'

thx

later edit: here is the command line / log I got from eclipse:

g++ -II:\proj\bp\PyCML -IC:\PF\Python26\include -II:\proj\bp/include -O3 -Wall -c -fmessage-length=0 -oPyCML\cml.o ..\PyCML\cml.cpp
In file included from I:\proj\bp/include/boost/python/object/make_instance.hpp:9:0,
             from I:\proj\bp/include/boost/python/object/make_ptr_instance.hpp:8,
             from I:\proj\bp/include/boost/python/to_python_indirect.hpp:11,
             from I:\proj\bp/include/boost/python/converter/arg_to_python.hpp:10,
             from I:\proj\bp/include/boost/python/call.hpp:15,
             from I:\proj\bp/include/boost/python/object_core.hpp:14,
             from I:\proj\bp/include/boost/python/args.hpp:25,
             from I:\proj\bp/include/boost/python.hpp:11,
             from ..\PyCML\cml.cpp:11:
I:\proj\bp/include/boost/python/object/instance.hpp:14:36: warning: type attributes ignored after type is already defined
In file included from ..\PyCML\cml.cpp:28:0:
I:\proj\bp\PyCML/PyCMl/vector.h: In function 'void init_module_PyCML()':
I:\proj\bp\PyCML/PyCMl/vector.h:22:1: error: expected primary-expression before 'template'
I:\proj\bp\PyCML/PyCMl/vector.h:22:1: error: expected ';' before 'template'
..\PyCML\cml.cpp:58:1: error: expected '}' at end of input
A: 
expected ';' before 'template'
expected primary-expression before 'template'

You forgot a semicolon before the first line. Can you give the previous line ?

William Briand
like I said, there are only includes, but here you go:#include <PyCML/PyCML.h>#include <boost/python.hpp>using namespace boost::python;using namespace cml;#include <string>#include <sstream>template <typename VectorType>std::string repr_vector_dynamic(const VectorType* vect)
lj8888
there are no missing ';' before this
lj8888
Can you give the command line you used ? Is it the only file ?
William Briand
there are more files, I am building with eclipse, I guess I can extract the command line
lj8888
A: 

Maybe you should be using g++, the C++ compiler, instead of gcc, the C compiler.

Another possibility is that one of your headers has #defined a truly evil macro.

Ben Voigt
gcc will run in C++ mode if the file has a C++ extension. And if this was actually compiled as C, you'll get a much different error.
R Samuel Klatchko
+1  A: 

As others have suggested, it is what comes before the template declaration that is missing the ;.

Take a look at what the compiler sees, thus:

g++ -E <yourfile>.cpp | less

Johnsyweb
I tried your idea, I had to remove all of the includes because the file was getting too long:g++ -E vector.h /* mine's a header file */ -I<include paths> > test.txtbut like I said before, preceding the template are only includes:#include <string>#include <sstream>template <typename VectorType>std::string repr_vector_dynamic(const VectorType* vect){
lj8888
@lj8888: The point is you need to see what the headers have expanded to after preprocessing so you musn't remove anything otherwise you invalidate this particular test. Also, don't run this on the header file, just run it on the source file that won't compile. Rather than `| less` you can just redirect it to a file using `> yourfile.ii` if you want. You can then search through that file using your favourite text editor to find your `repr_vector_dynamic` function template and see _exactly_ what is on the preceding lines now that all headers and macros have been replaced with actual content.
Troubadour
if I run it on the .cpp file I only get the g++ errors, that I added in the edit, if I run the -E on the header, the file size is huge, adn repr_vector_dynamic cannot by found by searching
lj8888
A: 

The errors that you have posted suggest that the problem is within the function void init_module_PyCML(). It would therefore appear that you have tried to define a function template within the body of another function.

For example trying to compile the following code gives me the same error

void f()
{

template < typename T >
void g()
{
};

}

The last error line suggests that perhaps you have forgotten the closing } for init_module_PyCML()?

Troubadour
you might have the right answer, in my mind BOOST_PYTHON_MODULE was leaving between the braces the scope of a namespace, so I included these headers in that, so far I get 96 boost::python errors, but I think it's a start
lj8888