C++ 第一个程序带有头文件和2.cpp文件

C++ 第一个程序带有头文件和2.cpp文件,c++,visual-c++,c++11,C++,Visual C++,C++11,我正在写一个程序,其中我需要两个不同的.cpp文件,第一个是我的main文件,第二个是main中使用的一些函数。我是否还需要一个头文件进行初始化?我不太清楚如何处理这件事。。感谢您的帮助 当您想使用多个.cpp文件时- .h文件基本上用于将.cpp文件链接在一起 您可以将函数声明放在.h文件中 您将在主.cpp文件中包含.h文件 您可以在functions.cpp文件中编写函数,还可以包括.h文件 查看此链接了解更多信息-。这应该适合您 您的main.cpp文件: // main.cpp #

我正在写一个程序,其中我需要两个不同的.cpp文件,第一个是我的main文件,第二个是main中使用的一些函数。我是否还需要一个头文件进行初始化?我不太清楚如何处理这件事。。感谢您的帮助

当您想使用多个
.cpp
文件时-

  • .h
    文件基本上用于将
    .cpp
    文件链接在一起
  • 您可以将函数声明放在
    .h
    文件中
  • 您将在主
    .cpp
    文件中包含
    .h
    文件
  • 您可以在functions
    .cpp
    文件中编写函数,还可以包括
    .h
    文件
查看此链接了解更多信息-。

这应该适合您

您的
main.cpp
文件:

// main.cpp
#include "header.h" // bring in the declarations

// some function that performs initialization.
// it is static so it cannot be seen outside of this compilation unit.
static void init_function() {

}

// initialization functions etc. here
int main(int argc, char *argv[]) {

  return 0;
}
// header.h
#ifndef HEADER_H_INCLUDED_
#define HEADER_H_INCLUDED_
// this is an include guard, google the term

// function declaration
int my_function();


#endif /* HEADER_H_INCLUDED_ */
// header.cpp 
//
// file containing the definitions for declarations in header.h
#include "header.h"

// definition of my_function
int my_function() {
// do cool stuff
}

// some helper function that is not to be used outside of
// header.cpp
static int helper_function() {

}
您的
header.h
文件:

// main.cpp
#include "header.h" // bring in the declarations

// some function that performs initialization.
// it is static so it cannot be seen outside of this compilation unit.
static void init_function() {

}

// initialization functions etc. here
int main(int argc, char *argv[]) {

  return 0;
}
// header.h
#ifndef HEADER_H_INCLUDED_
#define HEADER_H_INCLUDED_
// this is an include guard, google the term

// function declaration
int my_function();


#endif /* HEADER_H_INCLUDED_ */
// header.cpp 
//
// file containing the definitions for declarations in header.h
#include "header.h"

// definition of my_function
int my_function() {
// do cool stuff
}

// some helper function that is not to be used outside of
// header.cpp
static int helper_function() {

}
您的
头文件.cpp
文件:

// main.cpp
#include "header.h" // bring in the declarations

// some function that performs initialization.
// it is static so it cannot be seen outside of this compilation unit.
static void init_function() {

}

// initialization functions etc. here
int main(int argc, char *argv[]) {

  return 0;
}
// header.h
#ifndef HEADER_H_INCLUDED_
#define HEADER_H_INCLUDED_
// this is an include guard, google the term

// function declaration
int my_function();


#endif /* HEADER_H_INCLUDED_ */
// header.cpp 
//
// file containing the definitions for declarations in header.h
#include "header.h"

// definition of my_function
int my_function() {
// do cool stuff
}

// some helper function that is not to be used outside of
// header.cpp
static int helper_function() {

}
头文件仅对要向其他编译单元公开的函数和类是必需的。我假设您不想将初始化函数公开给任何其他模块,所以不在头文件中声明它们是可以的。它们还应该标记为
静态
,这样它们就没有外部链接


<>在C++中,你也可以使用匿名命名空间来提供内部链接,但这不应该与你有关。

“我需要两个不同的.CPP文件”——我认为他需要多个.CPP文件。不过我不确定。@Andrew_CS“第一个是我的主要功能,第二个是一些功能”我想我涵盖了他的情况。在你的包括警卫在内的情况下,您需要在
ifndef
之后定义宏,它才能工作。@pmr我没有看到您在一个代码块中显示了3个不同的文件。我明确建议将其拆分为单独的代码块,以便于可读性。我相信我已经解决了所有问题,但是我现在面临的唯一问题是试图在functions.cpp文件。它显然是在main中声明的,所以我无法访问它,我试图创建一个全局变量,但无法确定如何初始化它。@Cellery72为什么不将它作为参数传递到函数中?