EDIT: SOLVED
Make sure you compile all files together! For anyone new and can't slog through the other question.
I am trying to compile a main.cpp file that references my "CardCreator.h" file. When I try to compile the "main.cpp" file it throws the error undefined reference to `CardCreator::getCardHealth()' collect2.exe: error: ld returned 1 exit status
also receiving 'string' does not name a type; did you mean 'stdin'?
when I remove the second int main() from the CardCreator.cpp file.
My code looks like:
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 - should note the function getCardName() contains some code that doesn't do much.
#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
I'm not sure why I am getting the error.