简体   繁体   English

我需要帮助在C ++ LNK2005和LNK1169中分离类代码

[英]I need help separating class code in C++ LNK2005 and LNK1169

I'm new at C++ and I have to create a simple video game, that has an enemy class, the problem comes when I try to separate the enemy code from the main.cpp, creating enemy.h and enemy.cpp, I followed all instructions I saw on the internet but it keeps showing me the error message I hope you guys can help me. 我是C ++的新手,我必须创建一个简单的视频游戏,它有一个敌人类,当我尝试将敌人的代码与main.cpp分开时,问题就来了,创建了enemy.h和enemy.cpp,我跟着我在互联网上看到的所有说明,但它一直向我显示错误信息,希望你们能帮助我。

enemy.cpp file enemy.cpp文件

#include "enemy.h"

enemy::enemy(int _hp, int _attackValue, string _name) {
    hp = _hp;
    attackValue = _attackValue;
    name = _name;
}

void enemy::attack(enemy agressor, enemy objective) {
    objective.set_hp(objective.hp - agressor.attackValue);
    objective.showinfo(objective, 2);
}

void enemy::showinfo(enemy enemy, int hero) {
    if (hero == 1) {
        cout << "     \n\n\n\n\n\n\n";
        cout << enemy.name;
        cout << "     \n\n\n\n\n\n\n\n";
        for (int i = enemy.hp / 5; i > 0; i--) {
            cout << "|";
        }
        cout << "     \n\n\n\n\n\n\n\n\n";
        cout << enemy.hp;
    }
    else {
        cout << "                                   \n\n\n\n\n\n\n";
        cout << enemy.name;
        cout << "                                   \n\n\n\n\n\n\n\n";
        for (int i = enemy.hp / 5; i > 0; i--) {
            cout << "|";
        }
        cout << "                                   \n\n\n\n\n\n\n\n\n";
        cout << enemy.hp;
    }
}

int enemy::get_hp() {
    return hp;
}

void enemy::set_hp(int _hp) {
    hp = _hp;
}

int enemy::get_attackValue() {
    return attackValue;
}

string enemy::get_name() {
    return name;
}

enemy.h file enemy.h文件

#pragma once

#ifndef enemy_H
#define enemy_H

class enemy {

private:

    int hp, attackValue;
    string name;
public:
    enemy();
    enemy(int, int, string);
    void attack(enemy, enemy);
    void showinfo(enemy, int);
    int get_hp();
    void set_hp(int hp);
    int get_attackValue();
    string get_name();
};

#endif // !enemy_H

PD: I still don't know how to implement setcursorposition in c++ as you can see. PD:我仍然不知道如何在c ++中实现setcursorposition,如你所见。

You have declared enemy() but haven't defined it. 你已经声明了enemy()但没有定义它。 If you declare the default constructor, make sure you define it (possibly in your .cpp file) 如果声明默认构造函数,请确保定义它(可能在.cpp文件中)

That error that you get means that you violated ODR (one definition rule) . 您获得的错误意味着您违反了ODR(一个定义规则) In other words, when you tried to separate your enemy class from your main you didn't remote all the parts from there and ended up with the same code in multiple cpp files. 换句话说,当你试图将你的enemy类与你的主人分开时,你没有远离那里的所有部分并最终在多个cpp文件中使用相同的代码。

As a side note, looks like you forgot to define your enemy::enemy() constructor, or forgot to move it from main.cpp to enemy.cpp? 作为旁注,看起来你忘了定义你的敌人:: enemy()构造函数,或忘记将它从main.cpp移动到enemy.cpp?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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