| |

VerySource

 Forgot password?
 Register
Search
View: 654|Reply: 3

Ask a basic question, about the declaration ~

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-4 15:50:01
| Show all posts |Read mode
Written like this, you can compile
class ilist_item {
public:
ilist_item (int value, ilist_item * item = 0);
~ ilist_item () {};
private:
int _value;
ilist_item * _next;
};

class ilist {
public:
ilist (): _ at_front (0), _ at_end (0), _ size (0) ()
void insert (ilist_item * ptr, int value);
int size ();

private:
// Prevent calling these two functions, causing unnecessary errors
ilist (const ilist&);
ilist&operator = (const ilist&);
ilist_item * _at_front; // A pointer is defined and the constructor is not called
ilist_item * _at_end;
int _size;
};


If you write it like this, it will prompt that ilist_item has no constructor when compiling:
class ilist_item;

class ilist {
public:
ilist (): _ at_front (0), _ at_end (0), _ size (0) ()
void insert (ilist_item * ptr, int value);
int size ();

private:
// Prevent calling these two functions, causing unnecessary errors
ilist (const ilist&);
ilist&operator = (const ilist&);
ilist_item * _at_front; // A pointer is defined and the constructor is not called
ilist_item * _at_end;
int _size;
};

class ilist_item {
public:
ilist_item (int value, ilist_item * item = 0);
~ ilist_item () {};
private:
int _value;
ilist_item * _next;
};


But why doesn't it prompt me to repeat the definition error?

Change the class ilist_item; to:
extern class ilist_item;
A warning appears: warning C4091: "extern": Ignore left side of "ilist_item" when no variable is declared
What does this warning mean?
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-4 16:15:01
| Show all posts
The first line of the second method: class ilist_item;
Just a statement telling the compiler that there is a definition for this later. So there are no duplicate definition errors
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-4 16:33:01
| Show all posts
class ilist_item;
This is called a class declaration
Classes containing {}; are called class definitions
If you try to write a class ilist_item {}; you will be prompted
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 09:33:01
| Show all posts
Back:cat00019

You will implement void insert (ilist_item * ptr, int value); and use new ilist_item (value, ptr)
You can see what I said wrong

Back:diamond52

It stands to reason that it is not wrong to declare a definition (function is like this), why will it prompt that ilist_item has no constructor when compiling.

But the words of the class ... a bit like data types. Data types only have declaration assignments ..
...
Haha, a little confused
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list