I'm having a bit of trouble getting this constructor to cooperate. The constructor is supposed to create a linked list from the given array but I can;t even get it to compile correctly. It just gives me:
"No matching function for call to 'Node::Node()'. Candidates are Node::Node(int*), Node::Node(const Node&)'
Is my syntax wrong somewhere? I tried building it in the header, and the Node.cpp source with the rest of the functions but no dice.
Below is the Node.cpp source with most of the WORKING functions. They have been tested to work.
Ok, I declared the default constructor but I realized I was confused about how to declare it since it contains int key and *next. I updated the code to reflect what I've added in Node.h as the default constructor:
1 2 3 4
Node::Node(int data){
next = NULL;
key = data;
}
Is this how the default constructor should be declared? Its still throwing the same duplication error.
A constructor is only the default constructor if it can be called with no arguments. If you can't pass 0 arguments to it, it isn't the default constructor.
but it still threw the same error so I thought I had to define the values.
I apologize for all these questions. I realize these are pretty elementary questions but I couldn't find a lot of information on implementing default constructors with a function. Which brings me to my next question:
Is the implementation for Node::Node(int *array) member function correct? That way I can just focus on working on the smaller issues before I assume its anything other than default constructors.
You should only need to add a single line to the class definition:
Node() = default;
This tells the compiler to generate the constructor using its default implementation.
Do not confuse the terms "constructor" and "member function" - they are fundamentally different.
Your implementation of the array constructor is incorrect because it blindly assumes that the size of the array is 4. If you can't use std::vector, then pass the size of the array as a second parameter.
@LB The assignment was initially defined incorrectly so I have to assume the array being passed is size 4 since I Can only pass an array as a single parameter. The assignment was redefined to assume the array is of size 4. Sorry, forgot to clear that up earlier.
I'm terribly confused now after the last two posts. I updated the code as best as I could understand but I'm still getting the multiple definition error :
/classes/cs1254/cs125423/prog4/Node.h:21: multiple definition of `Node::Node()'
/tmp/ccKoKK9I.o:/classes/cs1254/cs125423/prog4/Node.h:21: first defined here
/tmp/ccWWtutr.o: In function `Node':
/classes/cs1254/cs125423/prog4/Node.h:21: multiple definition of `Node::Node()'
/tmp/ccKoKK9I.o:/classes/cs1254/cs125423/prog4/Node.h:21: first defined here
/tmp/ccWWtutr.o: In function `Node':
/classes/cs1254/cs125423/prog4/Node.h:23: multiple definition of `Node::Node(int*)'
/tmp/ccKoKK9I.o:/classes/cs1254/cs125423/prog4/Node.h:23: first defined here
/tmp/ccWWtutr.o: In function `Node':
/classes/cs1254/cs125423/prog4/Node.h:23: multiple definition of `Node::Node(int*)'
/tmp/ccKoKK9I.o:/classes/cs1254/cs125423/prog4/Node.h:23: first defined here
I really appreciate the help everyone! I feel like this is super trivial and it's probably a simple fix but I;m just having a hard time getting stuck with something this small.
It looks like you tried to define your constructors in the header - don't do that. Function definitions go in source files. Only the declarations go in headers.