-
Visual Studio 2005 (C++)
I am trying to do a multiple file program. I have class and function declarations in header files, and their corresponding definitions in .cpp source files. If I use the #include directive explicity for each filename, my project will compile just fine.
Code:
using namespace std;
#include fraction.h
#include func.h
#include fraction.cpp
#include func.cpp
main()
{
//bla bla bla bla
//
}
If you know Visual Studio 2005, you might be able to help with this, for this does NOT work
Header Files
-fraction.h
-func.h
Reference Files
Source Files
-Program.cpp (main source file)
-fraction.cpp
-func.cpp
Yes, I did tell the compiler to look in the project directory for each of those files
in Tools-->Options-->Projects and Solutions-->VC++ Directiories. What else must I do to make the compiler accept these files? If I try to compile it the way shown just above here, I get a vomit of error messages.
-
Do you have a good reason for including source files? What sort of errors do you get? You're saying it does not work when your solution explorer is laid out like this?
Code:
[Header Files]
-fraction.h
-func.h
[Resource Files]
[Source Files]
-Program.cpp
-fraction.cpp
-func.cpp
-
That is precisely what I am saying.
If I include just the header files and keep the function definitions in the program.cpp, things will work. The errors that I get involve arguments to the functions in the included .cpp files. I use istream and ostream objects, as well as a class object I created. My reason for doing this is becuase it is part of an assignment for a class=P. I have a feeling it has to do with how to manipulate the compiler menus......the code is all good, it complies if I throw ALL of it in the program.cpp file.
-
The only time you might include source files is if you're using templates, and then it should be at the bottom of your header file. There's been plenty of examples of how to split your project into multiple source files, but here you go:
Code:
//program.cpp
#include "func.hpp" //hpp extension might be preferable
int main()
{
//...
}
Code:
//func.hpp
#ifndef FUNC_H_ //arbitrary macro name for include guard
#define FUNC_H_
//function prototypes and inline and template function definitions in headers
void Func();
inline void Func2()
{
//...
}
template <class T>
void Func3()
{
//...
}
#endif
Code:
//func.cpp
#include "func.hpp"
void Func()
{
//...
}
Edit: And if this doesn't help, I think we might need a more specific/complete example from you.
-
>> I get a vomit of error messages.
You can post them, or at least the first few.