Zone Zone -4 years ago 121
C++ Question

'GameObject': no appropriate default constructor available

I'm trying to create a game with GameObject being everything from tiles, player, enemy and walls. I'm trying to get my class Character (Parent to two more classes Player and Enemy) to the base class GameObject. I get the C2512 Error in the Character.cpp file when I'm trying to create the constructor for Characters. Can anyone point out what I could be doing wrong? Thanks in advance.

Characters.h

#ifndef CHARACTERS_H
#define CHARACTERS_H

#include <cstdlib>
#include <ctime> /*cstdlib and ctime are used to help with the pseudo randomness for events*/
#include <cstdio> /*allows remove function*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <string>
#include <vector>
#include "GameObject.h"

using namespace std;

class Character : public GameObject{
public:
Character();
~Character();
virtual void attack(vector<Character *>&characters) = 0;
void set_values(string nam, int hp, int str, int mag) {
name = nam;
health = hp;
strength = str;
magic = mag;
};
string name;
int health;
int strength;
int magic;

};


#endif


GameObject.h

#ifndef GAMEOBJECCT_H
#define GAMEOBJECT_H

#include "Direct3D.h"
#include "Mesh.h"
#include "InputController.h"

class GameObject {
protected:
Vector3 m_position;
float m_rotX, m_rotY, m_rotZ;
float m_scaleX, m_scaleY, m_scaleZ; //Not really needed for A1.
//Used for rendering
Matrix m_world;
Mesh* m_mesh;
Texture* m_texture;
Shader* m_shader;
InputController* m_input; //Might have enemies who react to player input.

float m_moveSpeed;
float m_rotateSpeed;

public:
//Constructor
GameObject(Mesh* mesh, Shader* shader, InputController *input, Vector3 position, Texture* texture);
~GameObject();
void Update(float timestep);
void Render(Direct3D* renderer, Camera* cam);


Characters.cpp

#include "Characters.h"
#include <cstdlib>
#include <ctime> /*cstdlib and ctime are used to help with the pseudo randomness for events*/
#include <cstdio> /*allows remove function*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <string>
#include <vector>
using namespace std;

void Character::attack(vector<Character *>&characters) {};

Character::Character() {

};
Character::~Character() {};

Answer Source

Your GameObject should have a default constructor (constructor with no parameters).

Because in Character::Character(), compiler will generate a call to GameObject's default constructor.

If you don't implement any constructor for a class GameObject, compiler will generate an empty default constructor for it. But you have implemented a constructor for class GameObject, so compiler will not do it. You should provide a default constructor for it by yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download