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;
}
我在pokemonList.cpp文件中发现我有一堆Bad Code被注释掉了,这不是这个问题的内容。当我尝试编译时,我在main.cpp中遇到一个错误:
D:/CodeBlocks/Projects/RelearningCPlusPlus/main.cpp:15: undefined reference to `pokemonList::displayPokemon(std::string)'
答案 0 :(得分:6)
您定义了一个函数displayPokemon()
,但未将其定义为成员函数。要将其定义为类定义之外的成员函数,您需要提及类名:
void pokemonList::displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
答案 1 :(得分:2)
应该是
void pokemonList::displayPokemon(std::string pokemon){....
答案 2 :(得分:1)
你需要改变这个:
void displayPokemon(std::string pokemon) {
到此:
void pokemonList::displayPokemon(std::string pokemon) {
答案 3 :(得分:0)
Dietmar的回答解决了这个问题。
但我想补充一点,最好是通过引用传递参数,而不是在这种情况下用std::string pokemon
传递值,从而减少开销。所以displayPokemon
函数应该是:
void pokemonList:displayPokemon(std::string &pokemon) const {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
};
另外,请记住更改相应的成员函数声明。
答案 4 :(得分:0)
displayPokemon
函数被声明为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
将是内联函数。