473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Constructor & default arguments

This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)':
Main.cpp
no matching function for call to `Dot::Dot(Point&)'
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main':
[Linker error] undefined reference to `Dot::Dot()'

I'm using DEV-C++ and gcc.

Why doesn't it find the matching constructor:

Dot::Dot(Point&, Color&)

--The Directive
Jul 22 '05 #1
11 2577

"The Directive" <th***********@hotmail.com> wrote in message news:84**************************@posting.google.c om...
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}


I can't tell for sure with the snippet you've provided, but I suspect that at the point
where the "new Dot(..." is executed you haven't provided the default arguments yet.
The declaration with the defaulted arguments must be seen before the place it is used.
For example:

void func(int);

func();

void func(int x = 3) { }

won't compile because at the point func() is called, it hasn't seen the default values for
the arguments.

Jul 22 '05 #2

"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );
temp is of type "Dot", but you are trying to assign a Dot* (which is what
"new Dot" returns) to it.

Instead, the correct way to call a constructor might be:

Dot temp (*new Point (5, 5));

Code involving "*new" usually leads to memory leaks! You will need to
forget many Java habits. Do you have a decent C++ book?

In Dot class:

// Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}
First of all, you can only define default arguments in the function
declaration. (inside your class for a constructor).

Also, you're using *new again. In C++ you never use "new" without later
using "delete", since C++ does not have garbage collection. Since you don't
actually store the pointers here, you can't delete them, and you will get a
memory leak any time you use the default arguments.

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)':
Main.cpp
no matching function for call to `Dot::Dot(Point&)'
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main':
[Linker error] undefined reference to `Dot::Dot()'

I'm using DEV-C++ and gcc.

Why doesn't it find the matching constructor:

Dot::Dot(Point&, Color&)
It's hard to tell without more complete code.

--The Directive


HTH
--
KCS
Jul 22 '05 #3

"The Directive" <th***********@hotmail.com> skrev i en meddelelse
news:84**************************@posting.google.c om...
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)':
Main.cpp
no matching function for call to `Dot::Dot(Point&)'
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main':
[Linker error] undefined reference to `Dot::Dot()'

I'm using DEV-C++ and gcc.

Why doesn't it find the matching constructor:

Dot::Dot(Point&, Color&)

--The Directive


That code is just so shockfull of errors, I must recommend that you

a) forget about Java when coding C++.
b) learn about C++ - in particular when to use new and const correctness.

One link for you: http://www.parashift.com/C++-faq-lite

Kind regards
Peter

Jul 22 '05 #4
Dot *temp = ...;
or
Dot temp( *(new ...;

The Directive wrote:
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)':
Main.cpp
no matching function for call to `Dot::Dot(Point&)'
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main':
[Linker error] undefined reference to `Dot::Dot()'

I'm using DEV-C++ and gcc.

Why doesn't it find the matching constructor:

Dot::Dot(Point&, Color&)

--The Directive


Jul 22 '05 #5
Robert Paul Clark wrote:
<top-posted stuff>

You've posted a whole bunch of top-posted messages now. Could you please
stop that? It's very irritating.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6
[Snipped]
I can't tell for sure with the snippet you've provided, but I suspect that at the point
where the "new Dot(..." is executed you haven't provided the default arguments yet.
The declaration with the defaulted arguments must be seen before the place it is used.
For example:

void func(int);

func();

void func(int x = 3) { }

won't compile because at the point func() is called, it hasn't seen the default values for
the arguments.


Ron,

You hit the nail on the head.

--The Directive
Jul 22 '05 #7
"Kevin Saff" <go********@kevin.saff.net> wrote in message news:<Hr********@news.boeing.com>...
"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );
temp is of type "Dot", but you are trying to assign a Dot* (which is what
"new Dot" returns) to it.


This was a typo when posting the code.
Instead, the correct way to call a constructor might be:

Dot temp (*new Point (5, 5));

Code involving "*new" usually leads to memory leaks! You will need to
forget many Java habits. Do you have a decent C++ book?

In Dot class:

// Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}
First of all, you can only define default arguments in the function
declaration. (inside your class for a constructor).


Your statement is incorrect or I don't comprehend what you're stating:

#include <iostream>
#include <stdlib.h>

using namespace std;

class base
{
public:

void function (int);
};

void base::function (int temp = 4)
{
cout << temp << "\n";
}

int main(int argc, char *argv[])
{
base temp;

temp.function();
temp.function(7);

system("PAUSE");
return 0;
}

The code compiles, runs, and does what it's suppose to do.

However, the following version of the previous program doesn't work:

#include <iostream>
#include <stdlib.h>

using namespace std;

class base
{
public:

void function (int);
};

int main(int argc, char *argv[])
{
base temp;

temp.function();
temp.function(7);

system("PAUSE");
return 0;
}

void base::function (int temp = 4)
{
cout << temp << "\n";
}

which surprises me that the compiler (or the C++ standard) is
not smart enough to compile this version of the program.
Also, you're using *new again. In C++ you never use "new" without later
using "delete", since C++ does not have garbage collection. Since you don't
actually store the pointers here, you can't delete them, and you will get a
memory leak any time you use the default arguments.


When I post code, I usually only post a portion or fragments of the
true code to make it easy to illustrate my question.

--The Directive
Jul 22 '05 #8
[Snipped]
That code is just so shockfull of errors, I must recommend that you

a) forget about Java when coding C++.
b) learn about C++ - in particular when to use new and const correctness.
I did learn about C++. I'm now putting into practice what I've
learned. That's part of learning also. Writing c++ sample code
clarifies my C++ knowlege and expands it.

