簡體   English   中英

我需要幫助在C ++ LNK2005和LNK1169中分離類代碼

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

我是C ++的新手,我必須創建一個簡單的視頻游戲,它有一個敵人類,當我嘗試將敵人的代碼與main.cpp分開時,問題就來了,創建了enemy.h和enemy.cpp,我跟着我在互聯網上看到的所有說明,但它一直向我顯示錯誤信息,希望你們能幫助我。

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文件

#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:我仍然不知道如何在c ++中實現setcursorposition,如你所見。

你已經聲明了enemy()但沒有定義它。 如果聲明默認構造函數,請確保定義它(可能在.cpp文件中)

您獲得的錯誤意味着您違反了ODR(一個定義規則) 換句話說,當你試圖將你的enemy類與你的主人分開時,你沒有遠離那里的所有部分並最終在多個cpp文件中使用相同的代碼。

作為旁注,看起來你忘了定義你的敵人:: enemy()構造函數,或忘記將它從main.cpp移動到enemy.cpp?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM