Declare an Object Via Text File

This is a discussion on Declare an Object Via Text File within the C++ Programming forums, part of the General Programming Boards category; I need a way to simply declare an object based off what a text file says. For example, a text ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    7

    Exclamation Declare an Object Via Text File

    I need a way to simply declare an object based off what a text file says. For example, a text file could have the following contents:
    Code:
    /* buildings.txt */
    house1
    house2
    cabin1
    house3
    After the program read in those as strings, I would want it to make objects of a structure using those names. Say for instance, house1 was read and loaded into a string. I would want to be able to make an object called "house1" of a structure called Buildings. The catch is that the program can't know how many objects need to be declared and what names to use before compilation. I have been trying to use a container like vector or map; however, I don't really know how to apply that to my situation.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your objects are going to have to store their own names, in a name field or similar. You just aren't going to be able to use those names as variables.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    7
    So the structure needs to be something like this:

    Code:
    struct BUILDING
    {
      int size;
      string object_name;
      //rest of stuff
    };
    Therefore meaning the object cannot have the name read in the file? Maybe I was not clear on this, but there would be another file for each building. So a file for house1 with all of its properties which would be loaded into the house1.size and other elements.

    I am still unsure on how to declare a new object of structure BUILDING regardless of if I can have the name I want.
    Last edited by SnakeJam; 05-27-2010 at 06:33 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by SnakeJam View Post
    So the structure needs to be something like this:

    Code:
    struct BUILDING
    {
      int size;
      string object_name;
      //rest of stuff
    };
    Therefore meaning the object cannot have the name read in the file?
    You have to type the variable name into the file, now. You can't do it later.

    Quote Originally Posted by SnakeJam View Post
    I am still unsure on how to declare a new object of structure BUILDING regardless of if I can have the name I want.
    Just like any other object.
    Code:
    type_of_object name_of_object; //like int x or string b

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    7
    So lets say I declared 4 objects of BUILDING in the actual program code. That would work for the text file I have, but what if the text file had 5 names in it. Like this:

    Code:
    /* buildings.txt */
    house1
    house2
    cabin1
    house3
    cabin2
    How am I going to make another object for cabin2 without the program knowing that there are going to be 5?

    I need some function that I can call on to create new objects of structure BUILDING whenever I read in another string from the file.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by SnakeJam View Post
    So lets say I declared 4 objects of BUILDING in the actual program code. That would work for the text file I have, but what if the text file had 5 names in it. Like this:

    Code:
    /* buildings.txt */
    house1
    house2
    cabin1
    house3
    cabin2
    How am I going to make another object for cabin2 without the program knowing that there are going to be 5?

    I need some function that I can call on to create new objects of structure BUILDING whenever I read in another string from the file.
    You keep saying you want to use vector or map. Do so. (The usual idiom is to declare one object, read data into it, push it into the vector (which makes a copy for the vector), lather, rinse, and repeat.)

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    7
    I guess I might not be very clear about what I want to do because I don't really know how to explain it. basically I want to be able to make objects without knowing they are going to be created before the program is compiled. I don't know if its called dynamic allocation, but that would sound correct. Below is an example of what I want it to look like/do.


    Code:
    // 1. Read the first line
    // 2. Send that to a string
    // 3. Declare an object of structure BUILDING with the strings name.  Sort of like this (however, not creating an object called "string_content".  I want the object to have the name that the string contained.
    
    BUILDING string_content;  //assume that string_content contained "house1"
    
    //4. Then access that objects members like so:
    
    house1.member_1

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by SnakeJam View Post
    I guess I might not be very clear about what I want to do because I don't really know how to explain it. basically I want to be able to make objects without knowing they are going to be created before the program is compiled. I don't know if its called dynamic allocation, but that would sound correct. Below is an example of what I want it to look like/do.


    Code:
    // 1. Read the first line
    // 2. Send that to a string
    // 3. Declare an object of structure BUILDING with the strings name.  Sort of like this (however, not creating an object called "string_content".  I want the object to have the name that the string contained.
    
    BUILDING string_content;  //assume that string_content contained "house1"
    
    //4. Then access that objects members like so:
    
    house1.member_1
    If you really want that, you'll probably have to write the programming language that will allow it. A map from string to building is a pretty obvious second choice, if you intend to use C++.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    7
    Could you give me a simple example using map because I don't really know much about it. Not an entire program, just how to declare an object the way I want using map (if it is in fact that obvious). And yes, I intend to use c++.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by SnakeJam View Post
    Could you give me a simple example using map because I don't really know much about it. Not an entire program, just how to declare an object the way I want using map (if it is in fact that obvious). And yes, I intend to use c++.
    Code:
    #include <map>
    #include <string>
    .. define your building ..
    
    std::map<std::string, building> big_pile_of_buildings;
    std::string building_name;
    file >> building_name;
    building temp_building;
    .. fill in building from file ..
    map[building_name] = temp_building;

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    7
    In the 8th line did you mean string temp_building or building temp_building;

    Never mind, you didn't.


    Anyways, that looks like what I want to d. I take it that map[building_name] is the actual declaration of the BUILDING object.
    Last edited by SnakeJam; 05-27-2010 at 07:33 PM.

  12. #12
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Look up "class factories"; I think this is about as close as you are going to get. The basic pseudocode for a class factory looks like this:
    Code:
    void *classFactory(string strClass)
    {
          void *ptrReturn = NULL;
          if( strClass == "house")
          {
                ptrReturn = static_cast<CHouse *>(new CHouse);
          }
          else if(strClass == "apartment")
          {
                ptrReturn = static_cast<CApartment>(new Apartment);
          }
          else
          {
               // make other classes
           }
           return ptrReturn;
    }
    So you could read a line from a file, call classFactory(line) and if it does not return NULL, it has a pointer of your desired type. It helps with functionality if all of the classes made by the class factory are from a common base class, then you do away with the void* and use a base class pointer instead.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by SnakeJam View Post
    In the 8th line did you mean string temp_building or building temp_building;

    Never mind, you didn't.


    Anyways, that looks like what I want to d. I take it that map[building_name] is the actual declaration of the BUILDING object.
    Of course not. "building temp_building" is the declaration of the object. The assignment just puts the building in the map (again, via a copy).

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    7
    This is making a lot more sense, and I get how to I should use map now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with searching text file
    By zacharyrs in forum C Programming
    Replies: 30
    Last Post: 12-01-2009, 03:13 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21