Thread: Custom CVar system

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    Custom CVar system

    I am trying to write my own cvar system, similar to that found in quake games.

    This is my prototype...
    Code:
    class rCVarEngine
    {
    	public:
    		void setCVar(const char *name, const char *value);
    		void setNewCVar(const char *name, const char *value);
    		const char returnCVar(const char *name);
    	private:
    		rCVar *cvarList;
    };
    and I get this error for it...
    rCVar.h:31: error: multiple types in one declaration
    Then in this function where I return cvarList->value...
    Code:
    const char rCVarEngine::returnCVar(const char *name)
    {
    	while(cvarList)
    	{
    		if(cvarList->name == name)
    		{
    			return cvarList->value;			
    		} else {
    			cvarList = cvarList->next;
    		}
    		
    	}
    }
    I get this error:

    rCVar.h:74: error: invalud conversion from 'const char*' to 'char'
    Any help on fixing this would be great! I am using GCC v 3.4.2

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    const char rCVarEngine::returnCVar(const char *name)
    Perhaps this function is suposed to return a const char*?
    Code:
    const char *rCVarEngine::returnCVar(const char *name)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    if(cvarList->name == name)
    C strings can be compared with strcmp(..)

  4. #4
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Thanks! I still get the multiple types in one declaration error, in the class declaration.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I get the multiple type declaration errors when I forget to put a semi-colon after the class. Could this be the case, or could you show us where the exact line is?

  6. #6
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Code:
    class rCVarEngine
    {
    	public:
    		void setCVar(const char *name, const char *value);
    		void setNewCVar(const char *name, const char *value);
    		const char* returnCVar(const char *name);
    	private:
    		rCVar *cvarList;
    }; //<-- Error here, line 31

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    And what is before that? I highly suspect that it's simply a missing semi-colon.

  8. #8
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Well, you were right. I forgot a ; in my struct above my class :P

    *sighs
    Now I get these errors that I have no idea what they mean.
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0xd):test.cpp: undefined reference to `std::string::size() const'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0x60):test.cpp: undefined reference to `std::string:perator[](unsigned int) const'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0x9f):test.cpp: undefined reference to `std::string:perator[](unsigned int) const'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0xce):test.cpp: undefined reference to `std::string:perator[](unsigned int) const'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0x23b):test.cpp: undefined reference to `std::cout'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0x240):test.cpp: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0x275):test.cpp: undefined reference to `std::ios_base::Init::Init()'
    C:/DOCUME~1/ryan/LOCALS~1/Temp/ccUDbaaa.o(.text+0x290):test.cpp: undefined reference to `std::ios_base::Init::~Init()'
    collect2: ld returned 1 exit status

  9. #9
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Ok, it compiles fine, but now the program crashes when I run this function:

    Code:
    void rCVarEngine::setNewCVar(const char *name, const char *value)
    {
    	cvarList->next = 0;
    	
    	if(cvarList == 0)
    	{
    		strcpy((char*)cvarList->name, name);
    		strcpy((char*)cvarList->value, value);
    		return;
    	}
    	
    	rCVar *current = cvarList;
    	while(current->next)
    	{
    		current = current->next;
    	}
    	
    	current->next = cvarList;
    	cvarList->next = (rCVar*)0;
    }

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	if(cvarList == 0)
    	{
    		strcpy((char*)cvarList->name, name);
    		strcpy((char*)cvarList->value, value);
    		return;
    	}
    Operating on a null pointer

  11. #11
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Well, what that is doing, is if there is nothing set on cvarList, then set thoe values, instead of moving to the next object.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by St0rmTroop3er
    Well, what that is doing, is if there is nothing set on cvarList, then set thoe values, instead of moving to the next object.
    No. Look at it.
    Quote Originally Posted by Tonto
    Code:
    	if(cvarList == 0)
    	{
    		strcpy((char*)cvarList->name, name);
    		strcpy((char*)cvarList->value, value);
    		return;
    	}
    Operating on a null pointer
    If cvarList is a NULL pointer, then dereference it.


    "Hi, I'm pointing somewhere I can never write. Mind if I write this here?"
    *Crash* means, "No, I do mind".
    Last edited by Dave_Sinkula; 04-09-2006 at 10:13 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Ah, ok I fixed it... but it still crashes. I revamped it so I don't have two functions, one to set a new one and one to set an exisiting one. Hopefully I didn't mess it up.

    Linked Lists are big ol pain in the butt...and its so hard to understand them.

    Code:
    void rCVarEngine::set(const char *name, const char *value,bool lock)
    {
    	// If our head pointer is blank, then fill that first.
    	if(head == 0)
    	{
    		head->name = name;
    		head->value = value;
    		head->locked = lock;
    		return;
    	}
    	
    	while(cvarList)
    	{
    		// If we found an existing CVar
    		// then set its value and lock
    		// mode
    		if(cvarList->name == name)
    		{
    			cvarList->value = value;
    			cvarList->locked = lock;
    			return;
    		//Else if the next in the list is empty
    		// Set its new values
    		} else if(cvarList->next == 0) {
    	
    			// Create a temporary cvar
    			rCVar *newVar = new rCVar;
    			// Set its value
    			newVar->name = name;
    			newVar->value = value;
    			newVar->locked = lock;
    			newVar->next = 0;
    			
    			rCVar *temp = head;
    			while(temp->next)
    			{
    				temp = temp->next;
    			}
    			
    			// Add it to the end
    			temp->next = newVar;
    		// If we havn't found the end of the
    		// list or if we havn't found a pre-
    		// existing cvar, then move to the 
    		// next in the list
    		} else {
    			cvarList = cvarList->next;
    		} 
    		
    	}
    	
    }
    NOTE* I am following examples in C++ for Dummies 4th Edition.

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	if(head == 0)
    	{
    		head->name = name;
    		head->value = value;
    		head->locked = lock;
    		return;
    	}
    You're trying to dereference a null pointer again, allocate memory for it first, then set values. You might find it useful to have two auxillary pointers, head and tail, pointing to the first and last members of the list respectively.

    Code:
    if(cvarList->name == name)
    Probably need strcmp, unless they are std::string's now.

  15. #15
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Ok I added strcmp.

    I don't know how to allocate memory for that. :P Would it be better if I did

    if(strcmp(head->name,""))

    To check if it is empty?
    Your mom is like a struct, she has no class

    How many C programmers does it take to screw in a light bulb? One to do it, 99 to tell him how to do it faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Press a key
    By siavoshkc in forum C++ Programming
    Replies: 30
    Last Post: 01-21-2006, 10:15 AM
  2. New system build wont boot
    By lightatdawn in forum Tech Board
    Replies: 7
    Last Post: 12-02-2005, 06:58 AM
  3. Using mscorlib & system namespace in an MFC?
    By Robert_Sitter in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 06:47 PM
  4. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  5. system();
    By GanglyLamb in forum C Programming
    Replies: 5
    Last Post: 10-30-2002, 03:57 AM