Help a beginner

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PhilB

    Help a beginner

    Hello experts,
    I am a complete beginner in C++ (although I know C).
    I am trying to compile the code below, and I get
    the following error.
    Can anyone explain to me my mistake?
    Thanks!
    PhilB

    myprog2.cpp: In method `Line::Line (Point, Point)':
    myprog2.cpp:32: no matching function for call to `Point::Point ()'
    myprog2.cpp:12: candidates are: Point::Point (int, int)
    myprog2.cpp:9: Point::Point (const Point &)
    myprog2.cpp:32: no matching function for call to `Point::Point ()'
    myprog2.cpp:12: candidates are: Point::Point (int, int)
    myprog2.cpp:9: Point::Point (const Point &)
    myprog2.cpp:32: no matching function for call to `Point::Point ()'
    myprog2.cpp:12: candidates are: Point::Point (int, int)
    myprog2.cpp:9: Point::Point (const Point &)

    //------------------------
    class Point
    {
    protected:
    int x;
    int y;
    public:
    Point::Point(in t, int);
    };

    Point::Point(in t initx, int inity)
    {
    x=initx;
    y=inity;
    return;
    }

    class Line:public Point
    {
    public:
    Point start_point;
    Point end_point;

    public:
    Line::Line(Poin t, Point);
    };

    Line::Line(Poin t initp1, Point initp2)
    {
    start_point=ini tp1;
    end_point=initp 2;
    return;
    }

    main()
    {
    Point p1(10,20);
    Point p2(20,40);
    Line l1(p1,p2);
    }
    //------------------------
  • Marko Becirevic

    #2
    Re: Help a beginner

    > //------------------------[color=blue]
    > class Point
    > {
    > protected:
    > int x;
    > int y;
    > public:
    > Point::Point(in t, int);
    > };
    >[/color]

    not Point::Point(in t, int);
    but just Point(int, int);

    same in Line class.



    Comment

    • lallous

      #3
      Re: Help a beginner

      "PhilB" <pbruyant@yahoo .com> wrote in message
      news:10b172d6.0 312160551.18b02 94d@posting.goo gle.com...[color=blue]
      > Hello experts,
      > I am a complete beginner in C++ (although I know C).
      > I am trying to compile the code below, and I get
      > the following error.
      > Can anyone explain to me my mistake?
      > Thanks!
      > PhilB
      >
      > myprog2.cpp: In method `Line::Line (Point, Point)':
      > myprog2.cpp:32: no matching function for call to `Point::Point ()'
      > myprog2.cpp:12: candidates are: Point::Point (int, int)
      > myprog2.cpp:9: Point::Point (const Point &)
      > myprog2.cpp:32: no matching function for call to `Point::Point ()'
      > myprog2.cpp:12: candidates are: Point::Point (int, int)
      > myprog2.cpp:9: Point::Point (const Point &)
      > myprog2.cpp:32: no matching function for call to `Point::Point ()'
      > myprog2.cpp:12: candidates are: Point::Point (int, int)
      > myprog2.cpp:9: Point::Point (const Point &)
      >
      > //------------------------
      > class Point
      > {
      > protected:
      > int x;
      > int y;
      > public:
      > Point::Point(in t, int);
      > };
      >
      > Point::Point(in t initx, int inity)
      > {
      > x=initx;
      > y=inity;
      > return;
      > }
      >
      > class Line:public Point
      > {
      > public:
      > Point start_point;
      > Point end_point;
      >
      > public:
      > Line::Line(Poin t, Point);
      > };
      >
      > Line::Line(Poin t initp1, Point initp2)
      > {
      > start_point=ini tp1;
      > end_point=initp 2;
      > return;
      > }
      >
      > main()
      > {
      > Point p1(10,20);
      > Point p2(20,40);
      > Line l1(p1,p2);
      > }
      > //------------------------[/color]
      Hello,

      In the class as:
      [color=blue]
      > class Point
      > {
      > protected:
      > int x;
      > int y;
      > public:
      > Point::Point(in t, int);[/color]
      ^^^^remove the "Point::"[color=blue]
      > };[/color]

      Same for the other class.

      --
      Elias


      Comment

      • Rolf Magnus

        #4
        Re: Help a beginner

        PhilB wrote:
        [color=blue]
        > Hello experts,
        > I am a complete beginner in C++ (although I know C).
        > I am trying to compile the code below, and I get
        > the following error.
        > Can anyone explain to me my mistake?[/color]

        Isn't it obvious from the error messages?
        [color=blue]
        > Thanks!
        > PhilB
        >
        > myprog2.cpp: In method `Line::Line (Point, Point)':
        > myprog2.cpp:32: no matching function for call to `Point::Point ()'[/color]

        You try to use the default constructor (i.e. `Point::Point ()'), but
        your Point class doesn't have one.
        [color=blue]
        > myprog2.cpp:12: candidates are: Point::Point (int, int)
        > myprog2.cpp:9: Point::Point (const Point &)
        > myprog2.cpp:32: no matching function for call to `Point::Point ()'
        > myprog2.cpp:12: candidates are: Point::Point (int, int)
        > myprog2.cpp:9: Point::Point (const Point &)
        > myprog2.cpp:32: no matching function for call to `Point::Point ()'
        > myprog2.cpp:12: candidates are: Point::Point (int, int)
        > myprog2.cpp:9: Point::Point (const Point &)
        >
        > //------------------------
        > class Point
        > {
        > protected:
        > int x;
        > int y;
        > public:
        > Point::Point(in t, int);
        > };
        >
        > Point::Point(in t initx, int inity)
        > {
        > x=initx;
        > y=inity;
        > return;
        > }
        >
        > class Line:public Point
        > {
        > public:
        > Point start_point;
        > Point end_point;
        >
        > public:
        > Line::Line(Poin t, Point);
        > };
        >
        > Line::Line(Poin t initp1, Point initp2)
        > {
        > start_point=ini tp1;
        > end_point=initp 2;
        > return;
        > }[/color]

        Since you have not specified an initializer for your Point objects, the
        default constructor will be used to create them, but you don't have
        one. Try this instead:

        Line::Line(Poin t initp1, Point initp2)
        : start_point(ini tp1),
        end_point(initp 2)
        {
        }

        This will use the copy constructor (which is generated by the compiler
        automatically) to create your two Point members.
        [color=blue]
        >
        > main()
        > {
        > Point p1(10,20);
        > Point p2(20,40);
        > Line l1(p1,p2);
        > }
        > //------------------------[/color]

        Comment

        • Gary Labowitz

          #5
          Re: Help a beginner

          "PhilB" <pbruyant@yahoo .com> wrote in message
          news:10b172d6.0 312160551.18b02 94d@posting.goo gle.com...[color=blue]
          > Hello experts,
          > I am a complete beginner in C++ (although I know C).
          > I am trying to compile the code below, and I get
          > the following error.
          > Can anyone explain to me my mistake?
          > Thanks!
          > PhilB
          >
          > myprog2.cpp: In method `Line::Line (Point, Point)':
          > myprog2.cpp:32: no matching function for call to `Point::Point ()'
          > myprog2.cpp:12: candidates are: Point::Point (int, int)
          > myprog2.cpp:9: Point::Point (const Point &)
          > myprog2.cpp:32: no matching function for call to `Point::Point ()'
          > myprog2.cpp:12: candidates are: Point::Point (int, int)
          > myprog2.cpp:9: Point::Point (const Point &)
          > myprog2.cpp:32: no matching function for call to `Point::Point ()'
          > myprog2.cpp:12: candidates are: Point::Point (int, int)
          > myprog2.cpp:9: Point::Point (const Point &)
          >
          > //------------------------
          > class Point
          > {
          > protected:
          > int x;
          > int y;
          > public:
          > Point::Point(in t, int);
          > };
          >
          > Point::Point(in t initx, int inity)
          > {
          > x=initx;
          > y=inity;
          > return;
          > }
          >
          > class Line:public Point
          > {
          > public:
          > Point start_point;
          > Point end_point;
          >
          > public:
          > Line::Line(Poin t, Point);
          > };
          >
          > Line::Line(Poin t initp1, Point initp2)
          > {
          > start_point=ini tp1;
          > end_point=initp 2;
          > return;
          > }
          >
          > main()
          > {
          > Point p1(10,20);
          > Point p2(20,40);
          > Line l1(p1,p2);
          > }
          > //------------------------[/color]

          Please note it one of my top ten that a number of you objected to having on
          the list.

          To poster: Your Point constructor is protected, which means it can only be
          called by code of the Point class or one of its subclasses (Line). You are
          calling it from main.
          When you create a Line object, C++ will automatically construct the Point
          inherited portion by calling a constructor for a Point using a default
          constructor. And there isn't one.
          In addition, I'd say your design is flawed: A Line is not a kind of Point so
          you should not be using an is-a design. Lines have Points which is a has-a
          design. This is one way it could look:

          #include <iostream>

          using namespace std;

          class Linex;
          class Pointx
          {
          friend class Linex;
          int x, y; //coordinates
          public:
          Pointx (int x, int y):x(x), y(y){};
          Pointx ( ){}; //This is the missing constructor
          };

          class Linex
          {
          Pointx startPoint, endPoint; //points for line
          public:
          void show( );
          Linex (Pointx start, Pointx end)
          {
          startPoint = start;
          endPoint = end;
          }

          };

          void Linex::show( )
          {
          cout << "Start=(" << startPoint.x << ", " << startPoint.y
          << ") : End=(" << endPoint.x << ", " << endPoint.y
          << ")" << endl;
          }

          int main( )
          {
          Pointx point1(10,20);
          Pointx point2(20,30);
          Linex myLine(point1, point2);
          cout << "Line is ";
          myLine.show( );
          cout << endl;

          return 0;
          }

          Note: I made the entire class Linex a friend because my compiler is
          objecting to making single function a friend. Don't know why.
          MingW32: Is this a known bug?
          --
          Gary


          Comment

          • Kevin Saff

            #6
            Re: Help a beginner


            "PhilB" <pbruyant@yahoo .com> wrote in message
            news:10b172d6.0 312160551.18b02 94d@posting.goo gle.com...[color=blue]
            > Hello experts,
            > I am a complete beginner in C++ (although I know C).
            > I am trying to compile the code below, and I get
            > the following error.
            > Can anyone explain to me my mistake?
            > Thanks!
            > PhilB
            >
            > myprog2.cpp: In method `Line::Line (Point, Point)':
            > myprog2.cpp:32: no matching function for call to `Point::Point ()'[/color]

            The compiler is trying to tell you there is no default constructor for
            Point.
            [color=blue]
            > myprog2.cpp:12: candidates are: Point::Point (int, int)[/color]

            The compiler says it only sees this constructor, to create a point from two
            ints.
            [color=blue]
            >
            > [snip]
            > class Point
            > {
            > protected:
            > int x;
            > int y;[/color]

            Protected data is usually a mistake; it tends to introduce confusing
            dependencies between a base and its derived classes.

            Also, if you intend to use Point as a base class, you should declare a
            virtual destructor, (virtual ~Point()) or else derived objects may not be
            properly destroyed.
            [color=blue]
            > public:
            > Point::Point(in t, int);[/color]

            (Note: the "Point::" is unnecessary inside the class definition, it is only
            needed when you define the function outside of the class definition.)

            This declares a constructor from two ints, which hides the
            compiler-generated default constructor. If you want the default constructor
            as well, you would need to define one:

            Point () {}

            This is not really necessary though, there's a better solution to your
            immediate problem.
            [color=blue]
            > };
            >
            > Point::Point(in t initx, int inity)
            > {
            > x=initx;
            > y=inity;
            > return;
            > }[/color]

            You don't need to explicitly return here. This function would usually be
            written (for reasons explained later):

            Point (int initx, int inity) : x (initx), y (inity) {}

            This uses initialization syntax to assign the values of x and y. Your
            syntax instead, will first create the two ints and then assign new values
            for them. This isn't too big a deal for ints, but see below.

            (Also, many C++ coders prefer to mark member data in some way to
            differentiate between member data and function arguments. Popular ways
            include m_x ("m_" for member) and x_.)
            [color=blue]
            >
            > class Line:public Point
            > {
            > public:
            > Point start_point;
            > Point end_point;[/color]

            As someone trying to break old C habits it will probably be better for you
            to only use private data until you learn the exceptions to this rule.
            Instead, make functions that act on Lines and Points members of those
            classes.
            [color=blue]
            >
            > public:
            > Line::Line(Poin t, Point);
            > };
            >
            > Line::Line(Poin t initp1, Point initp2)
            > {
            > start_point=ini tp1;
            > end_point=initp 2;
            > return;
            > }[/color]

            OK, here's the compiler's problem; not just a style one :). This tells the
            compiler to first default construct two points, and then assign the new
            point values to them. However, the Point's constructor has hidden the
            default constructor, so this won't work. Instead, you should write:

            Line (Point initp1, Point initp2) : start_point (initp1), end_point
            (initp2) {}

            This will use the compiler-generated Point copy constructor (which isn't
            hidden by the user-declared constructor), instead. In general it is good
            practice to use initialization syntax where possible, since
            1) The compiler may be able to optimize it better.
            2) It's a familiar idiom to C++ coders.
            3) It removes the necessity of defining default constructors for objects.

            As you get more used to OO programming, you will find that default objects
            do not make sense for all kinds of classes, and often require
            special-purpose code to handle the default-constructed case. I think
            "Point" is a class where a default constructor may or may not be
            appropriate, depending on your other requirements.

            HTH
            --
            KCS




            Comment

            • Vardhan Prabhakar N [C]

              #7
              Re: Help a beginner



              PhilB wrote:[color=blue]
              > Hello experts,
              > I am a complete beginner in C++ (although I know C).
              > I am trying to compile the code below, and I get
              > the following error.
              > Can anyone explain to me my mistake?
              > Thanks!
              > PhilB
              >
              > myprog2.cpp: In method `Line::Line (Point, Point)':
              > myprog2.cpp:32: no matching function for call to `Point::Point ()'
              > myprog2.cpp:12: candidates are: Point::Point (int, int)
              > myprog2.cpp:9: Point::Point (const Point &)
              > myprog2.cpp:32: no matching function for call to `Point::Point ()'
              > myprog2.cpp:12: candidates are: Point::Point (int, int)
              > myprog2.cpp:9: Point::Point (const Point &)
              > myprog2.cpp:32: no matching function for call to `Point::Point ()'
              > myprog2.cpp:12: candidates are: Point::Point (int, int)
              > myprog2.cpp:9: Point::Point (const Point &)
              >[/color]
              You dont have a default constructor, so convert the existing one to one.

              [color=blue]
              > //------------------------
              > class Point
              > {
              > protected:
              > int x;
              > int y;
              > public:
              > Point::Point(in t, int);
              > };
              >
              > Point::Point(in t initx, int inity)[/color]
              make this
              Point::Point(in t initx=0, int inity=0)[color=blue]
              > {
              > x=initx;
              > y=inity;
              > return;
              > }
              >
              > class Line:public Point
              > {
              > public:
              > Point start_point;
              > Point end_point;
              >
              > public:
              > Line::Line(Poin t, Point);
              > };
              >
              > Line::Line(Poin t initp1, Point initp2)
              > {
              > start_point=ini tp1;
              > end_point=initp 2;
              > return;
              > }
              >
              > main()
              > {
              > Point p1(10,20);
              > Point p2(20,40);
              > Line l1(p1,p2);
              > }
              > //------------------------[/color]


              --
              regards,
              Vardhan
              --

              Comment

              • jeffc

                #8
                Re: Help a beginner


                "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                news:brn58a$ojr $06$1@news.t-online.com...[color=blue]
                > PhilB wrote:
                >[color=green]
                > > Hello experts,
                > > I am a complete beginner in C++ (although I know C).
                > > I am trying to compile the code below, and I get
                > > the following error.
                > > Can anyone explain to me my mistake?[/color]
                >
                > Isn't it obvious from the error messages?[/color]

                The only thing that's obvious is that it isn't.


                Comment

                • jeffc

                  #9
                  Re: Help a beginner


                  "lallous" <lallous@lgwm.o rg> wrote in message
                  news:brn59p$54m lg$1@ID-161723.news.uni-berlin.de...[color=blue]
                  > In the class as:
                  >[color=green]
                  > > class Point
                  > > {
                  > > protected:
                  > > int x;
                  > > int y;
                  > > public:
                  > > Point::Point(in t, int);[/color]
                  > ^^^^remove the "Point::"[color=green]
                  > > };[/color]
                  >
                  > Same for the other class.[/color]


                  Actually, I believe that's fine. Anyway, that's not the main problem. The
                  main problem is that he's trying to create Points with the default
                  constructor, and there's no default constructor.


                  Comment

                  • jeffc

                    #10
                    Re: Help a beginner


                    "Vardhan Prabhakar N [C]" <a3381c@motorol a.com> wrote in message
                    news:3FDF1DDB.1 040405@motorola .com...[color=blue][color=green]
                    > > //------------------------
                    > > class Point
                    > > {
                    > > protected:
                    > > int x;
                    > > int y;
                    > > public:
                    > > Point::Point(in t, int);
                    > > };
                    > >
                    > > Point::Point(in t initx, int inity)[/color]
                    > make this
                    > Point::Point(in t initx=0, int inity=0)[/color]

                    Not really a good explanation. That very well might not be what he wants.


                    Comment

                    • jeffc

                      #11
                      Re: Help a beginner


                      "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
                      news:K9mdnVgIj9 InvUKiRVn-sQ@comcast.com. ..[color=blue]
                      >
                      > Please note it one of my top ten that a number of you objected to having[/color]
                      on[color=blue]
                      > the list.
                      >
                      > To poster: Your Point constructor is protected, which means it can only be
                      > called by code of the Point class or one of its subclasses (Line).[/color]

                      No, you missed the public.


                      Comment

                      • jeffc

                        #12
                        Re: Help a beginner


                        "Marko Becirevic" <marija.SPAM_be cirevic@zg.tel. hr> wrote in message
                        news:brn54t$jlv $1@ls219.htnet. hr...[color=blue][color=green]
                        > > //------------------------
                        > > class Point
                        > > {
                        > > protected:
                        > > int x;
                        > > int y;
                        > > public:
                        > > Point::Point(in t, int);
                        > > };
                        > >[/color]
                        >
                        > not Point::Point(in t, int);
                        > but just Point(int, int);
                        >
                        > same in Line class.[/color]

                        That is not the problem. The problem is that he has Points in his Line.
                        There is no way to construct those Points when he goes to create a Line in
                        his main program.


                        Comment

                        • PhilB

                          #13
                          Re: Help a beginner

                          "Gary Labowitz" <glabowitz@comc ast.net> wrote in message news:<K9mdnVgIj 9InvUKiRVn-sQ@comcast.com> ...[color=blue]
                          > Please note it one of my top ten that a number of you objected to having on
                          > the list.
                          >
                          > To poster: Your Point constructor is protected, which means it can only be
                          > called by code of the Point class or one of its subclasses (Line). You are
                          > calling it from main.
                          > When you create a Line object, C++ will automatically construct the Point
                          > inherited portion by calling a constructor for a Point using a default
                          > constructor. And there isn't one.
                          > In addition, I'd say your design is flawed: A Line is not a kind of Point so
                          > you should not be using an is-a design. Lines have Points which is a has-a
                          > design. This is one way it could look:
                          >
                          > #include <iostream>
                          >
                          > using namespace std;
                          >
                          > class Linex;
                          > class Pointx
                          > {
                          > friend class Linex;
                          > int x, y; //coordinates
                          > public:
                          > Pointx (int x, int y):x(x), y(y){};
                          > Pointx ( ){}; //This is the missing constructor[/color]

                          Hello Gary
                          Thank you for your reply; I just added the line
                          Pointx ( ){};
                          and it compiles with no error!
                          Thanks to all for sharing your expertise.
                          PhilB

                          Comment

                          • Steven Green

                            #14
                            Re: Help a beginner

                            On Tue, 16 Dec 2003 05:51:02 -0800, PhilB wrote:
                            [color=blue]
                            > Hello experts,
                            > I am a complete beginner in C++ (although I know C).
                            > I am trying to compile the code below, and I get
                            > the following error.
                            > Can anyone explain to me my mistake?
                            > Thanks!
                            > PhilB
                            >
                            > myprog2.cpp: In method `Line::Line (Point, Point)':
                            > myprog2.cpp:32: no matching function for call to `Point::Point ()'
                            > myprog2.cpp:12: candidates are: Point::Point (int, int)
                            > myprog2.cpp:9: Point::Point (const Point &)
                            > myprog2.cpp:32: no matching function for call to `Point::Point ()'
                            > myprog2.cpp:12: candidates are: Point::Point (int, int)
                            > myprog2.cpp:9: Point::Point (const Point &)
                            > myprog2.cpp:32: no matching function for call to `Point::Point ()'
                            > myprog2.cpp:12: candidates are: Point::Point (int, int)
                            > myprog2.cpp:9: Point::Point (const Point &)
                            >
                            > //------------------------
                            > class Point
                            > {
                            > protected:
                            > int x;
                            > int y;
                            > public:
                            > Point::Point(in t, int);
                            > };
                            >
                            > Point::Point(in t initx, int inity)
                            > {
                            > x=initx;
                            > y=inity;
                            > return;
                            > }
                            >
                            > class Line:public Point
                            > {
                            > public:
                            > Point start_point;
                            > Point end_point;
                            >
                            > public:
                            > Line::Line(Poin t, Point);
                            > };
                            >
                            > Line::Line(Poin t initp1, Point initp2)
                            > {
                            > start_point=ini tp1;
                            > end_point=initp 2;
                            > return;
                            > }
                            >
                            > main()
                            > {
                            > Point p1(10,20);
                            > Point p2(20,40);
                            > Line l1(p1,p2);
                            > }
                            > //------------------------[/color]

                            The answer has been covered over and over, here so I will only include
                            my alternate implementation that doesn't require definition of the
                            default constructor

                            class Point
                            {
                            protected:
                            int x;
                            int y;
                            public:
                            Point(int, int);
                            };

                            Point::Point(in t initx, int inity)
                            {
                            x=initx;
                            y=inity;
                            return;
                            }

                            class Line
                            {
                            public:
                            Point* start_point;
                            Point* end_point;
                            public:
                            Line(Point*, Point*);
                            };

                            Line::Line(Poin t* initp1, Point* initp2)
                            {
                            start_point=ini tp1;
                            end_point=initp 2;
                            return;
                            }

                            main()
                            {
                            Point p1(10,20);
                            Point p2(20,40);
                            Line l1(&p1,&p2);
                            }

                            I removed the line is-a point relationship which makes little sence.
                            Additionally, I used pointers to avoid the calling of the default
                            constructor.

                            --Steve

                            Comment

                            • PhilB

                              #15
                              Re: Help a beginner

                              Steven Green <steven.green30 @verizon.net> wrote in message news:<pan.2003. 12.20.00.41.50. 909223@verizon. net>...[color=blue]
                              > The answer has been covered over and over, here so I will only include
                              > my alternate implementation that doesn't require definition of the
                              > default constructor
                              >
                              > class Point
                              > {
                              > protected:
                              > int x;
                              > int y;
                              > public:
                              > Point(int, int);
                              > };
                              >
                              > Point::Point(in t initx, int inity)
                              > {
                              > x=initx;
                              > y=inity;
                              > return;
                              > }
                              >
                              > class Line
                              > {
                              > public:
                              > Point* start_point;
                              > Point* end_point;
                              > public:
                              > Line(Point*, Point*);
                              > };
                              >
                              > Line::Line(Poin t* initp1, Point* initp2)
                              > {
                              > start_point=ini tp1;
                              > end_point=initp 2;
                              > return;
                              > }
                              >
                              > main()
                              > {
                              > Point p1(10,20);
                              > Point p2(20,40);
                              > Line l1(&p1,&p2);
                              > }
                              >
                              > I removed the line is-a point relationship which makes little sence.
                              > Additionally, I used pointers to avoid the calling of the default
                              > constructor.
                              >
                              > --Steve[/color]
                              Hi Steve,
                              Thanks a lot for your crystal-clear example,
                              and thanks for sharing your expertise.
                              PhilB

                              Comment

                              Working...