2

Possible Duplicate:
Most vexing parse: why doesn't A a(()); work?

I have two classes in file1.h:

class ZoneRecord {
public:
    //a lof of stuff here
};

class RegisterRecord {
public:
RegisterRecord(ZoneRecord rec); //this function register object rec in a fabric
};

And file2.cpp has:

#include "file1.h"
class MockZoneRecord: public ZoneRecord {
public:
MockZoneRecord(): ZoneRecord() {}
};

RegisterRecord mockrecord_register(MockZoneRecord());

This code compiles perfectly, except one thing. It says that mockrecord_register is a declaration of a function. But I actually wanted to create an global object of type RegisterRecord with name mockrecord_register. How to explicitly tell to compiler that this is not a function prototype, but an object?

Community
  • 1
  • 1
Ivan Kruglov
  • 743
  • 3
  • 12

2 Answers2

4

You are experiencing the most vexing parse.

One way to solve this is to use copying, like

RegisterRecord mockrecord_register = RegisterRecord(MockZoneRecord());

Another is the use of parenthesis like in the answer by yuri kilochek.

If your compiler is C++11 compatible, you could use this construct:

RegisterRecord mockrecord_register{MockZoneRecord()};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Place parenthesis around argument:

RegisterRecord mockrecord_register((MockZoneRecord()));
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59