Please help! Problem calling different constructor in constructor

Hi, I don't understand why I can't call the constructor of the class 'line' in a constructor of class 'rectangle' when I declared a line member variable in the class 'rectangle':

class line
{
public:
line(double X0, double Y0, double X1, double Y1);
void rotate_line(double radians);
void translate_line(double xT, double yT);
void print_line();
void rect_output(int condition);
friend class rectangle;
private:
double X0;
double Y0;
double X1;
double Y1;
};

line::line(double X0, double Y0, double X1, double Y1) : X0(X0), Y0(Y0), X1(X1), Y1(Y1)
{/*Body intentionally left empty*/}


class rectangle
{
public:
rectangle(double xR, double yR, double wR, double hR);
void print_rectangle();
private:
line line1;
};

rectangle::rectangle(double xR, double yR, double wR, double hR) //: line1(xR, yR, wR, hR)
{
xR;
yR;
wR = xR + wR;
hR = yR;
line1(xR, yR, wR, hR);
}


when I try to compile it, I keep getting:

error: no match for call to ‘(line) (double&, double&, double&, double&)’

Please help! I've been stuck for hours -_-
Please edit your post real quick and put [code] right before your code, and then [/code] right after it. This will automatically give it syntax highlighting and make it much easier for us to help you.

Also, what line are you getting that error on?
Also, why do you have those two pointless statements in your rectangle::rectangle() definition?
Last edited on
closed account (zb0S216C)
error: no match for call to ‘(line) (double&, double&, double&, double&)’

You should define a default constructor. According to your line class, you only define one constructor that takes arguments which becomes line's default constructor. However, you fail to define a constructor which takes no arguments. So, when an instance of line is created, you're forced to use the constructor that takes arguments. This presents a problem when defining instances of line within another class. The problem is that constructors cannot be called within classes. Without a default constructor for line, you have to use your constructor with arguments which isn't allowed.

A solution to this would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class line
{
    public:
    line(double X0, double Y0, double X1, double Y1);
    line( void ); // void is optional.

    // Members and such here...
};

line::line(double X0, double Y0, double X1, double Y1) : X0(X0), Y0(Y0), X1(X1), Y1(Y1)
{
}

line::line( void ) : X0( 0 ), Y0( 0 ), X1( 0 ), Y1( 0 )
{
}
Last edited on
Your declaration of

"line line1"

in class rectangle calls the default constructor of "line" which you have not defined.
If you want to use anything else than the default constructor then you can declare "line1" it as a pointer,
and initialize it with the correct arguments in the class rectangle.
The code example below works for me. (I also implemented the destructor )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include <iostream>
#include <cstdlib>

using namespace std;

void getData(string &namez, char& ticketType, int& row, int& column);

int main()
{
  //your main routine here if not allready define elsewhere
}

class line
{
public:
line(double X0, double Y0, double X1, double Y1);
void rotate_line(double radians);
void translate_line(double xT, double yT);
void print_line();
void rect_output(int condition);
friend class rectangle;
private:
double X0;
double Y0;
double X1;
double Y1;
};

line::line(double X0, double Y0, double X1, double Y1) : X0(X0), Y0(Y0), X1(X1), Y1(Y1)
{/*Body intentionally left empty*/
  
}


class rectangle
{
public:
  rectangle(double xR, double yR, double wR, double hR);
  ~rectangle();  
  void print_rectangle();
private:
  line *line1;
};

rectangle::rectangle(double xR, double yR, double wR, double hR) : line1(0)
{
  xR;
  yR;
  wR = xR + wR;
  hR = yR;
  line1 = new line(xR, yR, wR, hR );
    // line1(xR, yR, wR, hR);
}


rectangle::~rectangle()
{
  if( line1 !=0) 
   { 
     delete line1 ;
   } 
}
sorry accidentally added the line 9 by mistake when copy and paste, please ignore (and delete).
closed account (zb0S216C)
1
2
3
4
5
6
7
rectangle::~rectangle()
{
  if( line1 !=0) 
   { 
     delete line1 ;
   } 
}


That would help, assuming he/she allocates memory with *line. If *line is NULL, delete won't take effect. However, you failed to consider arrays. What if *line allocated an array of line instances? That destructor would fail to delete all of the memory allocated. Correct me if I'm wrong.

Edit:
Also, you forgot to set line to NULL after deleting it's memory. Some people argue that this is pointless, but I do believe that you should always set a pointer( especially pointers that allocate memory ) to NULL after deleting, even in destructors. Otherwise, it leaves the pointer hanging, allowing access to allocated memory that is no longer there.
Last edited on
line1 is not defined as an array. callig the correct delet vs dlete[] operator is one of the things one have
to keep track of when you program in C++. Im not even sure if its possible to check directly on "line1"
if its a pointer or an array (at least I didnt see many examples)

Your edit
I suppose you mean the destructor, and not the constructor ( I would always set it to zero in the constructor).
I agree that it would not hurt to set line1 to zero after deleting (except that people might think you are a newbie).
Normally not an issue as the destructor is only called when destroying the object.
It might be an issue if you have not overridden the & constructor and the assignment operator which you should
normally do ( albeit not here in this simple example).
closed account (D80DSL3A)
I find that your code compiles with no errors with this change to the rectangle constructor:
 
rectangle::rectangle(double xR, double yR, double wR, double hR) : line1(xR, yR, wR, hR) {}

No problem calling line constructor. No need for a no-arg "default" constructor in the line class.
Why had you commented out the : line1(xR, yR, wR, hR) part?

BTW: The statements within your given constructor don't really do anything.
1
2
3
4
  xR;
  yR;
  wR = xR + wR;
  hR = yR;

What are you trying to do here?

EDIT: Here's a simplified example which works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A
{
	int x;
	int y;
public:
	A(int X, int Y): x(X), y(Y){}	
};

class B
{
	float fz;
	A a;
public:
	B(float Z, int f, int g): fz(Z), a(f,g){}	
};


PS: I'm using VS2008 Express. Perhaps a compiler specific problem is involved.
Last edited on
Sorry I didn't highlight my code, this is the first time in my life posting for help on any kind of forum since I never thought that people actually respond (boy I was wrong!). Thanks for all your help! It turns out that I needed to declare line1 as a pointer so I can use the constructor that's not the default constructor. Thanks everyone!
Topic archived. No new replies allowed.