General Constructor Error
Oct 17, 2015 at 3:05pm UTC
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
#ifndef NODE_H
#define NODE_H
class Node {
public :
int key;
Node *next;
void add(int key);
void print();
Node *reverse();
int *array();
Node(int *array);
};
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.
Oct 17, 2015 at 3:48pm UTC
Topic archived. No new replies allowed.