0

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.

  • 1
    I see declaration of `getCardHealth`, but where is definition of it? – Marek R Apr 01 '23 at 15:54
  • 1
    Why do you have `main` defined twice? Don't do that. – ALX23z Apr 01 '23 at 15:55
  • main is defined twice because CardCreator.cpp throws the original error posted ```'string' does not name a type; did you mean 'stdin'? ``` however if I have the code provided I get the ```undefined reference to `CardCreator::getCardHealth()' collect2.exe: error: ld returned 1 exit status``` error when compiling main.cpp – Google Wont Hide My Name Apr 01 '23 at 15:58
  • The definition is in the second file. CardCreator.cpp – Google Wont Hide My Name Apr 01 '23 at 15:59
  • You need to compile the two files together into a single executable. This is easiest to do with a build system like cmake. You'll also need to make sure you've implemented all the methods of `CardCreator` (or at least the ones you're using) – Alan Birtles Apr 01 '23 at 16:01
  • Well, you need to compile *both* files if you want definitions from *both* files to be visible in your program. – Yksisarvinen Apr 01 '23 at 16:01
  • That fixed it. I wasn't compiling them both at the sametime leading to a few errors being thrown. Thank you. – Google Wont Hide My Name Apr 01 '23 at 16:03
  • @Yksisarvinen it might not be too your taste but calling it "dumb" isn't very constructive. A cmake file for this could be a single line (`add_executable(app main.cpp CardCreator.cpp)`) then cmake will take care of all the compiler and linker flags, might even be easier than doing it by hand never mind less error prone – Alan Birtles Apr 01 '23 at 16:09

0 Answers0