I've nested a class for use within another class and need to try accessing individual parts of it but can't. How would I go about doing this?
class Point
{
public:
Point() { float x = 0, y = 0; }
void Input(int &count); //input values
Rectangle myRec;
private:
float x, y;
};
class Rectangle
{
public:
Rectangle(); //side1 - horizontal, side2 - vertical
void SetPoint(const Point point1, const Point point2, const Point point3, const Point point4) { LLPoint = point1; LRPoint = point2; URPoint = point3; ULPoint = point4; }
float CalcSides(Point LL, Point LR, Point UL, Point UR);
private:
Point LLPoint, LRPoint, ULPoint, URPoint;
float side1, side2, length, width, area, perimeter; //side1 - horizontal, side2 - vertical
};
float Rectangle::CalcSides(Point LL, Point LR, Point UL, Point UR)
{
side1 = (LR.x - LL.x);
}
How can I access the x and y values for the points I've created in the Rectangle class?
If you really want to do this, then you can make the classes friends.
class Rectangle;
class Point
{
friend class Rectangle;
public:
Point() { x = 0; y = 0; }
void Input(int &count); //input values
private:
float x, y;
};
More likely though, you simply want to add accessors to the Point class as it is fairly useless as is.
class Point
{
public:
Point() { x = 0; y = 0; }
void Input(int &count); //input values
float getX() const { return x; }
float getY() const { return y; }
private:
float x, y;
};
Or, if Point is really going to be so simple and not need to maintain any invariants at all, just expose x and y as public members.
Also, you probably don't want to have Point contain a Rectangle but rather refer to one either through a pointer or a reference, if it refers to one at all. After all, a Point can be useful without reference to a Rectangle (e.g. - maybe it's used for Triangles too).
Related
So i created the class Point and want to use it as the parameter of the constructor in the class Circle , but the error : There is no default constructor for class "Point" shows up and I dont know how to fix it. The code is represented below this text:
class Point {
private:
int x, y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
};
class Circle {
private:
int radius;
Point centre;
public:
Circle(Point q, int r) {
centre = q;
radius = r;
}
};
int main() {
Point obj = Point(3, 4);
Circle obj = Circle(obj, 3);
}
The first problem is that when the constructor Circle::Cirlce(Point, int) is implicitly called by the compiler, before executing the body of that ctor, the data members centre and radius are default initialized. But since you've provided a user-defined ctor Point::Point(int, int) for class Point, the compiler will not synthesize the default ctor Point::Point(). Thus, the data member centre cannot be default initialized.
To solve this you can use constructor initializer list as shown below. The constructor initializer list shown below, copy initialize the data member centre instead of default initializing it.
class Point {
private:
int x, y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
};
class Circle {
private:
int radius;
Point centre;
public:
//--------------------------vvvvvvvvvvvvvvvvvvvv--->constructor initializer list used here
Circle(Point q, int r): radius(r), centre(q)
{
}
};
int main() {
Point obj = Point(3, 4);
Circle circleObj(obj,4);
}
Demo
Additionally, you had 2 objects with the same name obj inside main.
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 8 months ago.
I have two classes that I want to define, Position and TangentVector, partially given as follows:
class Position
{
public:
Position(double x, double y, double z);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
void translate(const TangentVector& tangent_vector);
private:
double m_x;
double m_y;
double m_z;
}
class TangentVector
{
public:
Tangent(double x, double y, double z, Position position);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
private:
double m_x;
double m_y;
double m_z;
Position m_position;
}
The key thing to note with the classes is that TangentVector has a member of type Position (TangentVector depends on Position) while Position has a method that takes in an argument of type const TangentVector& (Position depends on TangentVector?).
For context's sake, Position is intended to represent a point on the unit sphere, and TangentVector describes a vector tangent to the sphere, with the origin of the vector specified by a Position. Since the definition of a VectorTangent requires a Position to be specified, it seems reasonable to say that VectorTangent depends on Position. However, now I want to define a function that takes a Position on the sphere, and "translates" it along the sphere by a direction and distance given by TangentVector. I would really like this translate method to live in the Position class, since it a function that modifies the state of Position. This would lend itself to the following usage, which I feel is fairly natural:
Position position{ 1.0, 0.0, 0.0 };
TangentVector tangent_vector{ 0.0, PI/2, 0.0, position };
position.translate(tangent_vector); // Now { 0.0, 1.0, 0.0 };
However, I fear that this results in some circular dependency. So...
Is this case an example of circular dependency? Is this case bad practice?
If so, how can this circular dependency be avoided? How can this code be modified such that it is in-line with good OOP practices?
(I considered making the Position m_position member a raw pointer instead. In this case, however, I intend m_position to be fully owned by TangentVector, rather than allow the possibility of it being altered external to the class. In the example usage code, I do not want the translate method to modify tangent_vector, which would happen if tangent_vector's constructor took in position as a pointer and stored it as a member.)
class Position takes only a reference to class TangentVector. Therefore you might pre-declare TangentVector as class TangentVector; before the declaration of class Position:
class TangentVector;
class Position
{
public:
Position(double x, double y, double z);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
void translate(const TangentVector& tangent_vector);
private:
double m_x;
double m_y;
double m_z;
};
Just like I'm creating member variables for x, y, and theta, how would I create member variables for the type vector<double> in the constructor? What I'm doing below doesn't seem to work.
Positioning::Positioning(double x, double y, double theta) {
this->globalX = x;
this->globalY = y;
this->globalTheta = theta;
vector<double> this->xs;
}
I have done #include <vector> and have std::vector<double> xs; in the private section of Positioning.hpp
EDIT
Header file:
namespace Position {
class Positioning {
private:
double globalX;
double globalY;
double globalTheta;
std::vector<double> xs;
public:
Positioning(double x, double y, double theta);
~Positioning();
void updateGlobalPosition(double left_dist, double right_dist, double rear_dist);
double getX();
double getY();
double getTheta();
};
}
This works
Positioning::Positioning(double x, double y, double theta) {
this->globalX = x;
this->globalY = y;
this->globalTheta = theta;
}
All your class variables are created automatically, you don't have to do anything special to create them.
The code in the body of the constructor above is not creating anything, it's assigning values to your variables. Of course if you want to give a member variable a particular value, then you have to do something, but they get created automatically.
In the shape.cpp I need set a default constructor for class Point, But I don't know how to do that.
Thanks
Point::Point(double _f, double _g){
f = 1;
g = 1;
}
Rectangle::Rectangle():Point(1, 1) {
x = 1;
y = 1;
}
Rectangle::Rectangle( Point q, double l, double w):x(l),y(w),Point(q) {
}
#include <iostream>
using namespace std;
class Point{
int f,g; //declaring point(f,g)
public :
Point(); //This will be your default constructor
Point(double _f,double _g); // Constructor for class Point
int getf(){ //just for purpose of display function() below. You can remove this once you understand code.
return f;
}
int getg(){ //just for purpose of display function() below. You can remove this once you understand code.
return g;
}
};
class Rectangle{
double l,w; //declaring variable to store length and width of Rectangle
Point pt; //whatever your use of point is
public :
Rectangle(Point q,double l,double w); //constructor for class Rectangle
void display(){ // displaying the class...just for demonstration
cout<<"l = "<<l<<endl;
cout<<"w = "<<w<<endl;
cout<<"pt = ("<<pt.getf()<<", "<<pt.getg()<<")\n";
}
};
//Defining the constructor of class Point
Point::Point(double _f, double _g):f(_f),g(_g){} // I have used initialiser list here
Point::Point():f(0),g(0){} // (your needed default constructor)
Rectangle::Rectangle( Point q, double l, double w):l(l),w(w),pt(q) {} //Defining the constructor of class Rectangle
int main()
{ //Demonstrating object creation and displaying of object r of class rectangle
Point p(1,2);
Rectangle r(p,5,10);
r.display();
}
I have attached the code that would help you understand about constructors and how to define them.
Hope this solves your question !
class Point{
int f,g;
public:
Point();
};
Point::Point(){
f = 1;
g = 1;
}
int main(){
Point *p = new Point() ;
}
I'm having memory leak problems with the following line of code:
auto state = newSpriteState();
Where these are the related functions:
class SpriteState {
protected:
Vector3 position;
int width, height;
double rotation, scaling;
int priority;
public:
SpriteState()
: position(0,0,0),
width(1), height(1),
rotation(0), scaling(1.0f),
priority(0)
{}
std::shared_ptr<SpriteState> newSpriteState()
{
return std::make_shared<SpriteState>();
}
};
class Vector3 {
private:
double x, y, z;
public:
Vector3( double x_, double y_, double z_ )
{
x = x_; y = y_; z = z_;
}
};
Intel Inspector continues to report that I'm having a memory leak in
the function newSpriteState(); more specifically std::make_shared<SpriteState>().
UPDATE
Judging from the comments, it seems there may be some external reason for this so here's more code:
bool Sprite::loadImage() {
auto state = newSpriteState();
initStateVector(0, state);
}
where:
class Sprite
{
public:
Sprite();
std::map<const int, const std::shared_ptr<SpriteState>> stateVector;
void initStateVector(const int line, std::shared_ptr<SpriteState>& state)
{
stateVector.clear();
stateVector.insert(std::make_pair( line, std::move(state) ));
}
void loadImage();
}
I've uploaded a simplified version of the Sprite class I'm actually using for clarity.
Basically, I'm allocating a shared_ptr<SpriteState> and sticking into a std::map in class Sprite.
The problem has been solved after an upgrade to vs12. My best estimation is that the problem had something to do with the tr1 implementation of smart pointers.