Can't figure how includes/headers works

Hi,

I have a MyProject.cpp file where the "main" is, and I'm trying to create a new class but in a different file.

Most tutorials show how classes work in the same file, but I'd like to use different ones, and I'm not understanding how the include/headers work.

I have the MyProject.cpp, stdafx.h (with all the includes), MyClass.h (with the prototypes) and MyClass.cpp.

I'm getting alot of errors on the includes, what include lines should I have to make my "MyClass" useable in the "MyProject" main body?
I love pseudocode. Do something like this:

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
26
27
28
29
30
31
32
33
34
35
36
//headerFile.h
#ifndef mysuperflyingmonkieslikesugar
#define mysuperflyingmonkieslikesugar

#include <any header files used in class>

class myclass{
protected:
int x,y,z;
public:
void method();
void anotherMethod();
}
#endif

//headerFile.cpp

#include "headerFile.h"

myclass::method(){
//do method stuff
}
myclass::anotherMethod(){
//more method stuff
}

//main.cpp

#include "headerFile.h"
#include <other necessary header files>

int main(){
myclass instanceOfMyclass;

instanceOfMyclass.method();
}


edit: more realistic pseudocode
Last edited on
I would like to add that the reason for the incredibly long name on the #ifndef at the start, is so that there is no chance that any library you use will have that macro defined

Edit: I would also like to add that doStuff() was not declared as a function of myclass!!11!! Programs not gonna compile!!1
Last edited on
I tried this, and the error I get from this is "fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

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
26
27
28
29
30
31
32
33
34
35
36
37
//This is the class I'm trying to create and use
//MyClass.h
class MyClass
{
    public:
        MyClass();
        ~MyClass();

        FILE myFile;
        void WriteToFile();
};

//MyClass.cpp
#include "MyClass.h"
MyClass::MyClass()
{
    //some code here
}
//more code

//stdafx.h
#pragma once
#include "targetver.h" //this came when I created the project
#include "MyClass.h"

#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <iostream>

//MyProject.cpp
#include "stdfax.h"

int _tmain(int argc, _TCHAR* argv[])
{
    //can't instantiate my class here, I get an undefined type
}


This is what I have right now.

In the MyClass.cpp file, I use types from the libs in the stdafx.h, how should I include that in these files?
If I add a #include "MyClass.h" in the stdafx.h file, I get the following error: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int in the line "FILE myFile;"

When I created the project, I had the option "precompiled header" checked also, but I don't know exactly what that does...

How can I get this to work?
The error is refering to line 9. I think it should be a pointer like this FILE* myfile; (style preference) Also I'm pretty sure that it is defined in "stdio.h" which needs to be included in your header if you're going to use it. See My Reference: http://www.cplusplus.com/reference/clibrary/cstdio/FILE/

EDIT: This is the reason that I talk down about MS Visual studios, it doesn't teach you how to properly link your files. Just you wait until you want to use a specialized DLL file that doesn't reside in the %SYSTEMROOT%
Last edited on
Thanks, adding the FILE* myFile; and a #include "stdafx.h" in the MyClass.cpp worked.

I think I've understood now how the includes work, I was assuming it worked like web programming "includes".
I found this site and helped alot: http://www.learncpp.com/cpp-tutorial/19-header-files/ in case anyone's interested.

So from what I could understand, every .cpp file is compiled into an object, and the header isn't necessarily attached to it's own .cpp, instead it's just a helpful file for other .cpp files to understand what's in that file.

Thanks alot for the help!
You kind of missed the point I was trying to make... but as long as it's working and you have some way of doing this that is exceptable for you then good luck on your future endevours.
computergeek you have no idea how annoying it can get. microsoft vc 10 express lets you do all kindas of stuff without including the right headers. it especially sucks when you post the code online when it works for you but nobody else. i should switch, but i like a pretty IDE
Lol I wish windows had XCode...

@shakazahn Good! (i think) I'm glad you understand inclusion! Do you understand the need for a paired cpp and header file?
When just goofing around and trying out different concepts, I find it helpful in VC++ to create an empty project and just add the files you need on your own. To add a header file, right click 'Header Files' -> 'Add' -> 'New Item' -> select 'Header file (.h)' from the templates, name the file and click 'Add'. Go through the same procedure for your cpp files. This way you can control your includes and see what you actually need to include. To me, stdafx.h con-volutes things. So, for instance, I'm writing a fraction class just to hone my skills. I have a total of 3 files: 1 header (Fraction.h) and 2 source files (main.cpp and Fraction.cpp). The main.cpp file contains the main() function and is where I test my class. My includes look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Fraction.h

#ifndef FRACTION_H
#define FRACTION_H

#include <iostream>

namespace my_math
{

  class Fraction
  {
     // bunch of code...
  };

} // end namespace
#endif 

1
2
3
4
5
6
7
8
9
10
// Fraction.cpp

#include "Fraction.h"

namespace my_math
{

  // lots and lots of code

} // end namespace 

1
2
3
4
5
6
7
8
9
10
11
12
// main.cpp

#include <iostream>
#include "Fraction.h"

using namespace std;
using namespace my_math;

int main(const int argc, const char* argv[])
{
     // test code
}


Hope this helps!
Topic archived. No new replies allowed.