7

It says the addition of default argument on redeclaration makes this constructor a default constructor.

I did some research on it but I just don't understand what I need to do to fix this issue.

struct Transaction{
    int type;
    int amount;
    int to_from_type;
    Transaction(int, int, int);
};

Transaction :: Transaction(int type=0, int amount=0, int etc=0)
{
    this->type=type;
    this->amount=amount;
    this->to_from_type=etc;
}




 Transaction :: Transaction(int type=0, int amount=0, int etc=0) //I am getting an error at this code and I cannot fix it.

    {
        this->type=type;
        this->amount=amount;
        this->to_from_type=etc;
    }

I am not an expert in C++ and would love to get some help with my code.

Toni
  • 125
  • 1
  • 6
  • Could you mention which compiler you are using? I've tested your code on G++ and Visual C++ but didn't get even an warning. Everything seems to be working fine. – Faisal Rahman Avash Apr 29 '19 at 02:55
  • `Transaction(int, int, int);` is clearly defined twice. Remove one of the definitions and see what happens. – user4581301 Apr 29 '19 at 02:56
  • 2
    Don't add default arguments on redeclaration, de lare them on the firat declaration. Also use a [bases members initialiser](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor). – n. m. could be an AI Apr 29 '19 at 03:01
  • I'm trying to compile this on Xcode – Toni Apr 29 '19 at 03:01
  • @Toni, please find out what compiler you are using with Xcode. I've run your code in standard C++ environments and didn't run into any problems. – Faisal Rahman Avash Apr 29 '19 at 03:04
  • @FaisalRahmanAvash I used Xcode to create this project for C++, I think Xcode is my compiler. – Toni Apr 29 '19 at 03:10
  • @FaisalRahmanAvash I ran my code in VS and it didn't give me any errors. I don't know why it is giving me an error in Xcode – Toni Apr 29 '19 at 03:16

1 Answers1

9

XCode is using a combination of CLang and Apple LLVM to compile your C++ code. Clang enforces a few additional checks which includes your case. What happened here is you defined a constructor to accept 3 params but your implementation can be called without any, meaning, your implemented one is actually carrying the same method signature (method name and param list) as the implicit default constructor and the one with 3 params (the one defined inside the struct) is getting left out in the eyes of your compiler. The fix is easy:

struct Transaction{
    int type;
    int amount;
    int to_from_type;
    Transaction(int=0, int=0, int=0);
};

Transaction :: Transaction(int type, int amount, int etc)
{
    this->type=type;
    this->amount=amount;
    this->to_from_type=etc;
}
Faisal Rahman Avash
  • 1,268
  • 9
  • 13
  • its probably silly but this (obviously) works for class definitions as well including a scenario where you are using a separate .h and .cpp to keep your project organised. I am using clang++ on Mac OS VS Code Terminal. – Jay Feb 06 '21 at 15:26
  • 1
    so, it looks like your solution was to remove the default arguments in the constructor, which now makes it clear that there is only 1 no argument constructor, which just so happens to call the non-default constructor (with the intended default argument values). seems clear! – Adam Feb 19 '23 at 18:58