简体   繁体   English

如何在C ++程序中包含自定义文件

[英]How to include Custom files in C++ program

How to include file 2 in file 1. What changes I need to make in file 2. 如何在文件1中包含文件2。我需要在文件2中进行哪些更改。

file 1 文件1

 #include <iostream>

 using namespace std;

int main()
{
cout<<"Hello World";

return 0;
}

file 2 文件2

 int otheFun()
 {
   cout<<"Demo Program";
   return 0;
 }

You do not include cpp files in to another cpp files. 您不将cpp文件包括在另一个cpp文件中。
Also, a c++ program can have only one main() function. 同样,一个c ++程序只能具有一个main()函数。
If you are trying to play around with a program which has multiple files, You will need to have something like this: 如果您尝试使用具有多个文件的程序,则需要具有以下内容:

file2.cpp file2.cpp

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


int printHelloWorld()
{
    std::cout<<"Hello World";

    return 0;
}

file2.h 文件2.h

 #ifndef FILE2_H    <----Lookup Inclusion Guards on google, this is important concept to learn.
 #define FILE2_H

 int printHelloWorld();

 #endif //FILE2_H

file1.cpp file1.cpp

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


 int main()
 {
     std::cout<<"Demo Program";
     printHelloWorld();
     return 0;
 }

What changes I need to make in file 2? 我需要在文件2中进行哪些更改?

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello world";
   cout << "Demo Program";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM