我正在尝试编译一个引用我的“CardCreator.h”文件的 main.cpp 文件。当我尝试编译“main.cpp”文件时,它会抛出错误
undefined reference to `CardCreator::getCardHealth()' collect2.exe: error: ld returned 1 exit status
我的代码看起来像:
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "CardCreator.h"
void myFunction() {
std::string name = "function name";
std::cout << name << std::endl;
}
int main(int argc, char** argv){
CardCreator machine;
machine.getCardHealth();
std::cout << "hello world" << std::endl; myFunction();
}
CardCreator.cpp - 应该注意函数 getCardName() 包含一些不做太多的代码。
#include <iostream>
// #include <string>
#include <fstream>
using namespace std;
#include "CardCreator.h"
bool CardCreator::fileCheck()
{
if (fopen("card.json", "r"))
{
cout << "found" << endl;
return true;
}
else
{
cout << "Not found" << endl;
// ofstream MyFile("card.json", ios_base::app);
// MyFile.close();
return false;
}
}
// methods go here
std::string CardCreator::getCardName(){
cout << "enter card name: " << endl;
// getline(cin,name);
// // cin >> name;
// cin.getline(name, 20) >> ws;
// cout << "the card is called: " << name << endl;
// this fix came from [https://stackoverflow.com/questions/26071275/c-while-loop-and-getline-issue]
// must clear the buffer for the while loop to allow input(break the loop)
// getline(cin, name);
// cin.ignore(256,'\n');
bool validInput = false;
while (!validInput)
{
cout << "enter card name: " << endl;
getline(cin >> ws, name);
if (name[0] != '\0')
{
cout << "The name is: " << name << endl;
validInput = true;
}
else
{
cout << "Invalid input. Please try again." << endl;
}
}
return name;
}
//file continues
int main() {}
CardCreator.h
#ifndef CARDCREATOR_H
#define CARDCREATOR_H
#include <string>
class CardCreator {
private:
bool isCreature;
bool isCastable;
public:
// change to char
std::string name;
int hp;
int atk;
int mana;
int cards_to_create;
bool fileCheck();
std::string getCardName();
int getCardAtk();
int getCardHealth();
int getCardMana();
int CardsToCreate();
void saveCard(std::string cardName,int cardatk, int cardhp, int cardmana);
};
#endif
我不确定为什么会出现错误。