I understood people responses about the pointer typo, new/delete
usage, and default arguments. What do you specifically mean by "const
correctness."?
One link for you: http://www.parashift.com/C++-faq-lite

Kind regards
Peter


--The Directive
Jul 22 '05 #9
The Directive wrote:

First of all, you can only define default arguments in the function
declaration. (inside your class for a constructor).
Your statement is incorrect or I don't comprehend what you're stating:


Not at all:

8.3.6 Default arguments

3 A default argument expression shall be specified only in the
parameter-declaration-clause of a function declaration or in
a template-parameter.

[snip example 1]

The code compiles, runs, and does what it's suppose to do.
.... but is still illegal.

[snip example 2]
which surprises me that the compiler (or the C++ standard) is
not smart enough to compile this version of the program.
The compiler reads the source code from top to bottom. It never
steps back and corrects previously compiled code.

In

class base
{
public:
void function(int);
};

int main()
{
base temp;

temp.function();

the compiler expects you to provide an argument to function(). This
is what the declaration has told the compiler.

When I post code, I usually only post a portion or fragments of the
true code to make it easy to illustrate my question.


Then prepare that people will correct mistakes in your posting which
are not there in your true source code. If you do that often enough
(posting not the true code), people finally will stop correcting
your programs, since it is a waste of time to correct errors and
afterwards you are saying: it was only a typo in posting.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10
Karl Heinz Buchegger wrote:


When I post code, I usually only post a portion or fragments of the
true code to make it easy to illustrate my question.


Then prepare that people will correct mistakes in your posting which
are not there in your true source code. If you do that often enough
(posting not the true code), people finally will stop correcting
your programs, since it is a waste of time to correct errors and
afterwards you are saying: it was only a typo in posting.


Forget that last paragraph. I misread what you typed. You post
real code (using cut&paste), that's fine.

But still: using *new usually indicates a problem.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11

"The Directive" <th***********@hotmail.com> skrev i en meddelelse
news:84**************************@posting.google.c om...
[Snipped]
That code is just so shockfull of errors, I must recommend that you

a) forget about Java when coding C++.
b) learn about C++ - in particular when to use new and const
correctness.
I did learn about C++. I'm now putting into practice what I've
learned. That's part of learning also. Writing c++ sample code
clarifies my C++ knowlege and expands it.

I understood people responses about the pointer typo, new/delete
usage, and default arguments. What do you specifically mean by "const
correctness."?
One link for you: http://www.parashift.com/C++-faq-lite

Kind regards
Peter
--The Directive


Ok - I'll bite. Here is your original code:

Dot temp = new Dot( *(new Point( 5, 5 )) );
Your first confusion (Java-inspired):

In C++
a) whenever you use new to create some variable (e.g. an object), you must
use delete to reclaim the space allocated. C++ does not have
garbage-collection, so you are leaking memory.
b) new does not return an object, but a pointer to one. Thus, the code above
would not compile (you can't normally assign a pointer to an object to an
object). Probably the code above should be written as:

Dot temp = Dot(Point( 5, 5));

or more succintly:
Dot temp(Point( 5, 5));

However, with the definition of the Dot-constructer being as it is, this
would not compile. Let's move on:

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )


Here you do again use new to create an object. new is a relatively slow
operation (compared to no new), and thus the call is less than optimal in
speed. This is neglectible compared to the memory leak, of course. The
proper constructor should probably be:

Dot::Dot(Point point = Point( 5,5),Color color = Color(100,125,106))

which is better except for a perhaps inefficient passing of arguments. And
this is where const correctness comes into play. If you can, you should
always strive to pass arguments by value (as above) or by const reference
(prefer const reference if you can - except for the simplest types (built
ins)). This gives you the following interface:

Dot::Dot(Point const& point = Point( 5,5),Color const& color =
Color(100,125,106))

In a constructor you should always strive to allow all parameters to be
temporary - which means pass-by value (Point point) or by const reference
(Point const& point).

When you see an assignment x = y; you would not normally expect y to change,
would you? And yet, this is precisely what could happen in your
Dot-constructor.

Kind regards
Peter
Jul 22 '05 #12

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

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
6
by: Gunnar G | last post by:
If I don't define a default constructor for my class "Foo", I still get one from the compiler. But if I define a constructor that takes an argument, I don't get the default constructor, why? I...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
34
by: Jason Heyes | last post by:
Is there a special name for a constructor that does struct-like initialisation? Example: class DefaultCtor { std::string class_name; bool compiler_generated; public:...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.