C++中函数的多重定义[关闭]

问题描述 投票:0回答:0

我正在做一个小项目,我遇到了这个问题。

我在

Item
item.cpp
上了一堂课
.hpp
。我在它们单独的
HealingItem
BoostingItem
文件中制作了派生类
.cpp
.hpp
。由于它们都是从
Item
派生的,所以我需要在它们中都包含
item.hpp

但是,当我运行我的

main()
时,我得到这个错误:

item.cpp:(.text+0xd0): `Item::get_weight() const'的多重定义。

我该怎么办?

我添加了包括保障措施,但它没有解决问题。

对了,这是我的编译命令:

clang++ -Wall -Wextra -pedantic -fsanitize=undefined -std=c++20 -Werror item.cpp boostingItem.cpp healingItem.cpp -o main

这是我的

item.hpp

#ifndef ITEM_H
#define ITEM_H

#include <string>

class Item
{
public:
    Item(std::string name, unsigned int weight) : name(name), weight(weight){};

    // void use() const;
    std::string get_name() const;
    unsigned int get_weight() const;

private:
    const std::string name;
    const unsigned int weight;
};

#endif

这是我的

healingItem.hpp

#ifndef HEALINGITEM_H
#define HEALINGITEM_H

#include "item.hpp"
#include "pokemon.hpp"

class HealingItem : public Item
{
public:
    HealingItem(std::string name, unsigned int weight, unsigned int hp) : Item(name, weight), healingPower(hp){};

    unsigned int get_healingPower() const;
    void use(Pokemon p) const;

private:
    unsigned int healingPower;
};

#endif

更新:我也被要求提供

.cpp
文件,所以它们在这里:

item.cpp

#include "item.hpp"

std::string Item::get_name() const
{
    return name;
}

unsigned int Item::get_weight() const
{
    return weight;
}

healingItem.cpp

#include "healingItem.hpp"

unsigned int HealingItem::get_healingPower() const
{
    return healingPower;
}

这是我的

boostingItem.hpp

#ifndef BOOSTINGITEM_H
#define BOOSTINGITEM_H

#include "item.hpp"
#include "pokemon.hpp"

class BoostingItem : public Item
{
public:
    BoostingItem(std::string name, unsigned int weight, unsigned int bp) : Item(name, weight), boostingPower(bp){};

    unsigned int get_boostingPower() const;
    void use(Pokemon &p) const;

private:
    unsigned int boostingPower;
};

#endif

boostingItem.cpp

#include "boostingItem.hpp"

unsigned int BoostingItem::get_boostingPower() const
{
    return boostingPower;
}

void BoostingItem::use(Pokemon &p) const
{
    p.boost();
}

pokemon.hpp

#ifndef POKEMON_H
#define POKEMON_H

#include <string>

class Pokemon
{
public:
    Pokemon(std::string name, unsigned int hp, unsigned int ap, unsigned int heal)
        : name(name), healthPoints(hp), attackPoints(ap), healPoints(heal), maxHealth(hp){};

    std::string get_name() const;
    unsigned int get_hp() const;
    unsigned int get_max_hp() const;
    unsigned int get_ap() const;
    unsigned int get_heal() const;
    bool is_boosted() const;

    void set_hp(unsigned int heal);

    void heal();
    void boost();

    void attack(Pokemon &opponent);
    void take_damage(unsigned int dmg);

private:
    std::string name;
    unsigned int healthPoints;
    unsigned int attackPoints;
    const unsigned int healPoints;
    const unsigned int maxHealth;
    bool boosted = false;
};

#endif

pokemon.cpp

#include <cmath>
#include <cstdlib>
#include "pokemon.hpp"

std::string Pokemon::get_name() const
{
    return name;
}

unsigned int Pokemon::get_hp() const
{
    return healthPoints;
}

unsigned int Pokemon::get_max_hp() const
{
    return maxHealth;
}

unsigned int Pokemon::get_ap() const
{
    return attackPoints;
}

unsigned int Pokemon::get_heal() const
{
    return healPoints;
}

bool Pokemon::is_boosted() const
{
    return boosted;
}

void Pokemon::set_hp(unsigned int heal)
{
    unsigned int new_hp = healthPoints + heal;
    if (new_hp > maxHealth)
    {
        healthPoints = maxHealth;
    }
    else
    {
        healthPoints = new_hp;
    }
}

void Pokemon::heal()
{
    // function used to heal a pokemon that has a heal skill
    // pokemon has a heal skill if his attribute healPoints is positive

    set_hp(healPoints);

    // it doesn't let a pokemon heal himself over his max HP
}

void Pokemon::boost()
{
    boosted = true;
}

void Pokemon::attack(Pokemon &opponent)
{
    if (boosted)
    {
        opponent.take_damage(attackPoints);
        boosted = false;
    }
    else
    {
        int halfAP = round(attackPoints / 2);
        int dmg = halfAP + (rand() % (halfAP + 1)); // random value between half attackPoints and attackPoints
        opponent.take_damage(dmg);
    }
}

void Pokemon::take_damage(unsigned int dmg)
{
    int hp = healthPoints - dmg;
    if (hp < 0)
    {
        healthPoints = 0;
    }
    else
    {
        healthPoints = hp;
    }
}

最后

main.cpp

#include "ui.hpp"

int main(int argc, char *argv[])
{
    system("clear");
    Hero mc = Hero("name");
    Pokemon op = Pokemon("Charizard", 20, 100, 5);
    return 0;
}

在 main 中,我包含了 ui.hpp,其中我包含了 Hero.hpp,其中包含了所有其他文件。

如果没有提供所有必要的东西,我很抱歉,但我仍然是编程的新手(尤其是在 c++ 中)并且我是一个新的堆栈溢出用户,所以我没有以这种方式寻求帮助的经验:)

c++ include linker-errors clang++
© www.soinside.com 2019 - 2024. All rights reserved.