calling class elements inside a class

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
#include <iostream>

using namespace std;
class Point
{ 
      private:
              int x;
              int y;
      public:
             Point(int x1,int y1)
             {
                       x = x1;
                       y = y1;
             }
};
class Triangle
{
      private:
      Point P[3];
      public:
      Triangle();          
};
Triangle::Triangle()
{
                    
                 P[0](0,0);
                 P[1](0,0);
                 P[2](0,0); 
}
int main()
{
    system("PAUSE");
    return 0;
}


After calling class Point inside class Triangle I get this error.

no matching function for call to Point::Point().

Does any one know if there is anything wrong with my code. Please help.


-Sumair
You can't call a class. You can create instances of a class and call its member functions.

Since you have not specified which constructor to be used to initialize the objects in P it will try to use the default constructor (constructor that takes no arguments). Point doesn't have a default constructor so that's what the error message "no matching function for call to Point::Point()" is about.

Either create a default constructor or specify the constructor in the constructor initialization list.
1
2
3
4
Triangle::Triangle()
:	P{{0,0},{0,0},{0,0}}
{
}


To make line 26-28 compile you would have to define operator(int, int) as a member of Point.
Last edited on
Can you write the code of defining the operator(int,int). I don't understand why we need this.
You don't need it. I was just saying that if you wanted that exact syntax to compile you would have to.
After I added the default point constructor I get a new error;

"no match for call to (Point)(int,int)"
This code

1
2
3
4
5
6
7
Triangle::Triangle()
{
                    
                 P[0](0,0);
                 P[1](0,0);
                 P[2](0,0); 
}


is invalid. Expression for example, P[0](0,0); means calling of the function call operator that was not defined in class Point.
I learned in my C++ course that I can define a constructor like so
1
2
3
4
5
Point(int x1,int y1)
             {
                       x = x1;
                       y = y1;
             }


and then I can create an object like so inside main function:

 
Point P(0,0);


Is this not the correct method of using constructor? because that what I am doing in the Triangle constructor. Initializing the Point in the Triangle constructor.
Topic archived. No new replies allowed.