c++ help please!

Soldato
Joined
18 Feb 2003
Posts
5,817
Location
Chester
right i'm a bit confused has to how to fix this error i've got -

error C2270: 'getx' : modifiers not allowed on nonmember functions

when using -

Code:
float getx() const // return x coord of point
{
	float getX();    // not sure what to put in here tbh
}
with the class definition -
Code:
public:
Point(float x, float y, float z); // Construct from 3 floats
float getx() const; // return x coord of point
 
Last edited:
is getX() another function?

Code:
float getx() const // return x coord of point
{
        return getX();
}
 
Assuming your class is called Point and that you have a member variable x.

Code:
float Point::getx() const
{
       return x;
}

Is likely what you are trying to do.
 
yeah the class is called Point - although it is also present in another class under the same name. which looks like -
Code:
public:
	Coords2d(); // default constructor
	Coords2d (const Coords2d& source); // copy constructor
	Coords2d(float x, float y); // Construct from 2 floats
	Coords2d& operator= (const Coords2d& other); // assignment operator
	float getX() const;

float Coords2d::getX() const
{
	return x_;
}

and that works fine.
_________________________
since last night i've changed my function to -
Code:
float Point::getx() // return x coord of point
{
	return x;
}
and now get the error -
error C2511: 'float Point::getx(void)' : overloaded member function not found in 'Point'

and i can't remember much on overloading functions - any ideas?
 
|Show| said:
yeah the class is called Point - although it is also present in another class under the same name. which looks like -
Code:
public:
	Coords2d(); // default constructor
	Coords2d (const Coords2d& source); // copy constructor
	Coords2d(float x, float y); // Construct from 2 floats
	Coords2d& operator= (const Coords2d& other); // assignment operator
	float getX() const;

float Coords2d::getX() const
{
	return x_;
}

and that works fine.
_________________________
since last night i've changed my function to -
Code:
float Point::getx() // return x coord of point
{
	return x;
}
and now get the error -


and i can't remember much on overloading functions - any ideas?

The declaration has it as a const function, but the definition does not.

Incidentally - are you combining the class definition and its implementation in the same file? If so, thats not a very good idea.....
 
ah, sorry, no. its all coming from different files. I've got separate header files for the class definitions and separate cpp files for each class :)
 
Assuming your point class header definition is the same as your coords2d header definition for the getX function, you'll need to change the function definition in your code file so that it matches the header word for word, but with the class name preceding the function name:

Header:
float getX() const;

Code:
float Point::getX() const{}

Without that const there the header wont be able to find it.
The reason for your very first error "modifiers not allowed on nonmember functions" - without the Point:: part, there is no class attached to the function, i.e. it is not a member of any class.

Regarding overloading functions, an overload is a function which has been inherited from another (parent) class, then redefined in the new (child) class.
For example, with your classes you could define 'Point' as a derivation (child) of coords2d (parent), like so:

class Point : public coords2d
{
}

By doing this, all of the functions available in coords2d (including the getX which you've copied) would be available to the Point class, without having to define them again.
But if you wanted to change the getX function, and defined it inside the Points class, that new function would be an "Overload" of the coords2d function by the same name.
 
Last edited:
right looks like i forgot to actually construct anything from the Point class.

now after running through the constructors i get the error -

error C2614: 'Point' : illegal member initialization: 'x' is not a base or member
 
|Show| said:
right looks like i forgot to actually construct anything from the Point class.

now after running through the constructors i get the error -

Well the error message says exactly what the problem is......
 
Assuming this is all of your code:

Header (obviously with coords2d changed to Point...)

Code:
public:
	Coords2d(); // default constructor
	Coords2d (const Coords2d& source); // copy constructor
	Coords2d(float x, float y); // Construct from 2 floats
	Coords2d& operator= (const Coords2d& other); // assignment operator
	float getX() const;

and code:
Code:
float Point::getx() // return x coord of point
{
	return x;
}

You havent actually created the x variable anywhere, your "return x;" statement has no idea what x is. Add "float x = 0.0;" to the header.
 
Back
Top Bottom