Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Inheritance and Constructors

trying to work out how to use constructors with an inherited class. I know this is very much wrong, I've been writing C++ for about three days now, but here's my code anyway:

clientData.h, two classes, ClientData extends Entity :

#pragma once

class Entity
{
public:
 int x, y, width, height, leftX, rightX, topY, bottomY;

 Entity(int x, int y, int width, int height);
 ~Entity();
};

class ClientData : public Entity
{
public:
 ClientData();
 ~ClientData();
};

and clientData.cpp, which contains the functions:

#include <iostream>
#include "clientData.h"
using namespace std;

Entity::Entity(int x, int y, int width, int height)
{
 this->x = x;
 this->y = y;
 this->width = width;
 this->height = height;

 this->leftX = x - (width/2);
 this->rightX = x + (width/2);
   this->topY = y - (height/2);
 this->bottomY = y + (height/2);
}

Entity::~Entity()
{
 cout << "Destructing.\n";
}

ClientData::ClientData()
{
 cout << "Client constructed.";
}

ClientData::~ClientData()
{
    cout << "Destructing.\n";
}

and finally, I'm creating a new ClientData with:

ClientData * Data = new ClientData(32,32,32,16);

Now, I'm not surprised my compiler shouts errors at me, so how do I pass the arguments to the right classes?

The first error (from MVC2008) is error C2661: 'ClientData::ClientData' : no overloaded function takes 4 arguments

and the second, which pops up whatever changes I seem to make is error C2512: 'Entity' : no appropriate default constructor available Thanks.

like image 945
DizzyDoo Avatar asked Jan 28 '26 16:01

DizzyDoo


1 Answers

Currently the constructor for the Client data class wont work. You will need to make a constructor for Client data like:

ClientData(int x, int y, int width, int height): Entity(x, y, width, height)

if you want to call

new ClientData(32,32,32,16);
like image 120
shuttle87 Avatar answered Jan 31 '26 06:01

shuttle87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!