C++:连接Hpp和Cpp

4
/*Header.h file*/

#include <iostream>
using namespace std;

int main(){
    /*Menu*/
    /*case1:

        int x;
        cout << "Input ammount:";
        cin >> x
        sugarcake(x)*/

    /*case2:

       something else

    */
}

/*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;
}

我应该如何让它工作,或者我完全误解了吗?(昨天刚开始学习C++,所以请友善一些,我还没有能够在其他地方找到适合我的清晰答案)
简短版:在Header.h中调用sugarcake.cpp中的sugarcake(int x)函数。


我建议你继续阅读你用来自学C++的书籍。 - eerorika
这里有一些建议:https://dev59.com/_3RC5IYBdhLWcg3wK9yV - eerorika
是的,您误解了头文件应该如何使用 - 我强烈建议阅读一份cpp教程或书籍。 - MikeMB
2个回答

3

您不应该将代码放在头文件中,而是使用两个源(.cpp)文件,并让头文件仅包含对于您的情况下 sugarcake 函数的声明。

因此,头文件应该类似于以下内容:

// 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

sugarcake.cpp文件:

#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) {
    ...
}

最后是main.cpp文件:

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

int main() {
    int x;
    std::cin >> x;
    sugarcake(x);
}

如何构建这取决于您的环境,但如果您正在使用命令行中的GCC,则可以像这样执行: $ g++ -Wall -Wextra main.cpp sugarcake.cpp -o sugarcake 选项-Wall-Wextra 是为了启用额外的警告,这总是一个好主意。选项-o 告诉g++该如何命名它所创建的可执行程序。两个源文件是您刚创建的文件。

还是不太明白,为什么要有header.h文件?感觉它好像没做什么事情 :o 因为现在你把菜单放在了一个main.cpp文件中,它会做所有的事情吗?或者是header.h“连接”了所有的cpp文件?[main.cpp] sugarcake(5); --> [Header.h]void sugarcake(->5) --> [sugarcake.cpp] ... 5 - Widdin
目的是为了分离角色,在一个非常大的程序中,更容易找到函数并阅读代码,因为程序被分成许多头文件,每个头文件只做一件事。 - ranu
@Widdin 在这种简单情况下,你实际上不需要头文件,你可以将sugarcake声明放在main.cpp文件中。然而,在更大的项目中,为了避免大量复制粘贴声明(复制粘贴代码容易引入微妙(或不那么微妙)的错误),将常见的声明、类、符号常量(constconstexpr甚至预处理器宏)和其他需要共享的声明放在一起是非常必要的。 - Some programmer dude

1
我们使用头文件来描述在另一个场合中将要使用的函数。因此,我将展示如何在头文件中组织我的函数:
/* 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中。
// header.cpp

#include "header.h"

int sum(int &a, int &b) 
{ 
    int total = a + b; 
    return total; 
}

要在main.cpp中使用该函数,您只需包含header.cpp
// main.cpp
#include <iostream>
#include "header.h"

int main(int argc, char *argv[]) 
    {
        int a = 5;
        int b = 4;

        int total = sum(a, b);

        std::cout << "The total sum of the 4 and 5 numbers is: " << total << std::endl;
    } 

看到它是如何制作的了吗?然后您可以编译cpp文件,它就可以工作了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接