c++ unordered_set .find 方法无法编译,给出 "expression must have class type" 错误

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

因此,我正在尝试编写一个函数来确定集合中有多少字符串是该集合中其他字符串的变位词。为了快速做到这一点,我选择了对字符串进行排序,然后将它们移动到一个“ valid" 和 "invalid" 哈希集,这取决于我是否找到了重复项。问题是,当我尝试对 unordered_sets 使用 find 方法时,我得到一个编译时错误,它告诉我 "expression must have class type".

我浏览了整个网站,但没有看到任何包含我认为是同一问题的错误的帖子。

我在 visual studio 工作,使用 c++,我应该提到代码还没有完成;在给我错误的那一行之后我没有写任何东西。此外,它是 std::unordered_set "valid" 的特定名称,带有红色下划线。

同样值得注意的是,这段代码是一个正在进行的工作,所以有一些我可能不需要的东西被写下来;例如,我可能不会最终使用那些 long longs(因为我已经意识到尝试使用一个巨大的字符数组而不是字符串可能比它值得的更多努力。)

这是我正在研究的方法:

编辑:由于对其起源的敏感性,我删除了该方法的一些不相关部分。我为我缺乏远见而道歉。

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);

    }
}

此代码片段的最后一行未编译。这让我特别沮丧,因为它的写法与本 C++ 指南中的完全一致:

The c++ reference page I was looking at

我认为这就是我需要的全部代码,但经验告诉我,编程问题的原因通常是我认为不相关的部分代码。因此,我在下面发布了我的其余代码。

编辑:其余的代码原来是无关紧要的,所以为了清楚起见我删除了它。

这似乎不正确:

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

您正在声明两个 return 集合的函数,而不是两个集合。

你真的不想:

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