Public Group

# error: expected ' before '(' token

This topic is 3620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

template <class T>
class Maybe{
bool just;
T* value;

public:
Maybe<T>(){
just = false;
}

Maybe<T>(T* par){
this.Set(par);
}

void Set(T* par){
value = par;
just = true;
}

bool isNothing(){
return (not just);
}

bool isJust(){
return just;
}

T fromJust(){
return value;
}
};

class Node;

class Grid{
Maybe<Node>* gridvalues;
IntPoint3d size;
Grid(IntPoint3d sizepar){
size = sizepar;
gridvalues = (Maybe<Node>*)malloc(sizeof(Maybe<Node>)*size.Volume());
for(int x=0;x<size.Get(Coordinate3d(X));x++){
for(int y=0;y<size.Get(Coordinate3d(Y));y++){
for(int z=0;z<size.Get(Coordinate3d(Z));z++){
gridvalues[x+size.Get(Coordinate3d(X))*y+size.Get(Coordinate3d(X))*size.Get(Coordinate3d(Y))*z] = Maybe<Node>();
}
}
}
}

Maybe<Node>* Get(IntPosition3d pos){ //Error occurs here!
return (gridvalues+sizeof(Maybe<Node>)*(x+size.Get(Coordinate3d(X))*y+size.Get(Coordinate3d(X))*size.Get(Coordinate3d(Y))*z));
}
};


##### Share on other sites
Maybe<Node>* Get(IntPosition3d pos){ //Error occurs here!    return (gridvalues+sizeof(Maybe<Node>)*(x+size.Get(Coordinate3d(X))*y+size.Get(Coordinate3d(X))*size.Get(Coordinate3d(Y))*z));}

Where do x, y, z, X, Y come from?

##### Share on other sites
Here's some fixes to the Maybe class with comments:

#include <iostream>template <class T>class Maybe{    bool just;    T* value;    public:    //the <T> part in the following constructors seems unnecessary    Maybe<T>(){        just = false;    }    Maybe<T>(T* par){        //error: this.Set(par)        //(this is a pointer, so should you really want to use it        //it should read this->Set(par);        //But it is unnecessary anyway        Set(par);    }    void Set(T* par){        value = par;        just = true;    }    bool isNothing(){        return (not just);    }    bool isJust(){        return just;    }    T fromJust(){        //error: return value;        //value is T*, not T, so the types don't match        return *value;    }};//just a driverint main(){    int n = 42;    Maybe<int> m(&n);    std::cout << m.isNothing()        << ' ' << m.isJust() << ' '            << m.fromJust() << '\n';}

Another thing that is not good is that the default constructor leaves value uninitialized (it should at least be initialized to NULL).

The rest looks sort of fishy (for example allocating an array of objects with malloc - why not a vector that won't leak memory?).

The error you are asking might be because IntPosition3d is not declared or some other problems.

##### Share on other sites
I believe your problem comes from,

class Node;class Grid{    Maybe<Node>* gridvalues;

combined with,

T fromJust(){        return value;    }

It is fine to forward declare a class, and then create a pointer to it. That won't cause you any issues. I believe the problem comes when your fromJust() actually returns an instance of the class, and not just a pointer. If 'Node' is not declared somewhere, pointers to the Node type can be generated, but the code to create an instance can't. Did you mean to have T*? If not, another problem is you are returning 'value' which is a T* through a T. If you wanted to do that you would have to 'return *value' instead. Either of these issues could be causing that problem. (Or even something else!)

##### Share on other sites
Thanks for help. I got the problem now.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001835
×