C++ Header File Questions!

Asked 2 years ago, Updated 2 years ago, 117 views

Player.h

#ifndef_PLAYER_H_
#define _PLAYER_H_

class Player {
private:
    char * id;
    char * job;
    int level;
    int hp;

public:
    void setId(char * id);
    void setJob(char * job);
    void setLevel(int level);
    void setHp(int hp);
};

#endif

Player.cpp

#include <iostream>
#include "Player.h"

using namespace std;
//default constructor
void Player() {
    cout << "Create an object.";
}

//Initial value generator (parameter)
void Player(char * id, char * job, int level, int hp) {
    setHp(10); // Error output here! (Identifier "setHp" is not defined.)
}


//Setter
void Player::setId(char * id) {
    this->id = new char[strlen(id) + 1];
    strcpy_s(this->id, strlen(id) + 1, id);
}

void Player::setJob(char * job) {
    this->job = new char[strlen(job) + 1];
    strcpy_s(this->job, strlen(job) + 1, job);
}

void Player::setLevel(int level) {
    this->level = level;
}

void Player::setHp(int hp) {
    this->hp = hp;
}

I'm trying to call the function defined below in the constructor, but I can't find itcrying Maybe it's because I'm doing Java, but I'm so worn out! Help!

c++ header-files

2022-09-22 20:31

1 Answers

If you define a member function outside of the class,

class name::method name (...)
{
}

You have to write with.

For constructors,

Player::Player(){
    // Come on, come on

Please write it like this~


2022-09-22 20:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.