Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi Can any one help me to solve this Problem

C#
class sample
{
   int a,b;
public:
   sample(int x, int y)
   {
      a = x;
      b = y;
      cout<<"a="<<a<<endl<<"b="<<b<<endl;
   }
};

void main()
{
   sample obj1;
}



In the Above Program the Error it is showing is "No Appropriate Default Constructor is provided"

But The Compiler internally provides One Default Constructor know.
Why doesn't the compiler use the Compiler provided Default Constructor?

thanks
sarath
Posted
Updated 16-Feb-11 16:21pm
v2

You're almost right, it's easy to get confused. You misunderstood the syntax rules only slightly.

The C++ rule about default constructor does not say:
"If parameter-less constructor is not provided, default constructor is assumed".
Instead, it says:
"If you provide a constructor that takes a non-void parameter, and you want to allow your class to be created with no parameters, you must also provide a default constructor. The default constructor can be a constructor with default values for all parameters."
Do you see the subtle difference?

So, assuming you want to have the same members, you need to change one of the following, or both:

1) Add parameter-less constructor, something like this:
C++
class sample
{
   int a,b;
public:
   sample() : a(0), b(0) {}
   //...
}


2) Use your available constructor with two parameters:
C++
sample obj1(x, y);


—SA
 
Share this answer
 
v2
Comments
Abhinav S 16-Feb-11 23:08pm    
Good answer. 5.
Sergey Alexandrovich Kryukov 16-Feb-11 23:27pm    
Thank you.
--SA
ShilpiP 17-Feb-11 0:37am    
Well described... :) +5
Sergey Alexandrovich Kryukov 17-Feb-11 2:05am    
Thank you, Shilpi.
--SA
Espen Harlinn 17-Feb-11 9:34am    
Nice one, my 5
You have provided your own constructor which overrides the default constructor. And when you are creating a object, you are not passing any arguments which are required by your constructor.

Try changing your line


sample obj1;

to
int x = "some value you want in x";
int y = "some value you want in y";
sample obj1(x, y);


Replace some values you want in x/y to something meaningful.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Feb-11 22:31pm    
It does not "override" it (C++ constructors are not virtual functions).
My 4.
--SA
Emilio Garavaglia 17-Feb-11 8:59am    
hummmm ... "overriding" is not necessarily something related to virtual functions: I "override" something (A) with something else (B) every time B "masks" the usage of A.
The "scope" where this happens, for virtual function, is the (runtime) lifetime of an object, for non virtual function is the name scope of a class (that include all derived's), for a template definition are all the not-elsewhere specialized instances.

They are completely different semantics, but always "over-ride" is.

Sergey Alexandrovich Kryukov 17-Feb-11 20:13pm    
Sorry, in strict C++ terms it is, will you look at the text book.
A common misconception is "override happens in run-time". No, it's statically defined at compile time: VTable is defined for a class. It's run-time (not defined at the moment of compilation) what function defined in base class will be called, due to late binding, but subtle difference is: this is about a call, not about override.
The novice need to feel the difference in the situation when base-class name is hidden. It looks like "override" because new method is called is derived class is explicitly used, but late binding is lost. Also modifies "new" needs explanation.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900