1

I have this code:

namespace js0n
{

struct json
{
  typedef int json_object;

  json(){}

  json(json_object const& other)
  {
  }

  json& operator=(json_object const& other)
  {
    return *this;
  }
};

typedef json::json_object json_object;

}

The line js0n::json json(js0n::json_object()); gives a compile error.

int main()
{
  js0n::json json(js0n::json_object());

  return 0;
}

Note that I've culled away much of the code, as it is not responsible for the error. What am I doing wrong?

The error message:

test.cpp: In function 'int main()':
test.cpp:9:8: error: request for member 'parse' in 'json', which is of non-class type     'js0n::json(js0n::json_object (*)()) {aka js0n::json(int (*)())}'

The assignment operators i.e. (json = json_object();) is working just fine.

Xeo
  • 129,499
  • 52
  • 291
  • 397
user1095108
  • 14,119
  • 9
  • 58
  • 116

2 Answers2

3

You haven't said what the compilation error is, but it looks like you've tripped over the most vexing parse:

js0n::json json(js0n::json_object());

This declares a function. To declare a local variable, you need more parentheses:

js0n::json json((js0n::json_object()));
                ^                   ^
James McNellis
  • 348,265
  • 75
  • 913
  • 977
3

js0n::json json(js0n::json_object());

Welcome to the most vexing parse. This declares a function returning js0n::json and taking a js0n::json_object argument. To disambiguate, either add more parens, as suggested in the answer to that question, or (since you originally tagged this [c++11]), use list-initialization:

js0n::json json{js0n::json_object()};

Or

js0n::json json(js0n::json_object{});

Or

js0n::json json{js0n::json_object{}};

:)

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Yeah I've tagged `c++11` because of the `std::unordered_map`. Thanks! – user1095108 Nov 04 '12 at 17:26
  • @user: Always tag with `[c++]` atleast, and if it's something specific about C+11, add `[c++11]`. However, your code could've been cut down to no includes at all and a 5-10 line example, really. – Xeo Nov 04 '12 at 17:28
  • Cull complete, also note the `= default` alongside the default constructor, another C++11 feature. – user1095108 Nov 04 '12 at 17:40
  • @user: I culled it even further to drop that one too. :) Also, really, C++11 is the current standard, so the C++ tag alone suffices. Only if the question's *specifically* about a C++11 feature I'd add the C++11 tag. – Xeo Nov 04 '12 at 17:43