我正在尝试为我吃的和喝的东西制作一个小“数据库”程序,当我输入产品名称时,我希望弹出一个列表并告诉我成分。
然而。 我不想为它编写一个大程序,因为我希望这个列表会增长,而且我不想等待编译时间来对任何产品进行任何微小的更改。我想用不同的功能实现每个产品,然后通过 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/