Solved

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

Posted on 2010-11-22
11
1,157 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:graber
11 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 34192244
Can you show the CTextSignLine in its entirety please? don't assume that code you don't think isn't important isn't. For example, does that class have any constant or reference members? If so is the default constructor correctly initialising them? You may get this error if that isn't the case. What about CObject, does that have a default constructor too?

As an aside, the way you initialise your members in the constructor is inefficient. Use an initialiser list.
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
CTextSignLine::CTextSignLine():
   m_pSignParams(0),
   m_hWndParent(0),
   m_pBrSignBackground(0) 
{
}

Open in new window

0
 
LVL 2

Assisted Solution

by:graber
graber earned 0 total points
ID: 34192539
Thanks Evilrix.  I concur with your comment... at this point were trying to port some very old visual studio code to Visual Studio 2008.

I found the problem.  I had a forward declaration in the header but no corresponding header (TextSignLine.h) in the cpp (TextSign.cpp).  I was expecting to see an "unknown variable" but this wasn't going to happen because of the forward declaration.

I must be getting tired... Sorry about that.
//TextSignLine.h
...
class CTextSignLine : public CObject
{
   public:
      CTextSignLine();
};

//TextSignLine.cpp
CTextSignLine::CTextSignLine()
{
   m_pSignParams = NULL;
   m_hWndParent = NULL;
   m_pBrSignBackground = NULL;
}

//TextSign.h
class CSignFontMgr;
class CTextSignParams;
class CTextSignLine;

class CTextSign : public CObject
{
   friend class CTextSignPreview;
   private:
      ...
   public:
      CTextSign();
      ...
}
//TextSign.cpp
#include "TextSignLine.h" //<---- here was the culprit
CTextSign::CTextSign(...)
{
   ...
   m_SignLineArray = new CTextSignLine[m_pSignParams->m_nNumLines];
   for (int i = 0; i < m_pSignParams->m_nNumLines; ++i)
   {
      ...
   }
   ...
}

Open in new window

0
 
LVL 2

Expert Comment

by:calin131
ID: 34193163
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
}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 34193669
>>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)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34194446
>> I must be getting tired... Sorry about that.

No worries. Pleased you resolved the issue.
0
 
LVL 2

Author Comment

by:graber
ID: 34196049
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
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34196151
>> 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.
0
 
LVL 2

Author Comment

by:graber
ID: 34196200
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.
 
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34213648
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.

http://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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
Embarcadero C++ Builder XE2 TDateTime 8 75
Convert money to nchar 4 69
How to determine if the result of an equation is an integer in C++? 3 19
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question