user-defined Header files and classes

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?");
  }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  //inside library.h
  #include <iostream>
  #include <string>
  
  using namespace std;

  /**
  * Purpose: avoids crash of non-numeric integer
  * @args: Prompt 
  */
  int checkInt(string prompt) {
	int x = 0;
	cout << "\n" << prompt << endl;

	while ((!(cin >> x)) || cin.fail()){
		cout << "ERROR: Please try again" << endl;
		cin.clear();
		cin.sync();
		cin.ignore('\n', 10);
		cout << "\n" << prompt << endl;

	}
	return x;
  }


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++?
Last edited on
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.

You should also start using include guards. https://en.wikipedia.org/wiki/Include_guard
Last edited on
@Peter87 & anyone else that wants to help

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.

 
inline int 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++.
Last edited on
@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.
Topic archived. No new replies allowed.