heeeeeeellllllpppppppp!!!

This is a discussion on heeeeeeellllllpppppppp!!! within the C++ Programming forums, part of the General Programming Boards category; EDITED!!! ok, this is yet another edited version, thanks to ZEN, the .cpp and .h file used to get these ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    43

    Exclamation heeeeeeellllllpppppppp!!!

    EDITED!!!

    ok, this is yet another edited version, thanks to ZEN, the .cpp and .h file used to get these errors is below in one of his posts, and well...these are the errors im getting,
    i dont understand the first few, the not declared ones and the syntax. especially since he is the one who tinkered with it



    HashMap.cpp:4: syntax error before `::'
    HashMap.cpp:12: `KeyType' was not declared in this scope
    HashMap.cpp:12: `ValueType' was not declared in this scope
    HashMap.cpp:12: syntax error before `*'
    HashMap.cpp:14: `KeyType' was not declared in this scope
    HashMap.cpp:14: `ValueType' was not declared in this scope
    HashMap.cpp:14: syntax error before `*'
    HashMap.cpp:15: ANSI C++ forbids declaration `buckets' with no type
    HashMap.cpp:15: parse error before `['
    HashMap.cpp:16: syntax error before `<='
    HashMap.cpp:16: syntax error before `++'
    HashMap.cpp:21: syntax error before `='
    HashMap.cpp:25: syntax error before `::'
    HashMap.cpp:27: syntax error before `<='
    HashMap.cpp:27: syntax error before `++'
    HashMap.cpp:35: syntax error before `::'
    HashMap.cpp:43: syntax error before `->'
    HashMap.cpp:44: syntax error before `->'
    HashMap.cpp:45: `key' was not declared in this scope
    HashMap.cpp:45: implicit declaration of function `int hashfunc(...)'
    HashMap.cpp:45: ANSI C++ forbids declaration `buckets' with no type
    HashMap.cpp:45: `newptr' was not declared in this scope
    HashMap.cpp:45: assignment (not initialization) in declaration
    HashMap.cpp:46: parse error before `return'
    HashMap.cpp:50: syntax error before `::'
    HashMap.cpp:53: `KeyType' was not declared in this scope
    HashMap.cpp:53: `ValueType' was not declared in this scope
    HashMap.cpp:53: syntax error before `*'
    HashMap.cpp:54: ANSI C++ forbids declaration `front' with no type
    HashMap.cpp:54: `key' was not declared in this scope
    HashMap.cpp:55: ANSI C++ forbids declaration `back' with no type
    HashMap.cpp:56: parse error before `if'
    HashMap.cpp:69: ANSI C++ forbids declaration `front' with no type
    HashMap.cpp:69: redefinition of `int front'
    HashMap.cpp:54: `int front' previously defined here
    HashMap.cpp:69: base operand of `->' is not a pointer
    HashMap.cpp:70: parse error before `}'
    HashMap.cpp:75: syntax error before `::'
    HashMap.cpp:84: ANSI C++ forbids declaration `runner' with no type
    HashMap.cpp:84: base operand of `->' is not a pointer
    HashMap.cpp:85: parse error before `}'
    HashMap.cpp:91: syntax error before `::'
    HashMap.cpp:100: ANSI C++ forbids declaration `runner' with no type
    HashMap.cpp:100: redefinition of `int runner'
    HashMap.cpp:84: `int runner' previously defined here
    HashMap.cpp:100: base operand of `->' is not a pointer
    HashMap.cpp:101: parse error before `}'
    HashMap.cpp:102: syntax error before `='
    HashMap.cpp:108: syntax error before `::'
    HashMap.cpp:117: ANSI C++ forbids declaration `runner' with no type
    HashMap.cpp:117: redefinition of `int runner'
    HashMap.cpp:100: `int runner' previously defined here
    HashMap.cpp:117: base operand of `->' is not a pointer
    HashMap.cpp:118: parse error before `}'
    HashMap.cpp:123: syntax error before `&'
    HashMap.cpp:123: `ostream' was not declared in this scope
    HashMap.cpp:123: `o' was not declared in this scope
    HashMap.cpp:123: parse error before `<'
    HashMap.cpp:124: ANSI C++ forbids declaration `operator <<' with no type
    HashMap.cpp:124: `operator <<(...)' must have an argument of class or enumerated type
    HashMap.cpp:124: `operator <<(...)' must take exactly two arguments
    HashMap.cpp: In function `int & operator <<(...)':
    HashMap.cpp:127: `bucket' undeclared (first use this function)
    HashMap.cpp:127: (Each undeclared identifier is reported only once
    HashMap.cpp:127: for each function it appears in.)
    HashMap.cpp:127: `hm' undeclared (first use this function)
    HashMap.cpp:130: warning: control reaches end of non-void function `operator <<(...)'
    HashMap.cpp: At top level:
    HashMap.cpp:133: syntax error before `::'
    HashMap.cpp:136: `KeyType' was not declared in this scope
    HashMap.cpp:136: `ValueType' was not declared in this scope
    HashMap.cpp:136: syntax error before `*'
    HashMap.cpp:142: syntax error before `<'
    HashMap.cpp:150: ANSI C++ forbids declaration `runner' with no type
    HashMap.cpp:150: redefinition of `int runner'
    HashMap.cpp:117: `int runner' previously defined here
    HashMap.cpp:150: base operand of `->' is not a pointer
    HashMap.cpp:151: parse error before `}'
    HashMap.cpp:152: syntax error before `='
    make: *** [proj5] Error 1
    Attached Files Attached Files
    Last edited by DarkDays; 12-09-2001 at 05:37 PM.

  2. #2
    Unregistered
    Guest

    Didn't even look @ entire file

    I didn't even look at ur entire file and found a couple of errors. I will tell u what to do and come back when those are fixed.

    never declared buckets before u used it in the 4th line.

    write past the end of the array on lines 12-15 using ur loop

    the whole bottom part of ur program is commented out(not sure if u did this on purpose)

    not sure if u r still allowed to declare variables inside loop instructions(i don't)



    Now get workin and good luck!!

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    the part is commented out because if not there are so many errors i cant scroll to the top, and im too lazy to use a pipe. just trying to get one function working at a time..

    dont have to declair buckets, its a private data member of the HashMap class, its a given.

    and you can declair variables in a loop but i find they dont work outside of the loop, i only do that if i plan to only use them in the loop

    ill look into that going off the end of my array thing, but the rest of the errors you found should be fine and im positive they are not whats causing the errors at this point

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Everywhere you've defined a function like -

    template HashMap<class KeyType, class ValueType>
    bool HashMap::Replace ( KeyType key, ValueType value)


    you'll have to change to -

    template <class KeyType, class ValueType>
    bool HashMap<KeyType,ValueType>::Replace ( KeyType key, ValueType value)

    and every instance of a template object or pointer need should be -

    element<KeyType,ValueType> *front;

    Also , a few other errors spotted -

    One function was missing a closing brace, and one function had a parameter 'int buckets' which is re-defined in the function (did you mean 'int numbuckets').

    There are probably others, but this should get you started.
    zen

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    43

    aaah

    noted, but that dosnt fix the damn nagging errors i have in my constuctor, i commented out most of it so i could get one damn fucntion working then go from that, nobody seems to know why i am getting the errors i posted, they are all just from the constructor

    i know im missing they <KeyType, ValueType> in most of the functions, ill add them as i go

    aside from that your info was news to me, thanks =)

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    ok...new plan of attack, here is my current big problem...
    i am getting a parse error before the { at the end of this line...why? (its the line right after the constructors definition fi you want to see the full code) this parse error is causing a LOT of errors


    template HashMap<class KeyType, class ValueType>


    HashMap<KeyType, ValueType>::HashMap(hashfunc h, int numbuckets)
    {


    that constructor return type invalid error is also ........ing me off, i cant figure out whats causing it

    Ive edited my code above along with the list of errors, its up to date now
    Last edited by DarkDays; 12-09-2001 at 04:47 PM.

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You might find using typedefs for different things when using templates will help. It reduces the risk of missing template parameters.

    Looking at your code, it would appear you have more problems though, as you never actually store anything in your private member variables (from what I can see). You just create local variables in your constructor -

    element <KeyType, ValueType> **bucket;

    and don't store numbuckets or the hashfunc anywhere. You allocate memory and then loose the pointer to it as soon as the constructor ends. It's probably a bit late now but it's normally a good idea to create a fully functional non-template class first, and once you know everything is working correctly transform it to a template.
    zen

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    i realized the non template thing about 2 days ago.,..by then i was done writing it anyway, and had begun debugging...what can i say, this is my first time actually writing templates so i guess we all get one screw up, right?

    now about that constructor thing, ive been trying to use the private data and i think i have it, but there is no way to know...the parse errors and that return type error make the compiler mad before it gets to that code =( i should prob post the new version of errors and code soon, but im sure you can imagine it =)
    the errors are still the same, just tinkered with the constructor a bit

  9. #9
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I've been through your code, and removed all the syntax errors that are reported in Dev C++ (however, you may still get errors as templates aren't very consistent from one compiler to the next), but I don't know what your doing in some areas of your code and am pretty sure that it won't work as expected -

    Code:
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <stdexcept>
    using std::setw;
    using std::string;
    using std::cin;
    using std::cout;  
    using std::cerr; 
    
    
    
    template < class KeyType, class ValueType  >
    
    struct element
    {
       element *next;
       ValueType value;
       KeyType key;
    }; 
    
    template < class KeyType, class ValueType >
    class HashMap
    {
       public:
             
    
          typedef int (*hashfunc) ( KeyType key );
    
          HashMap( hashfunc h, int buckets );  
    
          ~HashMap( );
    
          bool Insert ( KeyType key, ValueType value); 
    
          bool Remove ( KeyType key );  
    
          bool Replace ( KeyType key, ValueType value);
    
          ValueType Find ( KeyType key ) const; 
    
          bool Has ( KeyType key ) const;  
    
          template < class K, class V > 
          friend ostream& operator<< ( ostream& o, const HashMap < K, V > & hm);
    
          void PrintBucket ( KeyType key ) const;  
    
       private:
          element <KeyType, ValueType> *bucket;
          int numbuckets;         
                      
    };
    
    template <class KeyType, class ValueType> 
    HashMap<KeyType, ValueType>::HashMap(hashfunc h, int _numbuckets) 
    { 
        numbuckets=_numbuckets;             
    	if (numbuckets <= 0) 
    	{ 
    		string s1 = "HashMap Constructor"; 
    		throw HashMapException(s1); 
    	} 
       typedef element<KeyType,ValueType>* elp;
    
    	element<KeyType,ValueType> **buckets; 
    	buckets = new elp[numbuckets]; 
    	for ( int i = 0; i <= numbuckets; i++ ) 
    	{ 
    		buckets[i] = NULL; 
    	} 
    
    	hashfunc d = h; 
    } 
    
    template <class KeyType, class ValueType> 
    HashMap<KeyType, ValueType>::~HashMap( ) 
    { 
    	for ( int i = 0; i <= numbuckets; i++) 
    	{ 
    		buckets[i] = NULL; 
    	} 
    	delete [] buckets; 
    } 
    
    template <class KeyType, class ValueType> 
    bool HashMap<KeyType, ValueType>::Insert ( KeyType key, ValueType value) 
    { 
        
        //What is this for?
    	//HashMap node<KeyType, ValueType>(hashfunc, numbuckets); 
    	element<KeyType,ValueType> *newptr = new element<KeyType,ValueType>; 
    	if (newptr == NULL) 
    	{ 
    		return false; 
    	} 
    	newptr -> data = value; 
    	newptr -> next = buckets[hashfunc(key)]; 
    	buckets[hashfunc(key)] = newptr; 
    	return true; 
    } 
    
    template <class KeyType, class ValueType> 
    bool HashMap<KeyType, ValueType>::Remove ( KeyType key ) 
    { 
    	element<KeyType,ValueType> *front; 
    	element<KeyType,ValueType> *back; 
    	front = buckets[hashfunc(key)]; 
    	back = front; 
    	if (front -> key == key) 
    	{ 
    		delete front; 
    		return true; 
    	} 
    	while (front != NULL) 
    	{ 
    		if (front -> key == key) 
    		{ 
    			back -> next = front -> next; 
    			delete front; 
    			return true; 
    		} 
    	front = front -> next; 
    	} 
    	return false; 
    } 
    
    template <class KeyType, class ValueType> 
    bool HashMap<KeyType, ValueType>::Replace ( KeyType key, ValueType value) 
    { 
    	element<KeyType,ValueType> *runner = buckets[hashfunc(key)]; 
    	while (runner != NULL) 
    	{ 
    		if (runner -> key == key) 
    		{ 
    			runner -> value = value; 
    			return true; 
    			runner = runner -> next; 
    		} 
    	return false; 
    	}
    }
    
    template <class KeyType, class ValueType> 
    ValueType HashMap<KeyType, ValueType>::Find ( KeyType key ) const 
    { 
    	element<KeyType,KeyType> *runner = buckets[hashfunc(key)]; 
    	while (runner != NULL) 
    	{ 
    		if (runner -> key == key) 
    		{ 
    			return runner -> value; 
    		} 
    	runner = runner -> next; 
    	} 
    	string s1 = "Find"; 
    	throw HashMapException (s1); 
    
    } 
    
    template <class KeyType, class ValueType> 
    bool HashMap<KeyType, ValueType>::Has ( KeyType key ) const 
    { 
    	element<KeyType,ValueType> *runner = buckets[hashfunc(key)]; 
    	while (runner != NULL) 
    	{ 
    		if (runner -> key == key) 
    		{ 
    			return true; 
    		} 
    	runner = runner -> next; 
    	} 
    	return false; 
    } 
    
    template < class K, class V > 
    ostream& operator<< ( ostream& o, const HashMap < K, V > & hm) 
    { 
    	for (int i = 0; i < buckets; i++) 
    	{ 
    		bucket[i].PrintBucket(hm->key); 
    	} 
    
    } 
    
    template <class KeyType, class ValueType> 
    void HashMap<KeyType, ValueType>::PrintBucket ( KeyType key ) const 
    { 
    	int bucketnum = hashfunc(key); 
    	element<KeyType,ValueType> *runner = bucket[hashfunc(key)]; 
    	if (runner == NULL) 
    	{ 
    		cout << "Bucket " << bucketnum << " is empty" << endl; 
    		return; 
    	} 
    	cout << "Bucket " << bucketnum << " contains: " << endl; 
    	while (runner != NULL) 
    	{ 
    		if (runner -> key == key) 
    		{ 
    			cout << "(" << runner -> key << ", " << runner -> value << endl; 
    			return; 
    		} 
    	runner = runner -> next; 
    	} 
    	string s1 = "PrintBucket"; 
    	throw HashMapException (s1); 
    }
    
    int main()
    {
    
    	return 0;
    }
    zen

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    zen, you are a GOD! this is the second time you saved my ass...that got rid of most of the errors, all thats left is syntax **** i can work out on my own

    at this point i dont care if it works right or not, as long as it compiles...wont get it too much trouble if a little trouble shooting is all that is left to do =)
    thanks again

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >wont get it too much trouble if a little trouble shooting is all that is left to do =)
    I wouldn't be too sure about that if I were you

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    arg, still major problems...
    im going to try to work with this for a while, but i may need to make another post soon...
    this project is a killer

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    ok, updated error list is at the top, damn syntax ****

  14. #14
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I wouldn't concentrate too heavily on the error messages you're getting as templates seem to mess up alot of compilers error reporting. You could create a copy of your files and strip out the template code and try and compile. Then concentrate on getting this code working.
    zen

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    43
    ive posted the problems im having with using this thing w/o templates...tons of errors still
    if anyone wants to help (zen =) ) its in the c++ board as a new post
    i think titled HELP! AGAIN! or something along that line
    thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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