473,378 Members | 1,470 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,378 software developers and data experts.

confusing errors

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

When I define an object, some data members cannot be initialized and no
appropriate default constructor is available. So, there is an error.

However, even if I add the default constructor explicitly, there are
still errors in the following code.

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A();
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

Why?

Dec 4 '06 #1
10 1261
heng wrote:
#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A(int y_){y=y_;}
Prefer initialisation over assignment. See FAQ.
};

int main()
{
A aobj;
return 0;
}

When I define an object, some data members cannot be initialized and
no appropriate default constructor is available. So, there is an
error.

However, even if I add the default constructor explicitly, there are
still errors in the following code.

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A();
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

Why?
*What* errors?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #2

heng wrote:
#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

When I define an object, some data members cannot be initialized and no
appropriate default constructor is available. So, there is an error.
Yes, that code will fail because there is no default constructor yet
you are attempting to use one.
>
However, even if I add the default constructor explicitly, there are
still errors in the following code.

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A();
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

Why?
This one will fail to link because there is no definition for your
default constructor.

Dec 4 '06 #3
>
This one will fail to link because there is no definition for your
default constructor.
Do I need to define my default constructor? I think the default
constructor is always as follows:

class Foo
{
public:
Foo(); //default constructor
}

Dec 4 '06 #4
heng wrote:
>This one will fail to link because there is no definition for your
default constructor.

Do I need to define my default constructor? I think the default
constructor is always as follows:

class Foo
{
public:
Foo(); //default constructor
}
No. The default constructor is the one that *can be called* without
any arguments, not the one that doesn't have any arguments. Think
default argument values:

class Foo
{
public:
Foo(int a = 42); // defautl constructor
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #5

heng wrote:

This one will fail to link because there is no definition for your
default constructor.

Do I need to define my default constructor? I think the default
constructor is always as follows:

class Foo
{
public:
Foo(); //default constructor
}
That is a declaration, not a definition. There is an important
distinction. All that does is say Foo() exists...but if it isn't
defined somewhere then it doesn't actually exist.

The original code caused the default constructor to not be created by
creating a custom one. In other words,

class X {};

has a default constructor while,

class Y { Y(int x) {} };

does not.

With,

class Y {
Y(int x) {}
Y();
};

The compiler will hapily compile any code that uses the default
constructor for Y assuming it will find it somewhere later and be able
to link to it. If there is no

Y::Y() {}

in your code anywhere (certainly not in what you posted) then of course
it never finds it.

Dec 4 '06 #6
Noah Roberts wrote:
>
class Y {
Y(int x) {}
Y();
};
So, in your code, the default constructor of Y is declared, but not
defined, hence it cannot be used by the compiler.

Right?

Dec 4 '06 #7
On 2006-12-04 18:00, heng wrote:
Noah Roberts wrote:
>>
class Y {
Y(int x) {}
Y();
};
So, in your code, the default constructor of Y is declared, but not
defined, hence it cannot be used by the compiler.
Actually the compiler will be happy but the linker won't.

--
Erik Wikström
Dec 4 '06 #8
Erik Wikström wrote:
On 2006-12-04 18:00, heng wrote:
Noah Roberts wrote:
>
class Y {
Y(int x) {}
Y();
};
So, in your code, the default constructor of Y is declared, but not
defined, hence it cannot be used by the compiler.

Actually the compiler will be happy but the linker won't.

--
Erik Wikström
Thanks a lot. What's the role of the linker?

Dec 4 '06 #9
On 2006-12-04 19:20, heng wrote:
Erik Wikström wrote:
>On 2006-12-04 18:00, heng wrote:
Noah Roberts wrote:

class Y {
Y(int x) {}
Y();
};

So, in your code, the default constructor of Y is declared, but not
defined, hence it cannot be used by the compiler.

Actually the compiler will be happy but the linker won't.

--
Erik Wikström

Thanks a lot. What's the role of the linker?
The compiler takes each .cpp-file and compiles it (before that the
pre-processor has expanded all your #includes into the file *) which
gives you an object-file, one for each .cpp-file. The linker than takes
all the object-files and puts them together into an executable. This is
good since when you change one .cpp-file you don't have to recompile all
the others, just the change one and then re-link it all.

* What the pre-processor does is to replace each #include-line with the
content of the file given, which can create quite large files. This
large file is a compilation-unit, which is what the compiler operates on.

--
Erik Wikström
Dec 4 '06 #10
Thanks a lot.

Erik Wikström wrote:
On 2006-12-04 19:20, heng wrote:
Erik Wikström wrote:
On 2006-12-04 18:00, heng wrote:
Noah Roberts wrote:

class Y {
Y(int x) {}
Y();
};

So, in your code, the default constructor of Y is declared, but not
defined, hence it cannot be used by the compiler.

Actually the compiler will be happy but the linker won't.

--
Erik Wikström
Thanks a lot. What's the role of the linker?

The compiler takes each .cpp-file and compiles it (before that the
pre-processor has expanded all your #includes into the file *) which
gives you an object-file, one for each .cpp-file. The linker than takes
all the object-files and puts them together into an executable. This is
good since when you change one .cpp-file you don't have to recompile all
the others, just the change one and then re-link it all.

* What the pre-processor does is to replace each #include-line with the
content of the file given, which can create quite large files. This
large file is a compilation-unit, which is what the compiler operates on.

--
Erik Wikström
Dec 4 '06 #11

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

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
65
by: Pmb | last post by:
I'm confused as to what the compiler error message I'm getting is refering to. Can someone take a gander and let me know what I did wrong? The program is below. When I compile it I get the...
6
by: blueblueblue2005 | last post by:
Hi, I am reading the const function definition part in Deitel book, it says : Defining as const a member function that calls a non-const member function of the class on the same instance of the...
16
by: Shelly | last post by:
(posted previously on comp.lang.php but no response received. Cross-posted in the dreamweaver forum) I am confused about what goes on with method POST. Here is an overview of a my code,...
8
by: ImOk | last post by:
I just have a question about trapping and retrying errors especially file locking or database locks or duplicate key errors. Is there a way after you trap an error to retry the same line that...
1
by: abcbook | last post by:
Can anyone explain a get around for this .js file I’m trying to create or tell me if there is one or what am I missing. I Do Not what to put the form Directly on the HTML Page. This .js file will...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
1
by: Nyx18 | last post by:
ok so i got my class to work now im having confusing errors with this: void sort(Student stu, int parameter, int count) { Student temp; bool finished = false; while (!finished) ...
7
by: Andy B | last post by:
I saw this in the set accessor of a property: Set(ByVal value As DataSet) What exactly does the stuff in the () mean? VS complained about it not being there when I took it out not knowing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.