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