• 13
• 18
• 19
• 27
• 10

# Can't use "const myClass& class" as a function parameter. Why?

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

## Recommended Posts

Hi i get this compiler error: f:\Programmiertes\nGin\ngMath.h(29): error C2143: syntax error : missing ',' before '&' for every funcion declaration that contains "const ngPlane& plane" and i don't know why. I can use this syntax with the other classes i have but ont with ngPlane. I must admit i don't really understand what this syntax does(i copied it from a book) but i have a guess: const just means that the value of the variable can't be changed but i'm confused about the & behing ngPlane...does this declare plane as a reference, or what? my ngPlane class(the dotVector3 calls are commented out because i ahve some probs with this function):
class ngPlane
{
private:
ngVector3 normal,point;
public:
float d;
void setNormal(ngVector3 n)
{
normal=n;
//		d=-dotVector3(n,point);
}
void setPoint(ngVector3 p)
{
point=p;
//		d=-dotVector3(normal,p);
}
ngVector3 getPoint() {return point;}
ngVector3 getNormal() {return normal;}

ngPlane(ngVector3 norm,ngVector3 pointOnPlane): normal(norm), point(pointOnPlane)
{
//		d=-dotVector3(normal,point);
}
ngPlane(void)
{
}
~ngPlane(void)
{
}
};


regards, m4gnus

##### Share on other sites
Are you including the header that ngPlane is declared in before you try to use it? The error is because the compiler doesn't know what a ngPlane is, so it gets confused.

Edit: Yes, const ngPlane& plane declares a constant reference to a ngPlane, called plane. Constant references are good to pass to functions in C++, since you don't get the overhead of passing by value (larger object to move onto the stack, construction and destruction costs), and you can do some thing with constant references that you can only do with them or passing by value, such as (Using a vector example):
DotProduct(Vector(1,2,3), Vector(4,5,6))
That'll construct two Vector objects, and pass them to DotProduct().

##### Share on other sites
you're right the compiler had some probs...i noticed that in ngMath i included ngPlane and ngPlane included ngMath ...

but now i get some errors using the plane class in my functions:
i get this error:

f:\Programmiertes\nGin\ngMath.cpp(51): error C2662: 'ngPlane::getNormal' : cannot convert 'this' pointer from 'const ngPlane' to 'ngPlane &'

on this line:

normal=plane.getNormal();
(normal is an ngVector3)

this error occurs every time i try to use ngPlane::getNormal()

how can i fix this?

regards,
m4gnus

##### Share on other sites
You have to declare any functions you want to call as const

e.g

ngVector3 getPoint() const {return point;}ngVector3 getNormal() const {return normal;}

Of course, those functions won't be able to modify the object, but that's expected since you're passing a const object.