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
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?"
//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...
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%
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.
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
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"
usingnamespace std;
usingnamespace my_math;
int main(constint argc, constchar* argv[])
{
// test code
}