Sign in to follow this  
steenreem

error: expected ' before '(' token

Recommended Posts

Thanks for reading!
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 this post


Link to post
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 this post


Link to post
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 driver
int 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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this