C++中的.h和.cpp如何关联
以下是.h和.cpp以及主程序三段,如何关联;//NameFunction.h#include<string>#include<vector>usingnamespace...
以下是.h和.cpp以及主程序三段,如何关联;
//NameFunction.h
#include<string>
#include<vector>
using namespace std;
//======================================
void FillVectors(vector<string> &name,vector<string> $origins);
string AskUserForName();
bool SearchForName(vector<string> &names,vector<string> &origins,string &userName,string &userOrigin);
//NameFunctions.cpp
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//======================================================================================================
bool SearchForName(vector<string> &names,vector<string> &origins,string &userName,string &userOrigin)
{
for(int index=0;index<names.size();index++)
{
if(userName==names.at(index)){userOrigin=origins.at(index);return true;}
}
return false;
}
//=======================================================================================================
void FillVectors(vector<string> &name,vector<string> &origins)
{
name.push_back("Barbara");
origins.push_back("greek,meaning stranger");
name.push_back("Kelly");
origins.push_back("celtic,meaning warrior or defender");
}
//=======================================================================================================
string AskUserForName()
{
string userName;
cout<<"Please enter a women's name,such as Kelly;"<<endl;
cin>>userName;
return userName;
}
//=======================================================================================================
//============================================================================
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#include "NameFunctions.h"
//=========================================================
int main()
{
cout<<"Welcome to C++ Name Search Program"<<endl;
vector<string> names,origins;
FillVectors(names,origins);//用函数对定义的两个vector对象赋值;
string userName,userOrigin,answer="yes";
bool Found;//判断是否找到;
for(;answer=="yes"||answer=="Yes";)
{
userName=AskUserForName();
Found=SearchForName(names,origins,userName,userOrigin);
if(Found)
{
cout<<"Here is your name: "<<userName<<endl;
cout<<"Origin: "<<userOrigin<<endl;
}
else{cout<<" Sorry "<<userName<<" isn't in our vector."<<endl;}
cout<<"Do you have another name? yes/no ";
cin>>answer;
}
return 0;
} 展开
//NameFunction.h
#include<string>
#include<vector>
using namespace std;
//======================================
void FillVectors(vector<string> &name,vector<string> $origins);
string AskUserForName();
bool SearchForName(vector<string> &names,vector<string> &origins,string &userName,string &userOrigin);
//NameFunctions.cpp
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//======================================================================================================
bool SearchForName(vector<string> &names,vector<string> &origins,string &userName,string &userOrigin)
{
for(int index=0;index<names.size();index++)
{
if(userName==names.at(index)){userOrigin=origins.at(index);return true;}
}
return false;
}
//=======================================================================================================
void FillVectors(vector<string> &name,vector<string> &origins)
{
name.push_back("Barbara");
origins.push_back("greek,meaning stranger");
name.push_back("Kelly");
origins.push_back("celtic,meaning warrior or defender");
}
//=======================================================================================================
string AskUserForName()
{
string userName;
cout<<"Please enter a women's name,such as Kelly;"<<endl;
cin>>userName;
return userName;
}
//=======================================================================================================
//============================================================================
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#include "NameFunctions.h"
//=========================================================
int main()
{
cout<<"Welcome to C++ Name Search Program"<<endl;
vector<string> names,origins;
FillVectors(names,origins);//用函数对定义的两个vector对象赋值;
string userName,userOrigin,answer="yes";
bool Found;//判断是否找到;
for(;answer=="yes"||answer=="Yes";)
{
userName=AskUserForName();
Found=SearchForName(names,origins,userName,userOrigin);
if(Found)
{
cout<<"Here is your name: "<<userName<<endl;
cout<<"Origin: "<<userOrigin<<endl;
}
else{cout<<" Sorry "<<userName<<" isn't in our vector."<<endl;}
cout<<"Do you have another name? yes/no ";
cin>>answer;
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询