Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++;? 在C++中,在“主”文件中包含类文件头的约定是什么?e、 g myclass.h class MyClass { doSomething(); } myclass.cpp doSomething() { cout << "doing something"; } run.cpp #include "myclass.h" #include "myclass.cpp" etc.. myclass.h 类MyClass{ doSomething(); } myclass.cpp doSomething(){ 这个问题有很多好的答案,请参见。_C++_Header_Include - Fatal编程技术网

在C++;? 在C++中,在“主”文件中包含类文件头的约定是什么?e、 g myclass.h class MyClass { doSomething(); } myclass.cpp doSomething() { cout << "doing something"; } run.cpp #include "myclass.h" #include "myclass.cpp" etc.. myclass.h 类MyClass{ doSomething(); } myclass.cpp doSomething(){ 这个问题有很多好的答案,请参见。

在C++;? 在C++中,在“主”文件中包含类文件头的约定是什么?e、 g myclass.h class MyClass { doSomething(); } myclass.cpp doSomething() { cout << "doing something"; } run.cpp #include "myclass.h" #include "myclass.cpp" etc.. myclass.h 类MyClass{ doSomething(); } myclass.cpp doSomething(){ 这个问题有很多好的答案,请参见。,c++,header,include,C++,Header,Include,通常单独编译.cpp文件,并将生成的.o与其他.o链接起来 因此,myclass.cpp将包括myclass.h,并将作为一个单元进行编译。您不包括.cpp文件,只包括.h文件。.cpp中的函数定义将编译为.obj文件,然后将其链接到最终的二进制文件中。如果您将.cpp文件包括在其他.cpp文件中,则sa将获得两个不同的.obj文件已编译函数定义,这将导致链接器错误。您单独编译cpp文件。如果将任何给定的cpp文件包含到两个或多个cpp文件中,您可能会在链接阶段遇到冲突。您没有将一个*.cpp包

通常单独编译.cpp文件,并将生成的.o与其他.o链接起来


因此,myclass.cpp将包括myclass.h,并将作为一个单元进行编译。

您不包括.cpp文件,只包括.h文件。.cpp中的函数定义将编译为.obj文件,然后将其链接到最终的二进制文件中。如果您将.cpp文件包括在其他.cpp文件中,则sa将获得两个不同的.obj文件已编译函数定义,这将导致链接器错误。

您单独编译cpp文件。如果将任何给定的cpp文件包含到两个或多个cpp文件中,您可能会在链接阶段遇到冲突。

您没有将一个*.cpp包含在另一个*.cpp中。相反:


myclass.h

class MyClass {
  doSomething();
}

myclass.cpp

#include "myclass.h"

MyClass::doSomething() {
      cout << "doing something";
}
#include "myclass.h"

etc..


而不是将myclass.cpp包含在main.cpp中(这样编译器就可以在一个过程中同时看到它们),您可以分别编译myclass.cpp和main.cpp,然后让“链接器”将它们组合成一个可执行文件。

您可以说一个.cpp文件及其包含的所有头文件组成一个翻译单元。顾名思义,一个翻译单元是独立编译的。每个翻译单元的结果通常称为file.o或file.obj,然后是li由链接器链接在一起,修复尚未解决的引用。因此,在您的情况下,您必须

Translation Unit 1 = run.cpp: myclass.h ...
Translation Unit 2 = myclass.cpp: myclass.h ...
您的类定义将出现在两个翻译单元中。但这没关系。只要两个类的定义相同,就允许这样做。但是,如果函数不是内联的,则不允许在两个翻译单元中出现相同的函数。不内联函数只允许在一个翻译单元中定义一次。然后,让链接器获取每个翻译单元的结果,并将它们绑定到一个可执行文件:

Executable = mystuff: run.o myclass.o ...

我可能错了,但我认为类是用两个单位声明的,并且只在一个单位中定义的:.h文件包含类的*声明,.cpp文件包含*定义。不,它包含定义。cpp文件包含的是成员函数/静态成员的定义。但是类定义放在heade中r(类声明只是“class foo;”,而定义可以是“class foo{};”)我同意博士!上面的这一点: