-1

I have a problem, I got a file with different cities and road, I did a class called Ville (city in french) and a class called Route (Road in french) and I need to put all my Ville into a vector of Ville and all my route into a vectof of route

Ville constructor is called like that:

Ville *myCity = new Ville("myCity");

and the road constructor:

Route *myRoad = new Road("myRoad",400, 130, myCity1, myCity2);

where the argument are the name of the road, the size, the maximum speed, and the two cities which are link by the road.

The file look like:

< ville>

  Paris
  Berlin
  ...
< /ville>
< route>
  A0 400 130 Paris Berlin
  ...
</route>

I did that for the data extraction:

  std::string element;
  int statut = 0;
  while(fichier >> element)
  {
      if(element == "</ville>" || element == "</route>")
      {
          statut = 0;
          fichier >> element;
      }
      if(element == "<ville>" || statut == 1)
      {
          if(statut == 0)
          {
              fichier >> element;
          }
          cout << "Ville: " <<  element << endl;
          statut = 1;
      }
      if(element == "<route>" || statut == 2)
      {
          if(statut == 0)
          {
              fichier >> element;
          }
          cout << "Route: " <<  element;
          fichier >> element;
          cout << " Taille: " << element;
          statut = 1;
          fichier >> element;
          cout << " Speed: " << element;
          fichier >> element;
          cout << " Relie: " << element;
          fichier >> element;
          cout << " à " << element <<endl;
          statut = 2;
      }
  }
  }
  else
  {
       cout << "Error: Can't open the file" << endl;
  }

I'm looking for a method that allow me to do something like:

Ville *element = new Ville (element);

Or should I change the way that I extract data to be easily transform into object?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
mel
  • 2,730
  • 8
  • 35
  • 70
  • I don't understand the question. If you can do `new Ville("myCity")`, and `element` is a string, do you just want `new Ville(element.c_str())`? – Barry Dec 30 '14 at 21:58
  • are you looking for xml parser? http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c or http://lars.ruoff.free.fr/xmlcpp/ btw. correct your last line - you use element for two different things – Palo Dec 30 '14 at 21:58
  • I did it by purpose, I'm looking to do that because the variable city will have the same name that the city itself. – mel Dec 30 '14 at 22:02
  • To clarify, do you want to make dynamically named objects? i.e. if the city in the file is called paris, you want a Ville *paris = new Ville("paris")? – Rubix Rechvin Dec 30 '14 at 22:05
  • Yes absolutely, this is exactly what I want – mel Dec 30 '14 at 22:07

2 Answers2

0

It seems that you need a std::map:

std::map<string, Ville*> cities;
std::vector<Route> roads;

where you retrieve a city data:

cities[element] = new Ville(element);

and where you retrieve road data

roads.push_back(Route(name, size, speed,
                      cities.at(cityName1),
                      cities.at(cityName2)));
Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

Ok, so you can't do what you're trying to do directly in C++. There is a bit of a workaround though:

You can create a map<string, Ville*>, let's call it myCities:

You can then use this map by reading out an 'element' from the file, and then using the code:

myCities[element] = new Ville(element);

From this point on you can access your items by myCities["whichever city you want"]. Other than this, I can't think of an easy way to do this as C++ doesn't support dynamically named variables.

Rubix Rechvin
  • 571
  • 3
  • 16