Thread: Static array of 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,268
    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,708
    >> 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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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