Thanks for your reply!
keskiverto wrote: |
---|
The nullptr has specific pointer type.
The NULL is a macro, like you would write . |
Ah okay, I don't have a lot of experience with macros.
keskiverto wrote: |
---|
It is (usually) a good habit to initialize variables, particularly pointers. |
Ah, I've heard a lot of different people say a lot of different things for this. I started doing things like
int num = 0;
or
std::string str1 = "";
, then people told me on the cplusplus forums that there's no need to do that because they are set to that by default. So I stopped doing that and now someone tells me it's a good habit. Haha..
keskiverto wrote: |
---|
I wrote 42, not int 42. |
My response from above explained why I wrote
int data
rather than just
data
:
PiggiesGoSqueal wrote: |
---|
I looked it up and I know error: new initializer expression list treated as compound expression was because it didn't include a data type. So I added the int to data and that made 1 less error appear. But I don't know what datatype nullptr would be. |
In other words, when I had it only has
data
another error appeared.
I removed the "int" from int data and got these errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
In file included from Main.cpp:39:0:
DoublyLinkedList.h: In member function ‘void IntDoublyList::push_front(int)’:
DoublyLinkedList.h:73:56: error: new initializer expression list treated as compound expression [-fpermissive]
head = new Int_Node2(data, nullptr, nullptr); // 1.
^
DoublyLinkedList.h:73:56: error: no matching function for call to ‘Int_Node2::Int_Node2(std::nullptr_t)’
DoublyLinkedList.h:8:8: note: candidate: Int_Node2::Int_Node2()
struct Int_Node2 {
^~~~~~~~~
DoublyLinkedList.h:8:8: note: candidate expects 0 arguments, 1 provided
DoublyLinkedList.h:8:8: note: candidate: constexpr Int_Node2::Int_Node2(const Int_Node2&)
DoublyLinkedList.h:8:8: note: no known conversion for argument 1 from ‘std::nullptr_t’ to ‘const Int_Node2&’
DoublyLinkedList.h:8:8: note: candidate: constexpr Int_Node2::Int_Node2(Int_Node2&&)
DoublyLinkedList.h:8:8: note: no known conversion for argument 1 from ‘std::nullptr_t’ to ‘Int_Node2&&’
|
keskiverto wrote: |
---|
You can either use aggregate initialization:
head = new Int_Node2 { data, nullptr, nullptr };
or provide a constructor:
1 2 3 4 5 6 7 8 9 10
|
struct Int_Node2 {
int data;
Int_Node2 *next;
Int_Node2 *prev;
Int_Node2( int d, Int_Node2* p, Int_Node2* n )
: data( d ), next( n ), prev( p )
{}
};
head = new Int_Node2( data, nullptr, nullptr );
|
|
Well, I'm thoroughly confused now. Okay, so was the first comment of
head = new Int_Node2 ( data, nullptr, nullptr );
assumed I would add a constructor too? And I didn't which is why I got an error, yes?
Then the aggregate initialization method only requires that single line to work, yes?
I tried both methods. The aggregate initialization method gave a Segmentation fault when ran.
Here's the setup for that:
1 2 3 4 5
|
void push_front(int data) {
// FIXME - Work In Progress
head = new Int_Node2 { data, nullptr, nullptr };
head->next->prev = head;
}
|
Then I commented out that line and tried with the constructor:
- Here is the part for the struct code:
1 2 3 4 5 6 7 8
|
struct Int_Node2 {
int data;
Int_Node2 *next; // address of the next node
Int_Node2 *prev; // address of the previous node
Int_Node2( int d, Int_Node2* p, Int_Node2* n )
: data( d ), next( n ), prev( p )
{}
};
|
- Here is the part for the actual member function:
1 2 3 4 5 6
|
void push_front(int data) {
// FIXME - Work In Progress
//head = new Int_Node2 { data, nullptr, nullptr };
head = new Int_Node2( data, nullptr, nullptr );
head->next->prev = head;
}
|
This gave errors when I tried to compile it:
http://cpp.sh/6leuq
(I put in a cpp.sh link because I reached maxed post length when I pasted it)