0

These functions all worked when I had them in the main.cpp file, however I have now moved these to a header file.

Here is my general_functions.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include "general_functions.hpp"
using namespace std;


bool check_if_room_exists(int room_number){
    string room_number_full = to_string(room_number) + ".txt";
    ifstream new_file;
    new_file.open(room_number_full);

    if(new_file.is_open()){
        return true;
    }else{
        return false;
    }
    }

int get_new_room_number(){
    int room_number;
    cout<<"What room number do you want this room to have?"<<endl;
    cout<<"Enter your choice: ";
    cin>>room_number;

    while(!room_number || check_if_room_exists(room_number) == true){
        // Taken from StackOverflow to avoid endless loop: https://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a-number
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        // End of code taken from StackOverflow
        if(check_if_room_exists(room_number) == true){
            cout<<"A room with that number already exists.\nEnter your new choice: ";
        }else{
            cout<<"Please ensure you enter only numeric values for the room number.\nEnter your new choice: ";
        }
        cin>>room_number;
    }
    return room_number;
};

int get_cost_per_night(){
    int cost;
    cout<<"Please enter the cost per night for this room: ";
    cin >> cost;

    while(!cost || cost < 1){
        // Taken from StackOverflow to avoid endless loop: https://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a-number
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        // End of code taken from StackOverflow
        if(!cost){
            cout<<"Please ensure you enter only numeric values for the cost per night.\nEnter your new choice: ";
        }else{
            cout<<"The price is less than £0. Please enter a value above £0: ";
        }
        cin>>cost;
    }
    return cost;
}

int get_room_number(){
    int room_number;
    cout<<"Please enter the room number you wish to view: ";
    cin >> room_number;

    while(check_if_room_exists(room_number) == false){
        // Taken from StackOverflow to avoid endless loop: https://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a-number
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        // End of code taken from StackOverflow
        if(check_if_room_exists(room_number) == false){
            cout<<"That room doesn't exist enter a new one: ";
        }

        cin>>room_number;
    }
    return room_number;
}

void delete_room(int room_number){
    remove((to_string(room_number) + ".txt").c_str());
}

This is the general_functions.hpp file:

#ifndef general_functions_hpp
#define general_functions_hpp

#include <stdio.h>
using namespace std;

bool check_if_room_exists(int);
int get_new_room_number();
int get_cost_per_night();
int get_room_number();
void delete_room(int);
#endif

And finally, this is how I am importing the headerfile into my main.cpp file:

#include "general_functions.hpp"

This causes the following error to arise:

Undefined symbols for architecture x86_64:
  "delete_room(int)", referenced from:
      single_room_manager(SingleRoom) in main-f8f1e8.o
  "get_room_number()", referenced from:
      _main in main-f8f1e8.o
  "get_cost_per_night()", referenced from:
      single_room_manager(SingleRoom) in main-f8f1e8.o
      create_single_room() in main-f8f1e8.o
  "get_new_room_number()", referenced from:
      single_room_manager(SingleRoom) in main-f8f1e8.o
      create_single_room() in main-f8f1e8.o
  "login()", referenced from:
      _main in main-f8f1e8.o
ld: symbol(s) not found for architecture x86_64

From the research I have done, I believe this is being caused by the definitions of the functions not matching what they actually are, however from what I can see, they match up and shouldn't cause any issues.

Help would be appreciated!

Thanks

wtreston
  • 1,051
  • 12
  • 28
  • 1
    It looks like `general_functions.cpp` is not being built. – drescherjm Apr 09 '19 at 12:56
  • How do you build your code? From the command-line? Using an IDE? With a `Makefile`? – Some programmer dude Apr 09 '19 at 12:57
  • @Someprogrammerdude I build it in iTerm2 with ```make main``` and then run it with ```./main``` – wtreston Apr 09 '19 at 12:58
  • 1
    So, a `Makefile`. Have you edited it to build with `general_functions.cpp`? – Some programmer dude Apr 09 '19 at 12:59
  • @Someprogrammerdude I haven't made any file to run with. I just type the command into the cmd. I have previously added another headerfile but again didn't edit anything – wtreston Apr 09 '19 at 13:01
  • 1
    Then you need to learn how `make` works. And to talk whoever set up the environment for you (and probably wrote the `Makefile` initially). And a fun exercise for you: If you run the command `ls`, does it list your files? What *other* files are listed? Is there one called `Makefile`? If you do `cat Makefile`, you will see the instructions used by the `make` program, but your new file (`general_functions.cpp`) will not be listed anywhere I guess. – Some programmer dude Apr 09 '19 at 13:12
  • btw if you try g++ general_functions.cpp you will see that your .cpp contains errors. That's what your future makefile will do before compiling main.cpp... – Aname Apr 09 '19 at 14:58

0 Answers0