473,847 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
}
//------------------------
Jul 22 '05 #1
15 1941
> //------------------------
class Point
{
protected:
int x;
int y;
public:
Point::Point(in t, int);
};


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

same in Line class.

Jul 22 '05 #2
"PhilB" <pb******@yahoo .com> wrote in message
news:10******** *************** ***@posting.goo gle.com...
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);
}
//------------------------ Hello,

In the class as:
class Point
{
protected:
int x;
int y;
public:
Point::Point(in t, int); ^^^^remove the "Point::" };


Same for the other class.

--
Elias
Jul 22 '05 #3
PhilB wrote:
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?
Isn't it obvious from the error messages?
Thanks!
PhilB

myprog2.cpp: In method `Line::Line (Point, Point)':
myprog2.cpp:32: no matching function for call to `Point::Point ()'
You try to use the default constructor (i.e. `Point::Point ()'), but
your Point class doesn't have one.
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;
}
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.

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


Jul 22 '05 #4
"PhilB" <pb******@yahoo .com> wrote in message
news:10******** *************** ***@posting.goo gle.com...
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);
}
//------------------------


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
Jul 22 '05 #5

"PhilB" <pb******@yahoo .com> wrote in message
news:10******** *************** ***@posting.goo gle.com...
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 ()'
The compiler is trying to tell you there is no default constructor for
Point.
myprog2.cpp:12: candidates are: Point::Point (int, int)
The compiler says it only sees this constructor, to create a point from two
ints.

[snip]
class Point
{
protected:
int x;
int y;
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.
public:
Point::Point(in t, int);
(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.
};

Point::Point(in t initx, int inity)
{
x=initx;
y=inity;
return;
}
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_.)

class Line:public Point
{
public:
Point start_point;
Point end_point;
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.

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

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


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


Jul 22 '05 #6


PhilB wrote:
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 &)
You dont have a default constructor, so convert the existing one to one.

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

Point::Point(in t initx, int inity) make this
Point::Point(in t initx=0, int inity=0) {
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);
}
//------------------------

--
regards,
Vardhan
--

Jul 22 '05 #7

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:br******** *****@news.t-online.com...
PhilB wrote:
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?


Isn't it obvious from the error messages?


The only thing that's obvious is that it isn't.
Jul 22 '05 #8

"lallous" <la*****@lgwm.o rg> wrote in message
news:br******** ****@ID-161723.news.uni-berlin.de...
In the class as:
class Point
{
protected:
int x;
int y;
public:
Point::Point(in t, int);

^^^^remove the "Point::"
};


Same for the other class.

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.
Jul 22 '05 #9

"Vardhan Prabhakar N [C]" <a3****@motorol a.com> wrote in message
news:3F******** ******@motorola .com...
//------------------------
class Point
{
protected:
int x;
int y;
public:
Point::Point(in t, int);
};

Point::Point(in t initx, int inity)

make this
Point::Point(in t initx=0, int inity=0)


Not really a good explanation. That very well might not be what he wants.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1900
by: Judi Keplar | last post by:
I am currently taking a course to learn Python and was looking for some help. I need to write a Python statement to print a comma- separated repetition of the word, "Spam", written 511 times ("Spam, Spam, … Spam"). Can anybody help me get started? I am completely new to programming! Thanks in advance!
15
3833
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I couldn't do it using reqular transact SQL. The problem is that this procedure is taking longer and longer to run. Up to 5 hours now! It is anaylizing about 30,000 records. I think partly because we add new records every month. The procedure...
8
2386
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
7
1616
by: BobJohnson | last post by:
Just started learning C++ and I need some help with my homework, shouldn't take long for people around here. I need to create a simple money calculator but I don't know how to make the output numbers two decimal places long like 10.01 I only know how to define numbers as int or double. Do I use float? Also, I'm using Visual Studio .NET is there anyway to keep the compiler on the screen long enough to actually see what it's outputting. ...
16
1869
by: ranger | last post by:
Hi, I'm a beginner with C++, studying on my own from a book and I've encoutered quite some problems.. If possible, someone out there might help me out.. The problem is the following.. I've tried to create a couple of string class/functions.. and they do compile.. but when I run them.. windows suddenly reports an error.. As I'm just a beginner in C++, with noone around me to help.. I'd appreciate if someone might help me out!
1
1535
by: Simon Matthews | last post by:
Hope someone can help an Access beginner! I've just started keeping my surgical logbook on access and it's a simple flat-file affair. I have created several queries that will list cases performed at different hospitals and reports based on the queries to print out the relavent details. What I would like to do is have a summary sheet in the Report Footer section that lists a grid of each type of procedure performed as well as the...
1
1292
by: newbie | last post by:
Hi I am using a toolkit written in c++ that consists of three parts 1) video captur 2) image processin 3) OpenGL wrappe The video capture program only works with local hardware (for example usb web cam). Since I have to use a remote live camera with a fixed IP-address, I then have to write my own video capture program that replaces the original video library. I am a beginner in programming, so I really don't have a clue how to do...
1
1921
by: hl | last post by:
Hi, I'm a beginner and need a little help with getting data back from a web service. I am using VB.Net and have added a web reference to a Wsdl that was provided to me. My reference.vb file that was generated has the following code at the end. <System.Xml.Serialization.SoapTypeAttribute("Map",
6
1821
by: fool | last post by:
Dear group, Given a string I have to print the permutation, using some looping tricks. This is not a Home work problem. My best try as beginner is: #include<stdio.h> #include<stdlib.h> int main(void)
1
1270
by: macmac | last post by:
I'm new to this forum and I am in a beginner level with programming too. I posted this thing out hoping that somebody could help me out in making this program. I am working right now as a C++ developer well of course as a junior developer, I know fundamentals of C++ and a little knowledge on classes, I also have a little knowledge in networking. My problem right now is that the company is urging me to deliver a bot for D2 game, specifically a...
0
10981
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10643
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.