error: expected ' before '(' token

Started by
3 comments, last by steenreem 15 years, 8 months ago
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));
    }
};
Advertisement
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?
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.
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!)
Thanks for help. I got the problem now.

This topic is closed to new replies.

Advertisement