error: expected primary-expression before ‘,’ token ... (only on linux)

C

carl

I have a template function that I call like this:

V testSize;
F image = getImage();
util.computeSize<V, I, P>(testSize, image);

But I get this error:

error: expected primary-expression before ‘,’ token

util is an instance of:

typedef MyUtil<I, M, T, D> UtilType;
UtilType util;

Which is a public field in my class. As can be seen I pass template
parameters (V and P) to the function that I do not pass to the class, could
this be the cause?

The wierd thing is that it compile fine in Visual Studio on windows Vista
but it gives the above error when I compile it on Ubuntu Linux.

Any ideas on what I am doing wrong?
 
I

Ian Collins

carl said:
I have a template function that I call like this:

V testSize;
F image = getImage();
util.computeSize<V, I, P>(testSize, image);

But I get this error:

error: expected primary-expression before ‘,’ token

util is an instance of:

typedef MyUtil<I, M, T, D> UtilType;
UtilType util;

Which is a public field in my class. As can be seen I pass template
parameters (V and P) to the function that I do not pass to the class,
could this be the cause?

The wierd thing is that it compile fine in Visual Studio on windows
Vista but it gives the above error when I compile it on Ubuntu Linux.

Any ideas on what I am doing wrong?

Not posting enough information!

Please post a minimal test case that shows your problem.
 
J

Jonathan Lee

I have a template function that I call like this:

    V testSize;
    F image = getImage();
    util.computeSize<V, I, P>(testSize, image);

But I get this error:

error: expected primary-expression before ‘,’ token

Can't tell from what you posted, but V isn't a macro for
a scoped class name is it? The only time I've had that
error is when I've done something like

myclass.somefunc<::std::string, ::std::string>(...)

where the string "<::" doesn't parse. Putting a space in
(i.e., "< ::") solves the problem (or, simply not using
global scope resolution but that's besides the point).

That's my only guess. Or "testSize" isn't successfully
declared, but you'd have an error preceding the one you
described.

--Jonathan
 
C

carl

Ian Collins said:
Not posting enough information!

Please post a minimal test case that shows your problem.



The call to the failing template function is made from the function
setup() in the class Components:


#include "my_types.h"

template<unsigned int D=3>
class Components{
public:
....
....
// Types
typedef typename MyTypes::VectorType V;
typedef typename MyTypes::ImageType F;
typedef typename MyTypes::Image2Type I;
typedef typename MyTypes::TransformType T;
typedef typename MyTypes::MeshType M;
typedef typename MyTypes::pointType P;
typedef MyUtil<I, M, T, D> UtilType;

// Instances
UtilType util;


void setup() {
V testSize;
F image;
util.computeSize<V, I, P> (testSize, image);

....

}






'util' is an instance of MyUtil which contains the failing function
'computeSize()' declared like this:


#include "my_types.h"

template<typename I, typename M, typename T, unsigned int D=3>
class MyUtil {
public:
typedef typename MyTypes::VectorType V;

...
...

template<typename V, typename I, typename P>
void computeSize(V & imageSize, typename I::pointer image) {

....

}



I have tried to remove the typedef:

typedef typename MyTypes::VectorType V;

from the MyUtil class and move it to the template declaration like this:

template<typename V, typename I, typename M, typename T, unsigned int D=3>
class MyUtil {


And change the code that creates the MyUtil class accordingly, but
that does not solve the problem.

But why does this work fine on windows and not in linux?
 
C

carl

carl said:
The call to the failing template function is made from the function
setup() in the class Components:


#include "my_types.h"

template<unsigned int D=3>
class Components{
public:
....
....
// Types
typedef typename MyTypes::VectorType V;
typedef typename MyTypes::ImageType F;
typedef typename MyTypes::Image2Type I;
typedef typename MyTypes::TransformType T;
typedef typename MyTypes::MeshType M;
typedef typename MyTypes::pointType P;
typedef MyUtil<I, M, T, D> UtilType;

// Instances
UtilType util;


void setup() {
V testSize;
F image;
util.computeSize<V, I, P> (testSize, image);

....

}






'util' is an instance of MyUtil which contains the failing function
'computeSize()' declared like this:


#include "my_types.h"

template<typename I, typename M, typename T, unsigned int D=3>
class MyUtil {
public:
typedef typename MyTypes::VectorType V;

...
...

template<typename V, typename I, typename P>
void computeSize(V & imageSize, typename I::pointer image) {

....

}



I have tried to remove the typedef:

typedef typename MyTypes::VectorType V;

from the MyUtil class and move it to the template declaration like this:

template<typename V, typename I, typename M, typename T, unsigned int
D=3>
class MyUtil {


And change the code that creates the MyUtil class accordingly, but
that does not solve the problem.

But why does this work fine on windows and not in linux?



Hm now I have tried to move the types to the MyUtil class and now the
function computeSize(...) is a simple non-template function:

void computeSize(V & imageSize, typename I::pointer image) {

}

But are there any special rules for calling a non-template function in a
template class that I am missing?
 
C

carl

Ha the error was that the vector type was a float in the one module and a
double in the other. Is that not a pretty nasty thing to NOT to throw an
error for when using th Visual Studio compiler?
 
T

Thomas J. Gritzan

carl said:
I have a template function that I call like this:

V testSize;
F image = getImage();
util.computeSize<V, I, P>(testSize, image);

But I get this error:

error: expected primary-expression before ‘,’ token

The compiler can't parse the expression, so if this code is in a
template function, most likely the compiler doesn't know that
computeSize is a template.

Just like you sometimes need to add the "typename" keyword to tell the
compiler that an identifier is a type, you need to add the "template"
keyword to tell him that something is a template, i.e.:

util.template computeSize<V, I, P>(testSize, image);

Since the type of util depends on a template parameter, the compiler
can't immediately determine that one of its members is a template or not.

For any code like this that doesn't work, the best way is to post a
minimal but compilable code example that shows the error. If you only
post those code extracts with important details replaced by "...", it is
really hard to guess the error. Please read the FAQ:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

PS: Why isn't this "typename" and "template" issue already in the FAQ?
 
J

Johannes Schaub (litb)

Thomas said:
The compiler can't parse the expression, so if this code is in a
template function, most likely the compiler doesn't know that
computeSize is a template.

Just like you sometimes need to add the "typename" keyword to tell the
compiler that an identifier is a type, you need to add the "template"
keyword to tell him that something is a template, i.e.:

util.template computeSize<V, I, P>(testSize, image);

Since the type of util depends on a template parameter, the compiler
can't immediately determine that one of its members is a template or not.

For any code like this that doesn't work, the best way is to post a
minimal but compilable code example that shows the error. If you only
post those code extracts with important details replaced by "...", it is
really hard to guess the error. Please read the FAQ:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

PS: Why isn't this "typename" and "template" issue already in the FAQ?

It's in the template faq: http://womble.decadentplace.org.uk/c++/template-
faq.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top