Constructor Error

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include "Node.h"

void Node::add(int key) {
  Node *pos = this;
  while (pos->next != NULL)
    pos = pos->next;
  pos->next = new Node;
  pos       = pos->next;
  pos->key  = key;
  pos->next = NULL;
}

void Node::print() {
  Node *pos = this;
  while (pos != NULL) {
    std::cout << pos->key << " ";
    pos = pos->next;
  }
  std::cout << std::endl;
}

Node *Node::reverse() {
  Node *head = this;
  if (!head->next)
    return head;
  Node *left = NULL,
        *mid = head,
      *right = head;

  while(mid != NULL){
   right     = mid->next;
   mid->next = left;
   left      = mid;
   mid       = right;
  }
  return left;
}

int *Node::array() {
  Node *pos = this;
  int count = 0;

  while (pos != NULL) {
    count++;
    pos = pos->next;
  }

  int *arr;
  arr = new int[count];

  pos = this;
  for(int i=0; i<count; i++) {
     arr[i] = pos->key;
     pos    = pos->next;
  }

  return arr;
}



AS well as the Node.h which contains the constructor that is giving me issues:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef NODE_H
#define NODE_H
#include <cstddef>

class Node {

  public:

    int key;
    Node *next;

    void add(int key);
    void print();
    Node *reverse();
    int *array();
    Node();
    Node(int *array);
    

};

Node::Node(){}



Node::Node(int *array){
  Node *head = this;
  for(int i=0; i<4; i++){
    head->add(array[i]); 
  }
}

#endif 


and the main.cpp that runs everything:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Node.h"
#include "DNode.h"
#include <iostream>
using namespace std;

int main() {

  
  Node *head = new Node();
  head->key = 2;
  head->add(3);
  head->add(5);
  head->add(7);
  head->print();

  head = head->reverse();
  head->print();

  int counter = 0;
  int *arr;
  arr = head->array();
  for (int i=0; i<4; i++)
    std::cout << arr[i] << std::endl;
} 


Taking out the Node(int *array) constructor has everything working fine. I'm at my wit's end with this...
Last edited on
You forgot to declare/define the default constructor. The one the compiler generates for you only exists as long as you don't declare your own.
Oooooohhhh Ok. Thank you! I'll give it a try! and sorry about the double post!
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.
Last edited on
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.
Last edited on
I initially had tried simply using

Node::Node();

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.
Last edited on
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.
@ct180 Node::Node(){} not ;
@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
Last edited on
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.
Wow! Thank you guys so much! Everything works smooth now! :D
I can't thank you guys enough! Thanks for putting up with me lol
Topic archived. No new replies allowed.