Static array of initialized members?

This is a discussion on Static array of initialized members? within the C++ Programming forums, part of the General Programming Boards category; Probably not the best description; I'm building a simple class structure for my tokenizer and I want all initialized members ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Static array of initialized members?

    Probably not the best description; I'm building a simple class structure for my tokenizer and I want all initialized members of the class to be put into an array.

    Unfortunantly; my compiler is yelling at me when I dump them into an array; could anyone point out how to actually accomplish this? Nothing I do seems to work t-t.

    Code:
    class Token {
    	public:
    	std::string token;
    	int opcode;
    	bool type;
    	static Token Tokens[32];
    	static int NTokens;
    
    	Token(std::string a, int b, bool c) {token = a; opcode = b; type = c; Tokens[NTokens++] = this;}
    	Token(char a, int b, bool c) {token[0] = a; token[1] = '\0'; opcode = b; type = c; Tokens[NTokens++] = this;}
    };
    I'm sure it's something simple; but I just don't see it.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are declaring a static array of 32 Tokens. That is going to create 32 token objects the same way that static int integers[32] would create 32 integers. Now which constructor is going to get called when those 32 Token objects get created?

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Quote Originally Posted by bithub View Post
    You are declaring a static array of 32 Tokens. That is going to create 32 token objects the same way that static int integers[32] would create 32 integers. Now which constructor is going to get called when those 32 Token objects get created?
    I always thought C++ compiled types before declarations; but I'm not really well versed in how it's compiled. If that's the case - do you have any recomendations on how to do this? I'd like to avoid dynamic memory allocation in this particular instance.

    [Edit]

    Ughh I'm an idiot; I forgot the compiler doesn't supply a default blank constructor if you supply your own. Thanks for the help!
    Last edited by Blackroot; 04-11-2009 at 09:12 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Blahhh; hate to bump this but my workaround doesn't work t-t. I can't figure out a single way to initialize a list of members without actually making a linked list; I can't even figure a way to use vectors for this ~ it's trying to initialize any data member before the constructor in announced t-t.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> token[0] = a; token[1] = '\0';

    That's a really bad idea. You don't add characters to an std::string that way. Use string:ush_back(char) or string:perator += (const char*). Furthermore, you should *never* terminate, resize, append, or otherwise alter the dimensions of an std::string manually (so no strcpy, strcat, etc). Always use the appropriate member functions for those sorts of operations.

    Anyway, here is an example of using a vector for your data structure:

    Code:
    class Token {
    	public:
    	std::string token;
    	int opcode;
    	bool type;
    	static vector<Token> Tokens;
    
    	void init(std::string a, int b, bool c)
    	{
    		token = a; 
    		opcode = b; 
    		type = c; 
    		Tokens.push_back(*this);
    	}
    	
    	Token(std::string a, int b, bool c) 
    	{
    		init(a, b, c);
    	}
    	
    	Token(char a, int b, bool c) 
    	{
    		char temp[2] = {a, 0};
    		init(temp, b, c);
    	}	
    };
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. static data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 05:18 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM

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