Link to home
Start Free TrialLog in
Avatar of graber
graberFlag for United States of America

asked on

C2512 "no appropriate default constructor available for a class with constructor taking no parameters

error C2515 'CTextSignLine' : no appropriate default constructor available is occuring when m_SignLineArray = new CTextSignLine[m_pSignParams->m_nNumLines];

There is a default constructor that takes no parameters and everyone that has had this problem for gets to add a default constructor... So what ridiculous mistake am I making.
<(")
//TextSignLine.h
class CTextSignLine : public CObject
{
   public:
      CTextSignLine();
      ~CTextSignLine();
   ...
};
//TextSignLine.cpp
CTextSignLine::CTextSignLine()
{
   m_pSignParams = NULL;
   m_hWndParent = NULL;
   m_pBrSignBackground = NULL;
}
//TextSign.h
class CTextSign : public CObject
{
   private:
      CTextSignLine *m_pSignLineArray;
      ...
   public:
      CTextSign();
      ...
};
//TextSign.cpp
CTextSign::CTextSign(...)
{
   ...
   m_SignLineArray = new CTextSignLine[m_pSignParams->m_nNumLines];
   ...
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You basically need a default constructor that gets as parameters the number of lines.
Something like:
CTextSignLine:: CTextSignLine( int numberOfLines )
{// set numberOfLines to your class attribute and then call your default
 // constructor without parameters or do what ever you need to initialize your object
}
>>You basically need a default constructor that gets as parameters the
>>number of lines.

You are aware that 'default constructor' and 'parameter' are mutually exclusive? ;o)
>> I must be getting tired... Sorry about that.

No worries. Pleased you resolved the issue.
Avatar of graber

ASKER

jkr
>> You are aware that 'default constructor' and 'parameter' are mutually exclusive? ;o)

As I understand it the default constructor has no parameters.  But this old dog could very be missing something important.  What do you have?

As the code sits there wasn't anything wrong with the constructor.  The forward declaration of CTextSignLine meant that the class instantiating it knew what it was but now how to 'Construct' it, ergo 'No appropriate constructor could be found.  Once I add the header for CTextSignLine to the cpp that was doing the constructing it was able to find an appropriate constructor.  Evil did I nail that.  

I've been doing this for some time and it still amazes me what I have yet to learn.  Thanks guys for all the support.  The turn around on this was great.
Gregg
>> As I understand it the default constructor has no parameters

Strictly speaking a default constructor is one that doesn't require arguments but it can have parameters as long as there are default values that mean the arguments are optional.

Both of these are default constructors and a class would only be able to have one of the other.

myclass(){}

myclass(bool b = false){}

>> Evil did I nail that.  

As I said before, there can be a number of reasons for this error and they are not all obvious without being able to see all the code. The lack of a constructor definition for a class that has been forward declared being one possible case.
Avatar of graber

ASKER

Yep that one evil that one escaped me....  
Thanks Evil ... I wish I could have shown you more of the source but I'm a contractor and even more sensitive to showing the customers stuff.
 
Since I've given you a good explanation as to what was generating this error I'm not sure I see why you would close it with only your own post as the solution. A more appropriate way to close this would be a split and possibly a B grade since my advice assisted you in resolving the issue rather than actually resolving it for you.

See the following link or the grading guidelines and how they should be applied.

https://www.experts-exchange.com/help.jsp#hs=29&hi=403
"If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B."

If you disagree that's fine but I'd appreciate your thoughts on why.