简体   繁体   English

错误:“(”令牌之前的预期主表达式

[英]error: expected primary-expression before ‘(’ token

In the following code, I am getting 在以下代码中,我得到

In member function ‘void no_matches::test_method()’:

error: expected primary-expression before ‘(’ token

    auto subject = anagram("diaper");

Code starts here 代码从这里开始

#include "anagram.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

using namespace std;

BOOST_AUTO_TEST_CASE(no_matches)
{
    auto subject = anagram("diaper");
    auto matches = subject.matches({"hello", "world", "zombies", "pants"});
    vector<string> expected;

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
};

Here is anagram.cpp 这是anagram.cpp

#include "anagram.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <algorithm>
#include <cctype>
using namespace std;
namespace
{
    string make_key(std::string const& s)
    {
        string key{boost::to_lower_copy(s)};
        sort(key.begin(), key.end());
        return key;
    }
}
namespace anagram
{
    anagram::anagram(string const& subject)
    : subject_(subject),
    key_(make_key(subject))
{
}
vector<string> anagram::matches(vector<string> const& matches)
{
    vector<string> result;
    for (string const& s : matches) {
        if (s.length() == key_.length()
        && boost::to_lower_copy(s) != boost::to_lower_copy(subject_)
        && make_key(s) == key_) {
        result.push_back(s);
        }
    }
    return result;
}
}

Here is anagram.h 这是anagram.h

#if !defined(ANAGRAM_H)
#define ANAGRAM_H
#include <string>
#include <vector>
namespace anagram
{
class anagram
{
    public:
        anagram(std::string const& subject);
        std::vector<std::string> matches(std::vector<std::string> const& matches);
   private:
        std::string const subject_;
        std::string const key_;
};
}
#endif

I am not gettting this error on my local machine. 我没有在本地计算机上收到此错误。 I am only getting it when I build with https://travis-ci.org . 我只有在使用https://travis-ci.org进行构建时才能得到它。 Can someone help me find the bug? 有人可以帮我找到错误吗?

You've put your class anagram inside a namespace anagram (poor idea, IMO), so the name you apparently want is anagram::anagram . 您已经将class anagram放在了namespace anagram (糟糕的想法,IMO)中,因此您显然想要的名称是anagram::anagram By itself, anagram just names the namespace. anagram本身仅命名空间。

So, at least at first glance, it appears the code should read: 因此,至少乍一看,该代码应显示为:

auto subject = anagram::anagram("diaper");

As to why you'd get it on one machine but not another: I'd guess you have a mismatched file, such as one containing a using namespace anagram; 至于为什么要在一台机器上而不是另一台机器:我猜您有一个不匹配的文件,例如一个包含正在using namespace anagram; that's missing from another. 那是另一个人所缺少的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM