Classes

Hi all,
I receive an error when compiling this. Cant understand why.
error: request for member 'toString' in 'shape; which is of non-class type of 'GeometricObject()'
GeometricObject.h
#ifndef GEOMETRICOBJECT_H_INCLUDED
#define GEOMETRICOBJECT_H_INCLUDED
#include <string>
using namespace std;

class GeometricObject{
public:
GeometricObject();
GeometricObject(string colour, bool filled);
string toString();

private:
string colour;
bool filled;
};

#endif // GEOMETRICOBJECT_H_INCLUDED

GeometricObject.cpp
#include "GeometricObject.h"

GeometricObject::GeometricObject(){
colour = "White";
filled = false;
}

GeometricObject::GeometricObject(string colour, bool filled){
this->colour = colour;
this->filled = filled;
}

string GeometricObject::toString(){
return "Geometric object colour " + colour +
" filled " + ((filled) ? "true" : "false");
}

main.cpp
#include "GeometricObject.h"
#include <iostream>

using namespace std;

int main()
{
GeometricObject shape;
cout<<shape.toString();

return 0;
}

Please assist thanks!
Last edited on
GeometricObject shape();
The compiler treat this as a function declaration. A function named shape returning a GeometricObject.

Remove the parenthesis and it will work better.
GeometricObject shape;
Last edited on
Thanks for the reply but im still having an error.
'undefined reference to 'GeometricObject::toString()'
You have to link all the source files.
Thank You so much for all your replies.
I think i did link them. I #include "GeometricObject.h"
Please assist!
When building a program you first have to compile all the source (.cpp) files to object files. Then you have to link all the object files together to create the executable file. You might be able to to it all in one step. How to do it depends on the compiler/IDE.
Topic archived. No new replies allowed.