-1

When compiling this c++ code, I get an error on the last line.

#include <iostream>

class TestClass {

public:
    TestClass(int val) 
        : value(val)
    {
    }

    int getValue()
    {
        return value;
    }

private:
    int value;

};

int main()
{
    TestClass b();
    std::cout << b.getValue() << std::endl;     
}

The compiler (gcc on Ubuntu 14.04) complains:

error: "request for member ‘getValue’ in ‘b’, which is of non-class type ‘TestClass()’"

Now my question is: What exactly does b contain? The compiler doesn't complain about TestClass b().

jitv
  • 1
  • 1

1 Answers1

3

You have a vexing parse with:

TestClass b(); // This is a declaration of function b which return TestClass.

If you have default constructor, use:

TestClass b; 

or

TestClass b{}; 

But as you only have constructor with int parameter do

TestClass b(42);

or

TestClass b{42}; 
Jarod42
  • 203,559
  • 14
  • 181
  • 302