Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with my Hash_set

Status
Not open for further replies.

homeless

Programmer
Nov 6, 2001
20
CH
I try to fill the m_theDefinitions atribute on class CDefinitionContainer.


the CDefinitionContainer.h
Code:
#include <iostream>
#include <string>
#include <vector>
#include "string_hasher.h" // with included <hash_set>

class CDefinition;


class CDefinitionContainer {
public: 
CDefinitionContainer();
void addDefinition(std::string,unsigned short);
void dump(void);
private:
	stdext::hash_set<CDefinition*,definitionhasher> m_theDefinitions;
};

#endif //CDEFINITIONCONTAINER_H


the the CDefinitionContainer.cpp
Code:
#include "CDefinition.h"
using namespace std;
using namespace stdext;

typedef hash_set<CDefinition*,definitionhasher>::iterator  iter_def;

void CDefinitionContainer::addDefinition(string N,unsigned short SiV){ 
	m_theDefinitions.insert(new CDefinition(N,SiV)));
}

void CDefinitionContainer::dump(){
	for(iter_def i = m_theDefinitions.begin() ;i < m_theDefinitions.end(); ++i)
		(*i)->dump(); 
}




the definitionhasher.h
Code:
#define DEFINITION_HASHER_H

#include <hash_set>
#include <string>

#include "CDefinition.h"


class definitionhasher : public
stdext::hash_compare<CDefinition*>
{
public:

 size_t operator() (const CDefinition* &s) const{
    size_t h = 0;
	std::string::const_iterator p, p_end;
	for(p = s->m_strSymbolName.begin(), p_end = s->m_strSymbolName.end(); p != p_end; ++p)
    {
      h = 31 * h + (*p);
    }
    return h;
  }

bool operator() (const CDefinition* &s1, const CDefinition* &s2) const
  {
	  return s1->m_strSymbolName < s2->m_strSymbolName;
  }
  
};


#endif

and the definition class looks so:

Code:
#ifndef CDEFINITION_H
#define CDEFINITION_H

#include <iostream>
#include <string>

class CDefinition {
protected:
        friend class definitionhasher;
        std::string m_strSymbolName;
public:
    	~CDefinition();
    	CDefinition(std::string, unsigned short);
	bool operator<( CDefinition const& a){
			return this->m_strSymbolName < a.m_strSymbolName ;}
        void dump(void);
};


#endif //CDEFINITION_H




I become the error:

c:\Documents and Settings\My Documents\Visual Studio Projects\hashing\CDefinitionContainer.cpp(67): error C2676: binary '<' : 'iter_def' does not define this operator or a conversion to a type acceptable to the predefined operator




the error refers to the function
Code:
void CDefinitionContainer::dump(){
	for(iter_def i = m_theDefinitions.begin() ;i < m_theDefinitions.end(); ++i)
		(*i)->dump();
on the CDefinitionContainer.cpp File
 
For STL iterators, you should always use this format by using the != operator instead of <
Code:
for ( iter_def i = m_theDefinitions.begin() ; i != m_theDefinitions.end(); ++i )
 
I can't belive it, but you have right.
THX cpjust. It seems to work now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top