Monday, July 27, 2009

Why am I getting a syntax error here in C++?

Point::Point(const Point%26amp; p)


{


num_points++; //Adds new point to number of points.


double *x = new double p.x; //Makes a copy of x.


double *y = new double p.y; //Makes a copy of y.


}





h:\point\point\point\point.cpp(24) : error C2146: syntax error : missing ';' before identifier 'p'


h:\point\point\point\point.cpp(25) : error C2146: syntax error : missing ';' before identifier 'p'

Why am I getting a syntax error here in C++?
if x and y are members of p as you infer with p.x and p.y, why not just set *x = p.x; and *y = p.y;?





unless p.x and p.y aren't doubles to start with...


new double(); requires parens, so in this case i'd write:


double *x = new double(p.x);


etc.
Reply:Point::Point(const Point%26amp; p) // i think this is your error should be Point::Point(const Point %26amp;p) in mot so sure myself


{


num_points++; //Adds new point to number of points.


double *x = new double p.x; //Makes a copy of x.


double *y = new double p.y; //Makes a copy of y.


}
Reply:Try putting parenthesis around the initialization parameter.





double *x = new double (p.x); //Makes a copy of x.


double *y = new double (p.y); //Makes a copy of y.


No comments:

Post a Comment