0

The question sounds a bit odd, but check the code sample

#include <iostream>

using namespace std;

int main() {
    string a, b(), c("test");

    // No problems
    a = c;

    // tester.cpp:9:7: error: assignment of function ‘std::string b()’
    b = c;

    // tester.cpp:10:7: error: invalid conversion from ‘std::string (*)() {aka std::basic_string<char> (*)()}’ to ‘char’
    a = b;

    // No problems
    c = a;
}

You can see that it looks like b is created with the default constructor, but in fact it is not. So my question is really is, what does string b() mean?

Leon
  • 12,013
  • 5
  • 36
  • 59

3 Answers3

1

Condensing your code to string b();, what you're doing here is declaring a prototype to a function called b that returns a string and takes no arguments.

The commas in the "compound declaration" are obfuscating this.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

It's actually a function declaration. It's possible to declare local functions but not to define them. So therefore it's not possible to explicitly call the default constructor.

Fytch
  • 1,067
  • 1
  • 6
  • 18
1

Classic mistake, but the error message already says it: function std::string b() /.

MSalters
  • 173,980
  • 10
  • 155
  • 350