I recently moved from Java for C++ but now when I am writing my application I'm not interested in writing everything of the code in the main function I want in main function to call another function but this other function is in another .cpp file.
我最近从Java转到c++,但是现在当我在写应用程序时,我对在main函数中想要调用另一个函数的main函数中的所有代码都不感兴趣,但是这个函数在另一个。cpp文件中。
Let me explain better if you wouldn't understand:
I have one file: main.cpp
inside it I have main function.
如果你不理解,让我来解释一下:我有一个文件:main。cpp里面有主函数。
I have the second file: second.cpp
inside I have a function called second()
I want to call this function called second()
from my main function..
我有第二个文件:第二份。cpp里面有一个叫second()的函数我想从主函数中调用这个叫second()的函数。
Any help?
任何帮助吗?
3 个解决方案
#1
57
You must use a tool called a "header". In a header you declare the function that you want to use. Then you include it in both files. A header is a separate file included using the #include
directive. Then you may call the other function.
你必须使用一个叫做“头”的工具。在header中,您声明要使用的函数。然后将它包含在两个文件中。头文件是使用#include指令包含的单独文件。然后你可以调用另一个函数。
// other.h
void MyFunc();
// main.cpp
#include "other.h"
int main() {
MyFunc();
}
// other.cpp
#include "other.h"
#include <iostream>
void MyFunc() {
std::cout << "Ohai from another .cpp file!";
std::cin.get();
}
#2
14
You should have header files (.h) that contain the function's declaration, then a corresponding .cpp file that contains the definition. You then include the header file everywhere you need it. Note that the .cpp file that contains the definitions also needs to include (it's corresponding) header file.
应该有包含函数声明的头文件(.h),然后是包含定义的相应的.cpp文件。然后在任何需要的地方都包含头文件。注意,包含定义的.cpp文件也需要包含(它是相应的)头文件。
// main.cpp
#include "second.h"
int main () {
secondFunction();
}
// second.h
void secondFunction();
// second.cpp
#include "second.h"
void secondFunction() {
// do stuff
}
#3
5
In C/C++ you have header files (*.H). There you declare your functions/classes. So for example you will have to #include "second.h"
to your main.cpp
file.
在C/ c++中,有头文件(*. h)。在这里你声明你的函数/类。例如,你需要#include "second。你的主要h”。cpp文件。
In second.h
you just declare like this void yourFunction();
In second.cpp
you implement it like
在第二个。你只需要声明就像这个void你的函数();在第二个。cpp你可以实现它。
void yourFunction() {
doSomethng();
}
Don't forget to #include "second.h"
also in the beginning of second.cpp
不要忘记包含“秒”。也在第二阶段开始。
Hope this helps:)
希望这有助于:)
#1
57
You must use a tool called a "header". In a header you declare the function that you want to use. Then you include it in both files. A header is a separate file included using the #include
directive. Then you may call the other function.
你必须使用一个叫做“头”的工具。在header中,您声明要使用的函数。然后将它包含在两个文件中。头文件是使用#include指令包含的单独文件。然后你可以调用另一个函数。
// other.h
void MyFunc();
// main.cpp
#include "other.h"
int main() {
MyFunc();
}
// other.cpp
#include "other.h"
#include <iostream>
void MyFunc() {
std::cout << "Ohai from another .cpp file!";
std::cin.get();
}
#2
14
You should have header files (.h) that contain the function's declaration, then a corresponding .cpp file that contains the definition. You then include the header file everywhere you need it. Note that the .cpp file that contains the definitions also needs to include (it's corresponding) header file.
应该有包含函数声明的头文件(.h),然后是包含定义的相应的.cpp文件。然后在任何需要的地方都包含头文件。注意,包含定义的.cpp文件也需要包含(它是相应的)头文件。
// main.cpp
#include "second.h"
int main () {
secondFunction();
}
// second.h
void secondFunction();
// second.cpp
#include "second.h"
void secondFunction() {
// do stuff
}
#3
5
In C/C++ you have header files (*.H). There you declare your functions/classes. So for example you will have to #include "second.h"
to your main.cpp
file.
在C/ c++中,有头文件(*. h)。在这里你声明你的函数/类。例如,你需要#include "second。你的主要h”。cpp文件。
In second.h
you just declare like this void yourFunction();
In second.cpp
you implement it like
在第二个。你只需要声明就像这个void你的函数();在第二个。cpp你可以实现它。
void yourFunction() {
doSomethng();
}
Don't forget to #include "second.h"
also in the beginning of second.cpp
不要忘记包含“秒”。也在第二阶段开始。
Hope this helps:)
希望这有助于:)