简体   繁体   中英

I am having issues with my header file and implementation file in c++

So I am creating a basic layout for a game and I have very little experience with separating classes from my main file and making two new files out of it (implementation file, .h file). I somehow messed up with some of the separation and I can't figure out where exactly I went wrong. I also am having trouble figuring out how to go about creating an object from these two new files in my main. for example, I will show you the ORIGINAL class, then my .h then my .cpp.

******ORIGINAL CLASS******

class Character{

public:
    string name;
    int health;

    Character(){
        setName("Unknown Caster");
        setHealth(20);
    }
    Character(string name, int health){
        setName(name);
        setHealth(health);
    }
    void setName(string x){
        cout << "What is your name?" << endl;
        cin >> x;
        name = x;  
    }
    int setHealth (int health){
        if(health < 0){
            health = 0;
        }
        this-> health = health;
        return health;
    }
    string getName(){
        return name;
    }
};

*****END OF ORIGINAL CLASS*****

*****START OF .h FILE*****

#ifndef Character_h
#define Character_h
using namespace std;

class Character{

public:
    string name;
    int health;

    Character();///default constructor

    Character(string name, int health);

    void setName(string x){  
    }

    int setHealth (int health){
    }

    string getName(){
        return name;
    }
};

#endif

*****END OF .h FILE*****

*****START OF .cpp FILE*****

#include <iostream>
#include <string>
#include "Character.h"


Character::Character(){
    setName("unknown caster");
    setHealth(20);
    }

Character::Character(){
    setName(name);
    setHealth(health);
    }
void Character::setName(){
    cout << "What is your name?" << endl;
    cin >> x;
    name = x;
}

int Character::setHealth(){
    if(health < 0){
        health = 0;
    }
    this-> health = health;
        return health;
}
string Character::getName(){
    return name;
}

****END OF .cpp FILE*****

My code was running relatively smoothly before I separated the one class into two different files, so I am inclined to believe that I did it incorrectly. My questions are: What did I do wrong? and how do I go about making an object from this class in my main? Thank you for your time in advance!

When you split out the header and cpp, don't leave any definitions in the header, for example these

void setName(string x){  
}

int setHealth (int health){
}

string getName(){
    return name;
}

should be

void setName(string x);
int setHealth (int health);
string getName();

Just because you emptied out all of the code between { and } , what you ended up doing is defining these functions to essentially do no work. Then you tried to redefine them again in the cpp file.

You are defining functions in the class twice, which counts as two defined functions that have the same name, return type and parameters, which is an error.

Declaration:

type func_name(parameter_type , parameter_type);

Definition:

type func_name(parameter_type name, parameter_type name2) {
    //code
}

You want to declare once and define once per program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM