C++ 实现不使用全局声明的函数
C++ Implementing Functions that don't utilize global declarations
我的代码已经在工作,在此处查看:http://pastebin.com/mekkrqkg
现在,我的功能起作用,但我想利用我声明globally
的信息,我想转换它们,以使它们处于lines 11-15
上的格式,但是我不确定如何转换它们这样做。简而言之,我正在尝试转换我的功能
"void add_county_election_file"
以
的格式 "void add_county_election_file(const string, const vector &, const vector &, const vector &, const vector &)"
我不知道从哪里或如何从哪里开始。
有人可以帮助我,向我展示如何为第一个功能做到这一点,以便我可以全面实施它?
谢谢大家!
您的函数声明应该看起来像这样:
void add_county_election_file(const string, vector<int>&, vector<string>..);
确保矢量模板的参数列表正确(这是您在&lt;>之间放置的类型)
然后将您的函数的实现与声明:
匹配: void add_county_election_file(const string, vector<int>&, vector<string>..){...}
现在,用主要的arguemtns调用您的功能:
string s;
vector<int> arg;
vector<string> sv;
void someFunction (s, arg, sv ...);
我认为您在声明的功能时做正确 void add_county_election_file(const string, vector<int>&, vector<int>&,..);
因此,您只需要使用所需的参数调用上述函数,因为现在您没有传递参数,而当前的定义不接受任何参数。
作为一个很好的做法,在您的int main()
功能中,您可以使用switch
而不是使用if else
。
- 将您的变量和功能存储在类,超载运算符并创建功能以访问这些变量。
-
声明
int main()
中的所有变量,并将要传递到每个函数的参数设置,例如void print_results()
被修改为void print_results(std::vector<int> vec, int nCount, etc..)
-
与第一个类似,创建一个结构来保存所有数据成员,然后将struct(通过参考)传递到每个函数中。
struct CountryTracker { std::vector<int> ID; std::string name; //etc... }
`void print_results(CountryTracker& Obj) //pass single struct into functions`
oop做到这一点的方法是创建一个名为 ElectionInfo
的类,其中:
这些将是其成员字段:
vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
这些将是其成员函数:
void add_county_election_file(const string);
void search_county(const string);
void print_results();
这样,您就不必将引用将引用传递给周围的向量,而是可以做的:
ElectionInfo an_elect_info;
char selection = get_menu_choice();
// some if-statements to decide which of the following to call:
an_elect_info.add_county_election_file(county_name);
an_elect_info.search_county(county_name);
an_elect_info.print_results();
但是,如果您希望使用当前功能方法:
在主要方法内声明并初始化以下内容:
vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
应调整所注释的函数声明的语法以使其看起来像:
void add_county_election_file(const string, vector<string>&, vector<int>&, vector<int&, vector<int>&);
(当然,定义应效仿)
您会这样调用:
add_county_election_file(countyname, countyNameVector, countyNCount, countyFCount, countyOCount);
对象是自动通过参考传递的。
重构的基本过程应在第一步仅涉及代码分组和放置,并且只应最小涉及编写新逻辑。以此为原则,您可以首先以以下方式修改代码。
string ReadInputString(const char* title)
{
string s
cout << title;
cin >> s;
}
void add_county_election_file(const std::string& filename
, std::vector<string>& countyNameVector
, std::vector<int>& countyNCount
, std::vector<int>& countyFCount
, std::vector<int>& countyOCount
)
{
int NCount = 0;
int FCount = 0;
int OCount = 0;
int NTotal = 0;
int FTotal = 0;
int OTotal = 0;
char vote;
std::ifstream input((filename).c_str());
string countyName;
if(input.is_open())
{
input >> countyName;
countyNameVector.push_back(countyName);
while(input >> vote)
{
if(vote == 'N' || vote == 'n')
{
NCount = NCount + 1;
}
else if(vote == 'F' || vote == 'f')
{
FCount = FCount + 1;
}
else
{
OCount = OCount + 1;
}
}
countyNCount.push_back(NCount);
countyFCount.push_back(FCount);
countyOCount.push_back(OCount);
}
cout << countyName << endl;
}
void add_county_election_file()
{
string fn = ReadInputString("Enter the county file to process: ");
add_county_election_file(fn,g_countyNameVector,g_countyNCount,g_countyFCount,g_countyOCount);
}
您可以看到,我刚刚提取了您的代码,并将其移至单个功能并更改了名称以发挥意义。就像在函数中读取intupstring一样 - " cin>> s"线最初是" cin>> fileName"。抽象名称" s"表示readInputString不知道或不在乎它正在从控制台读取的字符串的语义含义。
为了不更改您的主函数 - 我添加了一个重载的add_county_election_file,该函数调用一个函数,然后是另一个功能。这个想法是,您应该保持不变的东西,并改变其他东西(永远),然后在需要时进行替代。我已经更改了您的全局变量的名称,以使用" g_"将它们与本地变量区分开 - 关键是" g_"只能在您的代码中的几个位置找到。
- C++:为什么允许在另一个函数中声明函数,而不允许在函数定义中声明?
- 在c++中在类外声明函数有什么好处
- 使用 #define 声明函数
- 在静态库中声明函数,在使用该相同库的应用程序中定义它
- 如何强制编译器在 C/C++ 本身中声明函数?
- 如果您只需要在 .h 文件中声明函数.cpp是否需要在 .h 文件中声明函数?
- C++:<sys/sysctl.h> 无法声明函数CTL_HW和HW_NCPU
- 尝试声明函数的局部变量,但得到范围错误
- 如何在C++模板中声明函数
- 如何在另一个文件的类中声明函数
- 使用非类型模板参数正向声明函数模板
- 在此范围错误中未声明函数错误
- 当我们不能声明函数内联(GCC 编译器)时?
- 未在此作用域中声明函数,即使存在头文件也是如此
- 无法声明函数中的模板类型别名
- 单个CPP文件中多次声明函数声明可以吗?
- 编译一个支持VBA中声明函数的dll
- 使用从外部参数包中获取的参数类型声明函数
- 使用类成员正确地声明函数
- 在函数内重新声明函数