1

Just wanted to know, why i'm getting conflicting declaration error while calling the constructor??

 class Base {
   int a;
   public:
      Base() { a = 50; };
      Base(int a): a(a) {
      }
 };

int main() {
  int k = 20;
  Base(k); // this should create a temp object right??
  // i'm getting foll error "conflicting declaration ‘Base k’" 
  // why does compiler treat it has a declaration??
  return 0;
}
Akash
  • 71
  • 1
  • 1
  • 8
  • You can put parentheses around the variable name: `int (k) = 20;` is fine. – Evg Jun 09 '19 at 10:30
  • 3
    @Yksisarvinen, this is parsed not as a function declaration (what function?), but as a variable declaration: `Base(k);` is equivalent to `Base k;`. https://stackoverflow.com/questions/29675601/why-does-c-allow-us-to-surround-the-variable-name-in-parentheses-when-declarin – Evg Jun 09 '19 at 10:35
  • thanks @Evg, that makes it more clear – Akash Jun 09 '19 at 10:38
  • @Evg Feel free to write this as an answer, or mark to close as a different duplicate (I missed to change it, just reopened). – πάντα ῥεῖ Jun 09 '19 at 10:50

0 Answers0