问题
I have a constructor for a class that takes in a bool, a pointer to an array, and string.
TheConstructor(bool theBool=true, int *theArray=0, std::string message="0");
Is this the correct way to write it in the header file? My program isn't compiling right now because of a "undefined reference to "the constructor" and to other member functions".
What could be causing this also? I checked and in main.cpp I #included "Class.h" and defined every memberwise function that needed to be defined that was stated in "Class.h" I wrote in "Class.cpp"
回答1:
I hope you didn't name your class TheConstructor :) If you have class C you can declare its constructor almost as you did - you forgot to put the name of the bool argument:
C.h:
#include <string>
class C
{
public:
C(bool b = 0, int *theArray = 0, std::string message = "0");
};
C.cpp:
#include "C.h"
C::C(bool b, int *theArray, std::string message)
{
}
回答2:
The first parameter's name is not specified, but that probably wouldn't cause the error you're encountering:
TheConstructor(bool=0, int *theArray=0, std::string message="0");
You probably want to do something like this:
TheConstructor(bool flag=0, int *theArray=0, std::string message="0");
However, without seeing the definition, there is not much else I can say about it. It's difficult to predict what else could be wrong by simply looking at the declaration.
来源:https://stackoverflow.com/questions/10386144/constructor-parameter-issue-c