我有很长的字符串列表,我想在它自己的.h文件中放置、定义和声明这些字符串。我希望将这些字符串分组为向量,并在不同的.h文件中使用这些值。第二个文件将std::find用于查看向量中是否存在字符串。向量是对字符串进行分组的好方法,还是应该使用另一种方法?
我有一个kitBreakdown.h .h文件将包含多个字符串向量,如下所示:
#ifndef KIT_BREAKDOWN_H
#define KIT_BREAKDOWN_H
#include <vector>
void setup(){
std::vector<std::string> Bricks_Plates;
Bricks_Plates.push_back("2_1_plate"); //4211398
Bricks_Plates.push_back("2_1_brick"); //4211388
Bricks_Plates.push_back("2_2_brick"); //4211387
Bricks_Plates.push_back("4_1_plate"); //4211445
Bricks_Plates.push_back("4_2_plate"); //4211444
Bricks_Plates.push_back("6_2_plate"); //4211542
Bricks_Plates.push_back("8_2_plate"); //4211449
Bricks_Plates.push_back("2_1_smooth_plate"); //4211052
}
#endif我想在另一个名为searchControl.h的文件中使用这些字符串,其中包含一个searchControl类来实现机器人搜索。
#include "kitBreakdown.h"
#include <algorithm>
// The purpose of this class is to implement a search control structure
// So individual variables can be set up (limbs and cameras) before hand
// Search Geometry should be set and checked to ensure valid choices are made
class SearchControl
{ ...
private:
void _init_search();
...
std::vector<std::string> Bricks_Plates;
};
void SearchControl::_init_search()
{...
std::cout<<"What is your Desired Piece Type?\n";
int i = 0;
while (i==0)
{
std::cin >> _desired_piece;
if (std::find(Bricks_Plates.begin(),Bricks_Plates.end(), _desired_piece) !=Bricks_Plates.end())
{
std::cout << "Cool. " << _desired_piece << " will go in one bin and anything else will go in another\n";
i=1;
}
else {
std::cout << "I don't recognize what you want\n";
std::cout << "Your Choices are...\n";
for (int j=0; j<Bricks_Plates.size(); j++) {
std::cout<< Bricks_Plates[j]<< "\n";
}
std::cout << "Enter a new Desired Piece Type:\n";
}
}
}我希望这要求_desired_piece,检查_desired_piece是否在Brick_Plates向量中,并相应地执行if语句。但是,当我运行这段代码时,它没有输出Brick_Plates向量的任何元素。如何将第一个头文件中字符串的值传递给第二个头文件?
发布于 2014-07-24 18:38:11
修改steup函数以返回所构建的向量:
#ifndef KIT_BREAKDOWN_H
#define KIT_BREAKDOWN_H
#include <vector>
std::vector<std::string> setup(){
std::vector<std::string> Bricks_Plates;
Bricks_Plates.push_back("2_1_plate"); //4211398
Bricks_Plates.push_back("2_1_brick"); //4211388
Bricks_Plates.push_back("2_2_brick"); //4211387
Bricks_Plates.push_back("4_1_plate"); //4211445
Bricks_Plates.push_back("4_2_plate"); //4211444
Bricks_Plates.push_back("6_2_plate"); //4211542
Bricks_Plates.push_back("8_2_plate"); //4211449
Bricks_Plates.push_back("2_1_smooth_plate"); //4211052
return Bricks_Plates;
}
#endif并向SearchControl添加一个构造函数,该构造函数将其成员Bricks_Plates初始化为从安装程序返回的值:
#include "kitBreakdown.h"
#include <algorithm>
class SearchControl
{ ...
public:
SearchControl():Bricks_Plates(setup()){}
private:
void _init_search();
...
std::vector<std::string> Bricks_Plates;
};
void SearchControl::_init_search()
{...
std::cout<<"What is your Desired Piece Type?\n";
int i = 0;
while (i==0)
{
std::cin >> _desired_piece;
if (std::find(Bricks_Plates.begin(),Bricks_Plates.end(), _desired_piece) !=Bricks_Plates.end())
{
std::cout << "Cool. " << _desired_piece << " will go in one bin and anything else will go in another\n";
i=1;
}
else {
std::cout << "I don't recognize what you want\n";
std::cout << "Your Choices are...\n";
for (int j=0; j<Bricks_Plates.size(); j++) {
std::cout<< Bricks_Plates[j]<< "\n";
}
std::cout << "Enter a new Desired Piece Type:\n";
}
}
}尽管R的注释在技术上是正确的,有时使用外部变量或全局变量是唯一的方法,但人们普遍认为使用全局语言是不好的风格。
https://stackoverflow.com/questions/24940419
复制相似问题