user@ubuntu904desktop:~/Desktop/CSCI204Lab/Assignment3/Nov14$ g++ -c Point2D.h Point2D.cpp Point3D.h Point3D.cpp Line2D.h Line2D.cpp Line3D.h Line3D.cpp
Line2D.cpp: In constructor ‘Line2D::Line2D(Point2D, Point2D)’:
Line2D.cpp:7: error: no matching function for call to ‘Point2D::Point2D()’
Point2D.h:18: note: candidates are: Point2D::Point2D(int, int)
Point2D.h:10: note: Point2D::Point2D(const Point2D&)
Line2D.cpp:7: error: no matching function for call to ‘Point2D::Point2D()’
Point2D.h:18: note: candidates are: Point2D::Point2D(int, int)
Point2D.h:10: note: Point2D::Point2D(const Point2D&)
Line2D.cpp: In function ‘void setPt1(Point2D)’:
Line2D.cpp:30: error: ‘pt1’ was not declared in this scope
Line2D.cpp: In function ‘void setPt2(Point2D)’:
Line2D.cpp:35: error: ‘pt2’ was not declared in this scope
Point2D.h: In member function ‘void Line2D::setLength()’:
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
/usr/include/bits/mathcalls.h:154: error: too few arguments to function ‘double pow(double, double)’
Line2D.cpp:40: error: at this point in file
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
/usr/include/bits/mathcalls.h:154: error: too few arguments to function ‘double pow(double, double)’
Line2D.cpp:40: error: at this point in file
Line2D.cpp:40: error: expected ‘;’ before ‘)’ token
In file included from Line2D.h:5,
from Line3D.h:6:
Point2D.h:9: error: redefinition of ‘class Point2D’
Point2D.h:10: error: previous definition of ‘class Point2D’
Line3D.cpp: In constructor ‘Line3D::Line3D(Point3D, Point3D)’:
Line3D.cpp:5: error: no matching function for call to ‘Line2D::Line2D()’
Line2D.h:20: note: candidates are: Line2D::Line2D(Point2D, Point2D)
Line2D.h:10: note: Line2D::Line2D(const Line2D&)
Line3D.cpp:5: error: no matching function for call to ‘Point3D::Point3D()’
Point3D.h:16: note: candidates are: Point3D::Point3D(int, int, int)
Point3D.h:10: note: Point3D::Point3D(const Point3D&)
Line3D.cpp:5: error: no matching function for call to ‘Point3D::Point3D()’
Point3D.h:16: note: candidates are: Point3D::Point3D(int, int, int)
Point3D.h:10: note: Point3D::Point3D(const Point3D&)
Point2D.h: In member function ‘void Line3D::setLength()’:
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line3D.cpp:33: error: within this context
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line3D.cpp:33: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line3D.cpp:33: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line3D.cpp:33: error: within this context
Point3D.h:12: error: ‘int Point3D::z’ is protected
Line3D.cpp:33: error: within this context
Point3D.h:12: error: ‘int Point3D::z’ is protected
Line3D.cpp:33: error: within this context
I'm really a beginner in C++, desperately in need of assistance . Can any kind soul assist and see what is it causing the errors :(
This will try to initialize Line2D::pt1 and Line2D::pt2 by using the default constructor (constructor that takes no arguments). Line2D doesn't have a default constructor so you get an error message.
To fix this you can add a default constructor to Line2D and/or initialize Line2D::pt1 and Line2D::pt2 by using the copy constructor instead. That is done by listing them in the constructor initialization list, like this:
Thank you Peter87, that helps to a lot of errors.. :)
Now left with these...
Line2D.cpp: In function ‘void setPt1(Point2D)’:
Line2D.cpp:30: error: ‘pt1’ was not declared in this scope
Line2D.cpp: In function ‘void setPt2(Point2D)’:
Line2D.cpp:35: error: ‘pt2’ was not declared in this scope
Point2D.h: In member function ‘void Line2D::setLength()’:
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
/usr/include/bits/mathcalls.h:154: error: too few arguments to function ‘double pow(double, double)’
Line2D.cpp:40: error: at this point in file
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
/usr/include/bits/mathcalls.h:154: error: too few arguments to function ‘double pow(double, double)’
Line2D.cpp:40: error: at this point in file
Line2D.cpp:40: error: expected ‘;’ before ‘)’ token
Line2D.cpp: In function ‘void setPt1(Point2D)’:
Line2D.cpp:30: error: ‘pt1’ was not declared in this scope
Line2D.cpp: In function ‘void setPt2(Point2D)’:
Line2D.cpp:35: error: ‘pt2’ was not declared in this scope
You forgot to put Line2D:: in front of the function name when defining the function.
1 2 3 4 5
Point2D.h: In member function ‘void Line2D::setLength()’:
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
x and y is protected in Point2D so you can't access them directly from other functions. Make z and y public or add get functions that you can use to get their values.
1 2
error: too few arguments to function ‘double pow(double, double)’
Really appreciate that Peter87, thanks for pointing out to put Line2D::
again, that remove some errors.
I understand x and y are protected in Point2D, and I can't access them directly.
That is because it is mandatory to set x and y protected. How can I add get functions to use their values? This is my class Point2D
Point2D::Point2D(int X, int Y) //constructor
{
x = X;
y = Y;
}
int Point2D::getX() //get method for x
{
return x;
}
int Point2D::getY() //get method for y
{
return y;
}
double Point2D::getScalarValue() //merely an accessor method that returns the value of attribute distFrOrigin
{
return distFrOrigin;
}
void Point2D::setX(int SetX) //set method for x
{
x = SetX;
}
void Point2D::setY(int SetY) //set method for y
{
y = SetY;
}
void Point2D::setDistFrOrigin() //computes the dist of point to origin
{
distFrOrigin = sqrt (pow((x-0),2) + pow((y-0),2));
}
==========================
error: too few arguments to function ‘double pow(double, double)’
==========================
These are part of the requirements to compute length. Did I code the formula right?
4) In Line2D class, setLength () method computes the distance between its own Point2D attributes pt1 and pt2, and initializes the attribute length with this distance value. The formula to compute is as follows:
length = √ (pt1.x – pt2.x)2 + (pt1.y – pt2.y)2
5) In Line2D class, getScalarValue () method is merely an accessor method that returns the value of attribute length.
Apologized for the confusion. The error is actually for one of the formula in class Line2D. Here's the full error.
Point2D.h: In member function ‘void Line2D::setLength()’:
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
/usr/include/bits/mathcalls.h:154: error: too few arguments to function ‘double pow(double, double)’
Line2D.cpp:40: error: at this point in file
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
/usr/include/bits/mathcalls.h:154: error: too few arguments to function ‘double pow(double, double)’
Line2D.cpp:40: error: at this point in file
Line2D.cpp:40: error: expected ‘;’ before ‘)’ token
Well, the answer is that your calls to the function pow, you're supplying too few arguments.
Given that the error message is kind enough to tell you what arguments you should be supplying, you should have no problem identifying what you're doing wrong.
Got it!, I'm short of some parentheses in the formula! Thank you for the reminder :)
Now how can my formula actually access to the protected data 'x' & 'y" which is in my class Point2D?
Point2D.h: In member function ‘void Line2D::setLength()’:
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:12: error: ‘int Point2D::x’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
Point2D.h:13: error: ‘int Point2D::y’ is protected
Line2D.cpp:40: error: within this context
You set all members and functions in your class a 'protected' attribute so in short you can't access any members or functions directly. Find & Change 'protected' to 'public', and try again.
@Jackson Marie That is incorrect. His getter/setter functions are public, read more closely:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Point2D //class name and declaration section in class header
{
protected:
int x;
int y;
double distFrOrigin;
void setDistFrOrigin();
public:
Point2D(int, int);
int getX();
int getY();
double getScalarValue();
void setX(int);
void setY(int);
};
class Point2D //class name and declaration section in class header
{
protected:
int x;
int y;
double distFrOrigin;
void setDistFrOrigin();
public:
Point2D(int, int);
int getX(); //right///////////////
int getY(); //here////////////////double getScalarValue();
void setX(int);
void setY(int);
};