Thread: Array of Strings in a Class

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Array of Strings in a Class

    I've been scouring the internet all morning looking for a solution to this problem, but nothing ever fits what I need. I'm creating a class which needs access to a list of names. I'm having a lot of trouble creating and then accessing this list/array/vector of names. The list is approximately 3005 names long (I copy/paste it in), so I can't use vector.push_back(). I tried using constructors, but couldn't get that to work. Here's a shortened (and simplified) sample of my code:

    Code:
    class name:
    {
        std::string first_name[];
    public:
        name();
        static std::string get_first()
        {
            return first_name[0];
        }
    };
    
    name::name(void)
    {
        first_name[] = {"Aaliyah", "Aaron", "Abagail", "Abbey", "Abbie"...}
    }
    With code identical to that, I get errors:
    invalid use of member 'name::first_name' in static member function
    expected primary-expression before ']/{' token

    Can anyone help me? Is there an easier way to do this?

    EDIT: Forgot to mention, I'm new to C++. I've been programming in C for a long time, but this project necessitates C++ for class inheritance.
    Last edited by ThesaurusRex; 06-28-2010 at 09:00 AM. Reason: FYI

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Array initialization doesn't work that way in C++ any more than it does in C. Consider a std::vector instead.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Quote Originally Posted by rags_to_riches View Post
    Array initialization doesn't work that way in C++ any more than it does in C. Consider a std::vector instead.
    Let's say I consider a vector instead (which I have). How do I add 3000+ names to this vector easily? I could do it during initialization in a constructor, but then I don't think I'd be in the right scope to access that vector anymore once I needed it in the other methods.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    What does work is putting the giant arrays outside of the class definition, essentially making them global. I'm fairly certain that's considered horrendous programming style, so I'd like to fix it in some way. Any help? How can I get my class to define an array, assign stuff to it, and then access it from several methods?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is possible to create an array, but it just isn't possible to initialize it (unless it's static).
    What you need to do is create an array with a fixed size (or std::vector) and then assign your data to the array instead of initializing it.
    (This should be fixed in C++0x, I believe. But we're not there yet.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Thanks for the responses. I realized that the problem could be fixed by simply declaring the data arrays inside the methods from which they're used. The test program is not noticeably slower, and I guess that means it won't have to use that array if I don't use that method. All is good.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not put all the names in a file and have your program read each name into a vector of strings?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM