Are you able to include and use functions from a self-defined header file inside of a class?
1 2 3 4 5 6 7 8
//inside of Character.h
#include "library.h" // this is my header file that i use for all my programs
class Character
{
//under public
void tester();
}
1 2 3 4 5 6 7 8
//inside of Character.cpp
#include "Character.h"
void Character::tester()
{
int play = checkInt("Would you like to play?");
}
There is nothing wrong with my library file because i have used it over many programs without error. When i try to use the file inside of a class like above it goes crazy and gives off a whole list of errors about each function. Is this allowed in c++?
When you get a long list of errors you should always start with the first one because many of the ones below could be side effects of the first one.
One problem is that you've forgot the semicolon after the Character class definition.
Another problem that you'll notice if you try to include library.h from multiple source files is that the checkInt function gets defined multiple times. This is not allowed for regular non-inline functions. The solution to this problem is either to make the function inline or to define it in a source file.
I am new to c++ i just started classes this semester. I have the semicolon in my actual code it was just a typo converting it over here. Would it even be possible to make all the functions in the library.h header file inline? They are all defined in the .h file right now. Also i use #pragma once at the beginning of library.h is using the #ifndef method any better?
Would it even be possible to make all the functions in the library.h header file inline?
Yes. Just place the inline keyword in front.
inlineint checkInt(string prompt) {
Also i use #pragma once at the beginning of library.h is using the #ifndef method any better?
They both accomplish the same thing. #pragma once is obviously easier to use and most modern compilers seem to support it but it's not part of standard C++.
@Peter87
Sorry for the late reply, I was busy working on the program which is a text-based game for my class, it seems that making each of the functions inline fixed the problems I had with all the errors. I looked at them more and it was saying that they were already defined in Character.obj. So far things are going smoothly. Thank you so much for the help. Also I have switched to using the #ifndef method just incase, because I have heard that that is better than using #pragma once.