0

I have long list of strings that I want to put define and declare in its own .h file. I want to group these strings into vectors, and use these values in a different .h file. The second file will std::find to see if a string is in the vector. Are vectors a good way group the strings to do this or should I use another method?

I have a kitBreakdown.h file will have multiple vectors of strings like the following:

    #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

I want to use these strings in a different file called searchControl.h, which contains a searchControl class to implement a robotic search.

    #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";
        }
      }
    }

I want this to ask for the _desired_piece, check if _desired_piece is in Brick_Plates vector, and carry out the if statement accordingly. However when I run this code it outputs no elements of the Brick_Plates vector. How can I pass the values of the strings in the first header file to the second one?

tmwcheese
  • 3
  • 1
  • 2
  • 1
    Take a look at http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how/1433387#1433387 – R Sahu Jul 24 '14 at 17:41
  • Thanks! I'm still not sure I completely understand. I made a kitBreakdown.cpp file which defines the vector and includes kitBreakdown.h. kitBreakdown.h is only has the extern std::vector Bricks_Plates; line. I'm still getting an empty vector when I run. – tmwcheese Jul 24 '14 at 18:33
  • Try to reduce the code to as few lines as possible that still produces the problem. Then post the code. – R Sahu Jul 24 '14 at 18:35

1 Answers1

1

Modify your steup function to return the vector which you built up:

#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

and add a Constructor to SearchControl which initializes its member Bricks_Plates to the value which you return from setup:

#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";
    }
  }
}

Although R Sahus comment is technically correct and using extern or global variables is sometimes the only way to do things it is widely considered bad style to use globals.

odinthenerd
  • 5,422
  • 1
  • 32
  • 61
  • Great thanks! And if I have more vectors in addition to Bricks_Plates I could make a function for each and return each vector that way, right? – tmwcheese Jul 28 '14 at 16:58