Including classes in headers

Hi everyone,

I'm having hard times figuring out what's wrong with my approach to including and using header files, especially with classes. What I am doing seems to be exactly what is supposed to be done according to books and tutorials on the net, but in the end it never work.

Here is how my main file begins:

1
2
3
#include "../std_lib_facilities.h"
#include "classes.h"


The classes.h contains:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED


struct token {
  int value;
  char type;

  token(int x, char y);
  token(char y);
};

#endif // CLASSES_H_INCLUDED 


And the classes.cpp the definitions:

1
2
3
4
5
6
7
8
#include "../std_lib_facilities.h"
#include "classes.h"

token::token(int x, char y)
:value(x), type(y) {}

token::token(char y)
:value(0), type(y) {}


It didn't link that way.

I also tried to add the declaration of the header to the cpp and it didn't work either:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "../std_lib_facilities.h"
#include "classes.h"


struct token {
  int value;
  char type;

  token(int x, char y);
  token(char y);
};

token::token(int x, char y)
:value(x), type(y) {}

token::token(char y)
:value(0), type(y) {}


I am getting "undefined reference to" errors.

Any ideas what I should do?

Thanks.
Last edited on
Change #include "../std_lib_facilities.h" to
#include "std_lib_facilities.h"
and put all the files in the same folder.
Make sure that you add classes.cpp to your project.
It didn't work...
Please post the full error messages.
||=== Build: Debug in exercises (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `ZN12token_streamC2Ev':|
C:\Users\LEO\Documents\Codeblocks\exercises\main.cpp|16|undefined reference to `token::token(char)'|
obj\Debug\main.o||In function `ZN12token_stream3getEv':|
C:\Users\LEO\Documents\Codeblocks\exercises\main.cpp|30|undefined reference to `token::token(char)'|
C:\Users\LEO\Documents\Codeblocks\exercises\main.cpp|41|undefined reference to `token::token(char)'|
C:\Users\LEO\Documents\Codeblocks\exercises\main.cpp|56|undefined reference to `token::token(int, char)'|
obj\Debug\main.o||In function `Z9calculatev':|
C:\Users\LEO\Documents\Codeblocks\exercises\main.cpp|173|undefined reference to `token::token(char)'|
||error: ld returned 1 exit status|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Make sure all your files are part of the same project.
They are all in the same project and in the same folder
What IDE are you using?
Had a similar problem with folder structure.
Try changing this
 
#include "../std_lib_facilities.h" 


to this

 
#include ".\\"folder_name"\\std_lib_facilities.h" 
It didn't work either. Isn't there something wrong with the code?
Im using codeblocks
The code you posted looks fine. Can you show us main.cpp as well.
Could you maybe upload the whole project to dropbox or somewhere else?
Yes the code appears to be fine, the errors seem to be linker errors not compile errors. This usually means that you didn't add one or more of the files to your project.

But note although the code appears fine you really don't need to include the std_lib_facilities.h file in either of the files you've shown. And note your error messages have nothing to do with this header file. Th errors are being caused because the linker can't find your class implementation functions, your class constructors in this case.

Last edited on
As others have said, these are linker errors.

All the error messages mention token::<something>. Presumably your token.cpp file did not get compiled and linked since the linker can not find any functions in the token class. You do have a token.cpp file, right? If so, what is in it?

In the following snippet, you are mixing declarations and definitions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "../std_lib_facilities.h"
#include "classes.h"

struct token {
  int value;
  char type;

  token (int x, char y);
  token (char y);
};

token::token (int x, char y) : value(x), type(y) 
{}

token::token (char y) : value(0), type(y) 
{}

You didn't indicate the file name for this snippet. I'm guessing because you have the struct declaration in this file, that this is a header (token.h) file.

You should not be including the definition of your constructors in a header file. This will result in multiply defined symbol linker errors if you include the header in more than one .cpp file. This is not your problem, but it is a problem none the less.

Note that it is okay to include your constructor definitions as part of the structure declaration (lines 9-10), but that is not what you have done.

Last edited on
Hello everyone,

Forgive me if i have missed something, but you are talking about classes and I see what looks like a constructor token::token(...) yet the code starts out struct token. Should that not be class token? I do not recall a struct needing a constructor, I thought that constructors are used for classes.

Just my thought.

Hope It is useful,

Andy
The only real difference between a struct and a class is the default access specifiers. A struct defaults to public access where a class defaults to private access. A struct can have member functions just like a class and can also use access sections (public, private, protected).

closed account (E0p9LyTq)
struct and class are somewhat interchangeable keywords in C++ that differ with the default access of members. By default (no access modifier specified) struct members have public access, class have private access.

http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c
Last edited on
Ok. Problem solved. So for some reason, like you said jlb, eventhough the classes.cpp file was both in the folder's project in the windows explorer AND in the codeblocks' project folder it didn't detect it as being part of the project... weird. So I had to add the cpp file to the project. It seemed redundant but apparently codeblocks works this way...
Thanks everyone for your help.
Topic archived. No new replies allowed.