简体   繁体   中英

Undefined reference to function in class

main.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);
}

pokemonList.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

pokemonList.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;
}

I realize in the pokemonList.cpp file I have a bunch of Bad Code commented out, that isn't what this question is about. When I try to compile, I get a single error in the main.cpp saying:

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

You defined a function displayPokemon() but it isn't defined as a member function. To define it as a member function outside the class definition, you need to mention the class name:

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

它应该是

void pokemonList::displayPokemon(std::string pokemon){....

You need to change this:

void displayPokemon(std::string pokemon) {

to this:

void pokemonList::displayPokemon(std::string pokemon) {

Dietmar's answer solves the problem.

But I want to add that it is better to passing argument by reference instead of passing by value with std::string pokemon in this case, thus reducing overhead. So the displayPokemon function should be:

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

Also, remember to change the corresponding member function declaration.

displayPokemon function is declared as a pokemonList member function but is never defined.

This:

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

is a definition of some other function (with the same name) which is not a pokemonList class member function. To tell the compiler that this is a class pokemonList member function you must preceed its name with pokemonList::

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

Alternatively, you can define displayPokemon inside pokemonList class:

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:
};

The difference is that in the last case the displayPokemon will be an inline function.

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