C++ Help: "error: conflicting declaration" with multiple constructors

I’m confused about this compile-time error I’m getting when running
g++ -std=c++0x dtpulse.cpp dtpulse.hpp test.cpp -o a.out:

dtpulse.cpp: In constructor ‘LaundrySoupSequence::LaundrySoupSequence(std::string)’:
dtpulse.cpp:442: error: conflicting declaration ‘LaundrySoupSequence calculatedTokens’
dtpulse.cpp:424: error: ‘calculatedTokens’ has a previous declaration as ‘std::vector<Token, std::allocator<Token> > calculatedTokens’

Here are the constructors defined in dtpulse.cpp:

   419	LaundrySoupSequence::LaundrySoupSequence() {
   420	  LaundrySoupSequence("");
   421	}
   422	LaundrySoupSequence::LaundrySoupSequence(std::string expr) {
   423	  std::vector<Token> defaultStack;
   424		std::vector<Token> calculatedTokens;
   425	  defaultStack.push_back(Token("Error", "error", -1));
   426	  if (expr != "") {
   427	    Parser p = Parser(expr);
   428	    p.setForLaundry();
   429	    if (p.inError || !p.tokenStack.size()) {
   430	      calculatedTokens = defaultStack;
   431	      inError = true;
   432	    }
   433	    else {
   434	      calculatedTokens = p.tokenStack;
   435	      inError = false;
   436	    }
   437	  }
   438	  else {
   439	    calculatedTokens = defaultStack;
   440	    inError = false;
   441	  }
   442		LaundrySoupSequence(calculatedTokens);
   443	}
   444	LaundrySoupSequence::LaundrySoupSequence(std::vector<Token> tokens) {
   445		tokenStack = tokens;
   446	  pulseSequence = makePulseSequence(tokenStack);
   447	  workingPulseSequence = duplicateIntVector(pulseSequence);
   448	  numSteps = (int) pulseSequence.size();
   449	  readHead = -1;
   450	}

My header file, dtpulse.hpp properly defines the constructors, and the calculatedTokens variable appears nowhere else in the code. Any help would be greatly appreciated!

Could you also share dtpulse.hpp ?

dtpulse.hpp on GitHub: https://github.com/freddyz/computerscare-vcv-modules/blob/laundrypoly/src/dtpulse.hpp

The way you are calling a constructor from another constructor looks more like c# than c++ to me. Are you sure that syntax is correct? https://stackoverflow.com/questions/308276/can-i-call-a-constructor-from-another-constructor-do-constructor-chaining-in-c

That may exolain line 442 being interpreted as a declaration rather than what I think you mean is “now to do that constructor”

Maybe move all your constructor bodies to functions and just call them. (Or with the noop one use a default argument).

1 Like

The problem is that the constructor for LaundrySoupSequence(std::vector tokens) passes the tokens by value instead of by reference. This requires the compiler to copy the array of tokens, which would require calling the copy constructor. But you don’t have one; the constructors for Token explicitly pass parameters.

You can either change the constructor to pass by reference (e.g., LaundrySoupSequence(const std::vector& tokens) and store a reference to the passed array, or to add a copy constructor to Token.

1 Like

I got it working by creating new functions as you suggested. Thanks a lot for the reply and for the explanation!

1 Like

I was not aware of the “copy constructor” concept until now. Thank you very much for your time and for the clear explanation, I appreciate it!