Private Static class members

This is a discussion on Private Static class members within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a class to hold two variables to be used by a number of functions in ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Private Static class members

    Hi,

    I'm trying to write a class to hold two variables to be used by a number of functions in the program. Here's the class:
    Code:
    class InBAF
    {
    private:
    	static char location;
    	static CString server;
    
    public:
    	static void SetLocation(char newloc)
    	{
    		location = newloc;
    	}
    
    	static void SetServerName(char location)
    	{
    		switch (location)
    		{
    		case 'T':	
    				// Input Toronto Server
    			server = "ftp.pureftpd.org";
    			break;
    
    		case 'M':
    				// Input Montreal Server
    	                	server = "ftp.pureftpd.org"; 
    			break;
    		}
    	}
    
    	static CString GetServerName()
    	{
    		return server;
    	}
    };
    char InBAF::location = 'N';
    CString InBAF::server = '\0';
    when I try to compile I get this error for each member of the class:
    Code:
    private: static char  InBAF::location" (?location@InBAF@@0DA)
     already defined in GetData.obj
    GetData is a different function in my project; it has nothing to do with this class.

    What am I missing?

    and I tried:
    Code:
    CString InBAF::server[0] = '\0';
    but this comes up:
    Code:
    error C2466: cannot allocate an array of constant size 0
     error C2040: 'private: static class CString  InBAF::server' : 'class CString []' differs in levels of indirection from 'class CString'
    Last edited by earth_angel; 08-26-2005 at 09:48 AM. Reason: Code formating
    Everything is relative...

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I declare the class in the main header file. GetData.cpp has that header file included, and alphabetically it is compiled first. Can that be the problem? Should I define the class in a .cpp file?

    Edit: I moved the class definition to the main .cpp file and everythign seems to be happy. But that doesn't really make sense to me....
    Last edited by earth_angel; 08-26-2005 at 09:57 AM.
    Everything is relative...

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> CString InBAF::server = '\0';

    You should not assign a character to a CString. If you want the CString to be empty, just don't initialize it when you define it (the default constructor will initialize it to be empty).
    Code:
    CString InBAF::server;
    As with your other thread, you should never assign '\0' to the CString. A CString is a class that is not necessarily null-terminated, so assigning a null character to it doesn't make it empty. Always assign a string literal instead of a character literal to a CString (e.g. ""), or call the Empty() function to empty the contents.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    >> or call the Empty() function
    I didn't know about that function, but that's what I was trying to do with '\0'
    and a string literal is " "
    and a character literal is ' ', right? I haven't heard of it before.
    Everything is relative...

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Eh?

    Unless you know the internal representation of strings in CString, you cannot assume anything about them. And, if you do know something about the internal representation, then you should promptly put it out of mind and try to forget it.

    The \0 character is simply a signalling device -- a convention used with C-style strings. Instead of explicitly passing the string length everywhere (as is done with normal arrays), you simply read up until the \0. Now, a class may store an unterminated character array, and the length (which has the benefit of making calls to get the length constant time instead of linear time). If it does have a terminating character, then it will add it itself. So, when copying a char*, it will do what it has to with the terminating \0.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Yes, the double quotes are used for string literals, and the single quotes are used for character literals. So even if your string only has only zero or one characters, you would use double quotes if you want to treat it as a string. And of course a character literal in single quotes must be exactly one character.

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, in sight of all this. I'm readign a filepath that needs to be used by a different function. But the read may use \ or / for directory separation. to pen a file I have to use either \\ or /. would this be appropriate:
    Code:
    for(int i=0; i<strlen(fname); i++)
    		{
    			if(fname[i] == '\\')
    				fname.SetAt(i, '/');
    		}
    fname is a CString.
    Last edited by earth_angel; 08-26-2005 at 01:17 PM. Reason: Correcting code
    Everything is relative...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I would use the CString Replace function:
    Code:
    fname.Replace('\\', '/');

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Additionally, I'd imagine that there isn't an overload of strlen for CString, but it has it's own length function.

    (Though, the existence of a replace function definitely makes it the proper tool for this job.)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    GetLength() is the proper length function for a CString, although technically strlen would work because CString has an implicit conversion to LPCTSTR which ends up being a const char*.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,432
    Coming back to the original problem, move the definition of the statics;

    Code:
    char InBAF::location = 'N';
    CString InBAF::server = '\0';    // check this too, although it's not relevant to the original problem
    out of the header file and into one (and only one) of the source files that #include's that header.

    The reason the LINKER is complaining is that, by placing those lines into the header file, every source file that #include's that header instantiates a copy of the statics. That is a violation of the "One Definition Rule".

  12. #12
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Couldn't he also use the code
    Code:
    #ifndef INBAF_H
    #define INBAF_H
    
    class InBAF
    {
    .
    .
    .
    };
    #endif
    This is how I always do my classes is this bad form or something?

  13. #13
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    @~Kyo~:
    No include-guards do nor help in this case.
    The problem in this case is not that the .h-file is included multiple times in one compilition unit ( this is what include-guards are used for ) the problem is that this .h-file is included in different compilation units. So the variables InBAF::location and InBAF::server end up in different .o -files and the linker cannot resolve this conflict.
    Kurt

  14. #14
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I didn't do my homework properly in researching the CString functions.

    Thanks guys,

    AS.
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Read-only class members
    By kidburla in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2006, 12:52 PM
  3. are static class members the right choice?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2005, 09:41 AM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  5. program to unpack packed data
    By vsk in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 08:17 PM

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