Search code examples
c++visual-studiocompiler-errorsfindunordered-set

c++ unordered_set .find method won't compile, gives "expression must have class type" error


So, I'm trying to write a function that can determine how many strings in a collection are anagrams of other strings in that collection. To do this quickly, I've elected to sort the strings, and then move them into a " valid" and "invalid" hash set, depending on whether I've found a duplicate. Trouble is, When I try to use the find method for unordered_sets, I get a compile time error that tells me "expression must have class type".

I've looked around the website, but I didn't see any posts with that error that I recognized as being the same problem.

I'm working in visual studio, in c++, and I should mention that the code is not finished; I haven't written anything after the line that's giving me the error. Also, it is specifically the name of the std::unordered_set "valid" that is underlined in red.

It is also worth noting that this code is a work-in-progress, so there are a few things written down that I may not actually need; for example, I probably won't end up using those long longs (because I've realized that trying to use a single, enormous character array rather than strings is probably more effort than it's worth.)

Here is the method I'm working on:

Edit: I removed some of the irrelevant parts of this method due to sensitivities concerning it's origin. I apologize for my lack of foresight.

int Anagram_Locator::filterAnagrams()
{
    ...

    //the valid and invalid hash sets
    std::unordered_set<std::string> valid();
    std::unordered_set<std::string> invalid();
    //pull in the words, and sort them. Then, send them to either the valid or invalid string hash sets
    while (std::cin >> transferString)
    {

        ...

        //is it in the valid list?
        std::unordered_set<std::string>::const_iterator found = valid.find (transferString);

    }
}

The last line in this code snippet is the one that is not compliling. This is particularly frustrating to me, because it is written exactly as it was in this c++ guide:

The c++ reference page I was looking at

I would think that this is all the code I would need, but experience has taught me that programming problems often have causes in parts of the code that I think are irrelevant. As such, I have posted the rest of my code below.

Edit: the rest of the code turned out to be irrelevant, so I removed it for clarity.


Solution

  • This appears incorrect:

    std::unordered_set<std::string> valid();
    std::unordered_set<std::string> invalid();
    

    You are declaring two functions that return sets, not two sets.

    Don't you really want:

    std::unordered_set<std::string> valid;
    std::unordered_set<std::string> invalid;