0

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

#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

#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 . Can someone help me find the bug?

twerk_it_606
  • 63
  • 3
  • 12

1 Answers1

3

You've put your class anagram inside a namespace anagram (poor idea, IMO), so the name you apparently want is anagram::anagram. By itself, anagram just names the namespace.

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; that's missing from another.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • When I do the `anagram::anagram("diaper");` it says I can't call the constructor explicitly on my local machine. Of course I don't have any namespaces on that code. – twerk_it_606 Oct 06 '14 at 23:14
  • Yes, trying to use `anagram::anagram` without a namespace would be a problem. So we've pretty much covered the real problem: you need to name the class properly for the compiler to find it (and if it has different names on the two machines, then it's no surprise that you need to *use* different names on those machines). – Jerry Coffin Oct 06 '14 at 23:26
  • Ok, as soon as I test it and make dinner I will mark this as correct – twerk_it_606 Oct 06 '14 at 23:37