Hey guys the dude that asks newbie questions here again.
Today i have a question regarding using more then 1 .cpp file a quick example:
I have the math part of my program in one cpp and the general rest in the int main .cpp file and i pass variables between these 2. Now i just get errors saying it cant find the variables etc.
I am sorry but that dosent really help me at all, i know what a header file does and how it works in general. Thats not what im looking for here. exactly how do i use more then one .cpp file in one project is the question.
- Don't use globals. Pass variables to functions instead. There are situations where globals are appropriate, but it's not nearly as often as you think (in fact it's quite rare)
- Never #include .cpp files. That defeats the entire point of having multiple cpp files (not to mention it will cause all sorts of linker errors)
@ Da0omph:
Yes you can do that. The problem in your code comes from you trying to call someOtherFunction before you declared it.
You're calling someOtherFunction on line 8 of file2.cpp, yet the compiler doesn't know it exists until line 11. You either need to move someOtherFunction so that it is defined before SomeFunction, or you need to put a prototype of someOtherFunction above SomeFunction:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// file2.cpp
#include "myheader.h"
#include <iostream>
void someOtherFunction(); // <- Prototype it
void SomeFunction()
{
someOtherFunction(); // <- now you can call it
}
void someOtherFunction() {
std::cout << "Print from another cpp file!";
}