c++ - 尝试使用头文件

标签 c++ file function header

我正在尝试为我吃的和喝的东西制作一个小“数据库”程序,当我输入产品名称时,我希望弹出一个列表并告诉我成分。

然而。 我不想为它编写一个大程序,因为我希望这个列表会增长,而且我不想等待编译时间来对任何产品进行任何微小的更改。我想用不同的功能实现每个产品,然后通过 header 从我的 main.cpp 文件中调用该功能。如果可以,请你帮助我。谢谢。

我的代码: main.cpp

 #include <iostream>
 #include <limits>
 #include "Arizona_Green_Tea.h"

using namespace std ;



int main(){
    char input[100] ;

    std::cout << "Search Database: " ;
    std::cin >> input ;

    if (input == "Arizona"){
        Arizona_Green_Tea();
    }



return 0 ;
}

Arizona_Green_Tea.cpp

#include "Arizona_Green_Tea.h"

int Arizona_Green_Tea()
{

}

Arizona_Green_Tea.h

#include <iostream>
using namespace std ;

int Arizona_Green_Tea(){
    std::cout << "Premium Brewed Green Tea " << std::endl ;
    std::cout << "Filtered Water" << std::endl ;
    std::cout << "High-Fructose Corn Syrup" << std::endl ;
    std::cout << "Honey" << std::endl ;
    std::cout << "Citric Acid" << std::endl ;
    std::cout << "Natural Flavors" << std::endl ;
    std::cout << "Ginseng Extract" << std::endl ;
    std::cout << "Ascorbic Acid" << std::endl ;
 }

如何连接这些文件?或者我该怎么做才能使我的程序运行?谢谢!

最佳答案

你的标题应该有函数的原型(prototype),即没有主体的函数:

// This goes into the header
int Arizona_Green_Tea();
// Note that there are no curly braces here; your header has a pair of curly braces,
// meaning that there's an empty body.

目前,您的函数有一个主体,因此您最终会得到重复的函数定义。

I still can't see the output, after I write "Arizona", it just stays blank

这是罪魁祸首:

char input[100] ;

正是由于这个声明,下面的比较没有起作用:

if (input == "Arizona") ...

包括 <string> header ,并将声明更改为 std::string ,像这样:

string input;

关于c++ - 尝试使用头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444765/

相关文章:

c++ - 在 STL 列表中找到一对,其中只有第一个元素是已知的

java - Java中不循环地迭代某个目录中的文件

macos - 什么是 "Icon?"文件?

c++ - 理解通用引用的类型推导

c - 如果全局变量的值在函数中更新,它们的值是否会发生变化?

c++ - 将控制台中的指标从字符更改为像素

c++ - 检索计算机的名称并将其保存在变量中

c++ - 欧拉计划 #3 得到一个数的最大质因数

c# - 如果文件已存在且在 C# 中具有相同位置,则始终创建新文件

c++ - 关于函数和指针的作业需要帮助