Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello world. i have a question. i write the below code and i have an error in class line that say me "there isn't any default constructor in class point" but i don't know where in this code call the default constructor . please tell me why i need the default constructor.
thanks .
class point
{
	int x;
	int y;
public:
	point(int _x,int _y):x(_x),y(_y){}
	point& operator=(point&a)
	{
		x=a.x;
		y=a.y;
		return *this;
	}
};
class line
{
	int m;
	point first;
	point last;
	line(point &f,point &l)
	{
		first=f;
		last=l;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jun-14 0:58am    
What's wrong with reading on the topic in any C++ reference or manual?
—SA

In your line interface, you declared the members first and last of type point (side-tip: adopt some naming convention which let's you distinguish more easily between types and variables, e.g. let all user defined types start with a capital). Normally, the compiler will define a default-constructor for your class. However, when you provide a non-default one yourself (like you did in the point class), the compiler will omit its default implementation from the interface! This is why you don't have a default constructor, but why does the compiler require you to define one?

Let's take a look at what your line constructor looks like:

C++
line(point &f,point &l)
{
    first=f;
    last=l;
}


You should know that before the body of the constructor runs, the class-members have already been initialized. This means that the compiler will look for the default constructor of the line members. C++ offers a facility called member initializers, which are handled before the body of your constructor. All members that are not explicitly initialized by the programmer using member initializers (or similar C++11 features) will be default-initialized by the compiler. The syntax for member initializers is like so:

C++
line(point &f,point &l)
:  first(f),
   last(l)
{}


It might even be faster, as they will only be initialized once instead of twice in your code.

Conclusion: if your members don't offer default constructors, you have to provide member initializers with the appropriate arguments.

EDIT: Forgot to include a very useful, still maintained, online resource (by my former C++ teacher): http://www.icce.rug.nl/documents/cplusplus/[^]. Paragraph 7.3 handles this issue :-)
 
Share this answer
 
v6
It's better to explain why do you have a compilation error: you defined a constructor point(int, int), so you have to use it when declaring the variables first and last. As you declaring them without explicit initialization, the compiler assumes that you are using the parameterless constructor. The syntax is defined to assume the default constructor only if you don't define other constructors. If you did not add your constructor with two integer parameter, the compiler would use the default constructor, parameterless one, doing nothing; or you could explicitly define such constructor and initialize x and y.

Practically, using default constructor in this case would be a pretty bad idea, because you don't want leave uninitialized x and y, so you should better initialize first and last with two parameters or add an explicit parameterless constructor (and, then you would apparently have two options of initialization of the instances of the class).

And you could have read on C++ syntax related to constructors and corresponding semantics yourself:
http://en.wikipedia.org/wiki/Default_constructor[^],
http://www.cplusplus.com/doc/tutorial/classes[^] (search for "default constructor" on this page).

—SA
 
Share this answer
 
Comments
sara74 22-Jun-14 1:26am    
so can you tell me what should i do?
i write
pointer first(0,0);
pointer last(10,0);
but it is not true.
please help me.
thanks
Sergey Alexandrovich Kryukov 22-Jun-14 2:07am    
I just told you. Remove the word "pointer". What is it supposed to do?

No, not that. Do a different thing: read some C++ manual (the one I referenced above is good enough) from the very beginning, at least once, then get back to writing code. :-)

—SA
The Default Constructor is used to create an Object of Class, ..
Constructors are used to initialize the private data members of a Class.
A Class can have more then one Constructor BUT it only have one Destructor.

Suppose you have a class:

C++
Class Test{
 private:
  int x;
  int y;
 public:
  Test() // Default constructor
  { }
  Test(int a, int b){ // constructor with parameters
  x = a;
  y = b;
 }
 // . . . functions to traverse the data .. 

}
int main(){
 // Ok now you create object of the class
 Test testObject; // A brand new object  is created, without default constructor you cannot
// create an object like this.
// NOTE: the data members are still un-initialized.
 Test T(2,5); // constructor with parameters, sets the value x = 2 and y = 5
 return 0;
}
 
Share this answer
 
Comments
sara74 23-Jun-14 14:18pm    
thanks very much my good friends
Usman Hunjra 23-Jun-14 16:20pm    
You're welcome!

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