ok, I know this is probably going to be something really easy but I have this code implementing the boost libraries (this is my first time using it) and I can't seem to get it to work correctly. here's the code. hash.h

#include .....
#include "boost/unordered_set.hpp"
#ifndef HASH_H
#define HASH_H

class hash{
public:
    int toHash(string);
    void insertKey(string);
    bool lookupKey(string);
private:
    string hashTable[];
    vector<string> vfile;
    typedef boost::unordered_set<std::string, int> um;
struct hashkey{
    string state;
    int stateno;
};

};

#endif  /* HASH_H */

also the hash.cpp

#include <boost/unordered/unordered_set.hpp>
#include "hash.h"

int hash::toHash(string key){
    unsigned int x;
    std::stringstream ss;
    ss << std::hex << key;
    ss >> x;
    return x;
}

void hash::insertKey(string key){
    um.insert(key,toHash(key));
}

bool hash::lookupKey(string key){
    return um.find(key)==um.end();
}

I'm getting "hash.cpp:18:7: error: expected unqualified-id before ‘.’ token". And I reiterate, I know it's probably quite easy, I just haven't used boost libraries before. I looked at many examples on the internets and I can't seem to just get this "simple" piece to work.Thanks.

有帮助吗?

解决方案

Your um isn't a member variable, it's a typedef. Get rid of the typedef keyword.

Now with regard to your second issue......

Get rid of your hash computation member method & the template's second arg. Get rid of insert()'s second arg. boost unordered_set already provides hash functions for many standard types, including std::string. If you want to hash a user-defined type, though, you will need to provide a hash function, but not in the way you're doing it here. You'd create an overload of a function named hash_value() like this:

std::size_t hash_value(yourUserDefinedType_probablyAClassName const &t) 
{
    std::size_t retValue;
    //...compute your hash however you want & store in retValue....
    return retValue;
} 

其他提示

You use typedef to declare a type. um is a type, not a variable. Remove typedef from your code, or declare a variable of type um.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top