Unable to compile, assistance needed

Pages: 12
==========================
Line2D.h
==========================

#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include "Point2D.h"

using namespace std;

class Line2D
{
private:
Point2D pt1;
Point2D pt2;

protected:
double length;
void setLength();

public:
Line2D(Point2D, Point2D);
Point2D getPt1();
Point2D getPt2();
double getScalarValue();

void setPt1(Point2D);
void setPt2(Point2D);

};


==========================
Line2D.cpp
==========================

#include "Line2D.h"
//#include "Point2D.h"

using namespace std;


Line2D::Line2D(Point2D point1, Point2D point2)
{
pt1 = point1;
pt2 = point2;
}

Point2D Line2D::getPt1()
{
return pt1;
}

Point2D Line2D::getPt2()
{
return pt2;
}

double Line2D::getScalarValue()
{
return length;
}

void setPt1(Point2D PT1)
{
pt1 = PT1;
}

void setPt2(Point2D PT2)
{
pt2 = PT2;
}

void Line2D::setLength()
{
length = sqrt (pow(pt1.x - pt2.x),2) + pow(pt1.y - pt2.y),2);

}


==========================
Line3D.h
==========================

#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include "Point3D.h"
#include "Line2D.h"



using namespace std;

class Line3D : public Line2D
{
private:
Point3D pt1;
Point3D pt2;

protected:
void setLength();

public:
Line3D(Point3D, Point3D);
Point3D getPt1();
Point3D getPt2();

void setPt1(Point3D);
void setPt2(Point3D);

};


==========================
Line3D.cpp
==========================

#include "Line3D.h"

using namespace std;

Line3D::Line3D(Point3D point1, Point3D point2)
{
pt1 = point1;
pt2 = point2;
}

Point3D Line3D::getPt1()
{
return pt1;
}

Point3D Line3D::getPt2()
{
return pt2;
}

void Line3D::setPt1(Point3D SetPt1)
{
pt1 = SetPt1;
}

void Line3D::setPt2(Point3D SetPt2)
{
pt2 = SetPt2;
}

void Line3D::setLength()
{
length = sqrt (pow((pt1.x - pt2.x),2) + pow((pt1.y - pt2.y),2) + pow((pt1.z - pt2.z),2));

}

==========================
compile errors
==========================

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 :(
1
2
3
4
5
Line2D::Line2D(Point2D point1, Point2D point2)
{
	pt1 = point1;
	pt2 = point2;
}

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:
1
2
3
4
5
Line2D::Line2D(Point2D point1, Point2D point2)
:	pt1(point1),
	pt2(point2)
{
}
Last edited on
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
1
2
3
4
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)’

Check your parentheses: length = sqrt (pow(pt1.x - pt2.x),2) + pow(pt1.y - pt2.y),2);
Last edited on
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

How can I use x and x for my class Line2D?


==========================
Point2D.h
==========================

#include <iostream>
#include <string>
#include <fstream>
#include <math.h> // so that able to use sqrt and pow


using namespace std;

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);
};



==========================
Point2D.cpp
==========================

#include "Point2D.h"

using namespace std;


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.

x-0 is the same as x and y-0 is the same as y

also pow(x,2) is the same as x*x

so you could use distFrOrigin = sqrt(x*x+y*y)

although what you've got looks OK - I haven't tested it
Hi milk2718,

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



And here is the formular.

void Line2D::setLength()
{
length = sqrt (pow(pt1.x - pt2.x),2) + pow(pt1.y - pt2.y),2);

}



error: too few arguments to function ‘double pow(double, double)’
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.
1
2
length = sqrt(pow(pt1.x - pt2.x),2) + pow(pt1.y - pt2.y),2);
length = sqrt(pow(pt1.x - pt2.x, 2) + pow(pt1.y - pt2.y, 2));
In case it isn't clear, the second line is correct.
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


Call the functions that you created to return the values of X and Y. Do you remember these?
1
2
3
4
5
6
7
8
9
int Point2D::getX()	 //get method for x
{
    return x;
}

int Point2D::getY()	 //get method for y
{
    return y;
}
and 'Point.h'...

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);
};
Last edited on
Sorry for my bad look...
Because I usually enter the forum by MP...
Yes, I'm giving a good thinking about what L B has mention. Call the function.

Because it's mandatory to set my x and x protected. But Jackson, really appreciate that :)

hmm...but @L B, I did not declare the get and set function in my Line2D.h how can i call?
You declared functions already.
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(); //right///////////////
    int getY(); //here////////////////
    double getScalarValue();

    void setX(int);
    void setY(int);
};
Last edited on
They are in the class Point2D and not Line2D.h

Line2D.h
===========

class Line2D
{
private:
Point2D pt1;
Point2D pt2;

protected:
double length;
void setLength();

public:
Line2D(Point2D, Point2D);
Point2D getPt1();
Point2D getPt2();
double getScalarValue();

void setPt1(Point2D);
void setPt2(Point2D);


};
Whoops, copied wrong class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Line2D
{
private:
Point2D pt1;
Point2D pt2;

protected:
double length;
void setLength();

public:
Line2D(Point2D, Point2D);
Point2D getPt1(); //also////////////////
Point2D getPt2(); //here////////////////
double getScalarValue();

void setPt1(Point2D);
void setPt2(Point2D);


};


As an example: MyInstanceOfTheLine2DClass.getPt1().getX()
Last edited on
So do you mean in my Line2D.cpp, rather then coding it like this

length = sqrt (pow((pt1.x - pt2.x),2) + pow((pt1.y - pt2.y),2));

I should do a function call like this instead?

length = sqrt (pow((pt1.getPt1() - pt2.getPt1()),2) + pow((pt1.getPt2() - pt2.getPt1()),2));

OMG!!! I figured it out!!!

Yes!!!...

It's like this

length = sqrt (pow((pt1.getX() - pt2.getX()),2) + pow((pt1.getY() - pt2.getY()),2));

OMG, thank you guys so much... really appreciate the time :D
Pages: 12