0

主.cpp:

#include <iostream>
#include "pokemonList.h"
void pokemonLookup();

int main() {
    pokemonLookup();
    return 0;
}

void pokemonLookup() {
    pokemonList pL;
    std::cout<<std::endl<<"What Pokemon do you want to look up? ";
    std::string pokemonLookup;
    std::cin>>pokemonLookup;
    pL.displayPokemon(pokemonLookup);
}

口袋妖怪列表.h:

#ifndef POKEMONLIST_H
#define POKEMONLIST_H
#include <iostream>

class pokemonList
{
    private:
        struct pokemonTemplate {
            std::string pokemonName;
            std::string pokemonMoves[3];
            int pokemonLevel;
            int baseATK;
            int baseDEF;
            int baseSPATK;
            int baseSPDEF;
            int baseSPEED;
            };
        pokemonTemplate bulbasaur;
        pokemonTemplate pikachu;
    public:
        void displayPokemon(std::string pokemon);
    protected:
};

#endif // POKEMONLIST_H

口袋妖怪列表.cpp:

#include "pokemonList.h"
/*
pokemonTemplate* bulbasaur() {
    bulbasaur.pokemonName = "Bulbasaur";
    bulbasaur.pokemonMoves[3];
    bulbasaur.pokemonLevel = 5;
    bulbasaur.baseATK = 10;
    bulbasaur.baseDEF = 10;
    bulbasaur.baseSPATK = 10;
    bulbasaur.baseSPDEF = 10;
    bulbasaur.baseSPEED = 10;
}

pokemonTemplate* pikachu() {
    pikachu.pokemonName = "Pikachu";
    pikachu.pokemonMoves[3];
    pikachu.pokemonLevel = 5;
    pikachu.baseATK = 8;
    pikachu.baseDEF = 10;
    pikachu.baseSPATK = 12;
    pikachu.baseSPDEF = 6;
    pikachu.baseSPEED = 15;
}
*/
void displayPokemon(std::string pokemon) {
    std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}

我意识到在 pokemonList.cpp 文件中我有一堆错误代码被注释掉了,这不是这个问题的意义所在。当我尝试编译时,我在 main.cpp 中得到一个错误,说:

D:/CodeBlocks/Projects/RelearningCPlusPlus/main.cpp:15: undefined reference to `pokemonList::displayPokemon(std::string)'
4

5 回答 5

6

您定义了一个函数displayPokemon(),但它没有定义为成员函数。要将其定义为类定义之外的成员函数,您需要提及类名:

void pokemonList::displayPokemon(std::string pokemon) {
    std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
于 2013-09-01T02:35:22.390 回答
2

它应该是

void pokemonList::displayPokemon(std::string pokemon){....
于 2013-09-01T02:35:59.177 回答
1

你需要改变这个:

void displayPokemon(std::string pokemon) {

对此:

void pokemonList::displayPokemon(std::string pokemon) {
于 2013-09-01T02:35:45.310 回答
0

迪特玛的回答解决了这个问题。

但我想补充一点,在这种情况下,最好通过引用传递参数而不是通过值传递std::string pokemon,从而减少开销。所以displayPokemon函数应该是:

void pokemonList:displayPokemon(std::string &pokemon) const {
    std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
};

另外,记得更改相应的成员函数声明。

于 2013-09-01T02:55:30.557 回答
0

displayPokemonfunction 被声明为pokemonList成员函数,但从未定义。

这个:

void displayPokemon(std::string pokemon) {
    std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}

是某个不是pokemonList类成员函数的其他函数(具有相同名称)的定义。要告诉编译器这是一个类pokemonList成员函数,您必须在其名称前加上pokemonList::

void pokemonList::displayPokemon(std::string pokemon) {
    std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}

或者,您可以在类displayPokemon内部定义pokemonList

class pokemonList
{
    private:
        struct pokemonTemplate {
            std::string pokemonName;
            std::string pokemonMoves[3];
            int pokemonLevel;
            int baseATK;
            int baseDEF;
            int baseSPATK;
            int baseSPDEF;
            int baseSPEED;
            };
        pokemonTemplate bulbasaur;
        pokemonTemplate pikachu;
    public:
        void displayPokemon(std::string pokemon) {
            std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
        }
    protected:
};

不同之处在于,在最后一种情况下,displayPokemon将是一个内联函数。

于 2013-09-01T08:48:49.470 回答