C++ 中头文件和 cpp 文件的约定是什么?

What is the conventions for headers and cpp files in C++?(C++ 中头文件和 cpp 文件的约定是什么?)

本文介绍了C++ 中头文件和 cpp 文件的约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In C++, what is the convention for including headers for class files in the "main" file. e.g.

myclass.h 

class MyClass {
  doSomething();
}


myclass.cpp

  doSomething() {
      cout << "doing something";
  }


run.cpp

#include "myclass.h"
#include "myclass.cpp"

etc..

Is this relatively standard?

解决方案

You don't include the .cpp file, only the .h file. The function definitions in the .cpp will be compiled to .obj files, which will then be linked into the final binary. If you include the .cpp file in other .cpp files, you will get two different .obj files with the same funciton definition compiled, which will lead to linker error.

这篇关于C++ 中头文件和 cpp 文件的约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++ 中头文件和 cpp 文件的约定是什么?

基础教程推荐