C++ c++;,连接Hpp和Cpp /*Header.h文件*/ #包括 使用名称空间std; int main(){ /*菜单*/ /*案例1: int x; cout>x 糖饼(x)*/ /*案例2: 别的 */ } /*sugarcake.cpp文件*/ #包括 #包括“Header.h” 使用名称空间std; 空糖蛋糕(int x){ cout

C++ c++;,连接Hpp和Cpp /*Header.h文件*/ #包括 使用名称空间std; int main(){ /*菜单*/ /*案例1: int x; cout>x 糖饼(x)*/ /*案例2: 别的 */ } /*sugarcake.cpp文件*/ #包括 #包括“Header.h” 使用名称空间std; 空糖蛋糕(int x){ cout,c++,C++,您不应该将代码放在头文件中,而应该使用两个源(.cpp)文件,并且头文件只包含sugarcake函数的声明 所以头文件应该看起来像 /*sugarcake.cpp file*/ #include <iostream> #include "Header.h" using namespace std; void sugarcake(int x) { cout << (x * 0.75) << "st egg" << endl; co

您不应该将代码放在头文件中,而应该使用两个源(
.cpp
)文件,并且头文件只包含
sugarcake
函数的声明

所以头文件应该看起来像

/*sugarcake.cpp file*/

#include <iostream>
#include "Header.h"
using namespace std;

void sugarcake(int x) {
    cout << (x * 0.75) << "st egg" << endl;
    cout << (x * 0.75) << "dl sugar" << endl;
    cout << (x * 0.5) << "tsk vanillasugar" << endl;
    cout << (x * 0.5) << "tsk blabla" << endl;
    cout << (x * 0.75) << "dl wheatflour" << endl;
    cout << (x * 18.75) << "gram butter" << endl;
    cout << (x * 0.25) << "dl water" << endl;
}
sugarcake.cpp
文件:

// These first two line (and the last line of the file) is part of
// a header include guard (see https://en.wikipedia.org/wiki/Include_guard)
#ifndef HEADER_H
#define HEADER_H

void sugarcake(int x);

#endif // HEADER_H
#include <iostream>
#include "header.h"  // Technically we don't need to include the header
                     // file here, but it's always a good idea to do
                     // anyway

void sugarcake(int x) {
    ...
}
如何构建它取决于您的环境,但是如果您从命令行使用GCC,那么您会喜欢这样做

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

int main() {
    int x;
    std::cin >> x;
    sugarcake(x);
}
$g++-Wall-Wextra main.cpp sugarcake.cpp-o sugarcake
-Wall
-Wextra
选项用于启用额外警告,这始终是一个好主意。
-o
选项告诉
g++
它创建的可执行程序的名称。这两个源文件就是您刚刚创建的文件。

我们使用头文件来描述将在另一个occa中使用的函数每一个说

因此,我将展示如何在头文件中组织我的函数:

$ g++ -Wall -Wextra main.cpp sugarcake.cpp -o sugarcake 函数的定义写入
标题.cpp

/* header.h
 * include guards below, it is very import to use it to be sure you'r not
 * including things twice.
 */
#ifndef HEADER_H
#define HEADER_H

// I write function prototypes in the header files. 
int sum(int &a, int &b);

#endif // HEADER_H
// header.cpp

#include "header.h"

int sum(int &a, int &b) 
{ 
    int total = a + b; 
    return total; 
}
要在
main.cpp
中使用该函数,只需包含
header.cpp

/* header.h
 * include guards below, it is very import to use it to be sure you'r not
 * including things twice.
 */
#ifndef HEADER_H
#define HEADER_H

// I write function prototypes in the header files. 
int sum(int &a, int &b);

#endif // HEADER_H
// header.cpp

#include "header.h"

int sum(int &a, int &b) 
{ 
    int total = a + b; 
    return total; 
}
//main.cpp
#包括
#包括“header.h”
int main(int argc,char*argv[])
{
INTA=5;
int b=4;
整数总计=总和(a,b);

STD:我建议你继续阅读你正在使用的书来教你自己C++。这里有一些建议:是的,你误解了头文件应该如何使用。我强烈推荐阅读CPP教程或书。仍然没有得到它,有什么原因有头。h文件?不觉得它在做某事:O。Bcuz现在您在main.cpp文件中有了菜单,它可以执行所有操作?或者header.h是否“连接”所有cpp文件?[main.cpp]sugarcake(5);-->[header.h]void sugarcake(->5)-->[sugarcake.cpp]…5其目的是分离角色,在一个非常大的程序中,查找函数和读取代码会更容易,因为程序被分离在许多头文件中,每个头文件只做一件事。@Widdin在这样一个简单的情况下,您实际上不需要头文件,您可以在t中使用
sugarcake
声明他创建了
main.cpp
文件。但是,对于较大的项目,确实需要放置公共声明、类、符号常量(
const
constepr
甚至预处理器宏)和其他需要共享的声明,以避免复制粘贴大量声明(复制粘贴编码容易引入细微(或不太细微)的错误)。