I'm trying to store a list of names in alphabetical order, while implementing this linked list, but I've bumped into some errors. This is what I have:

Node.h

class node
{public:
    node(node* , node* , node* , node* , node* , node* , node* , node* , node* , node*);

  private:
    string data;node* previous;node* next;
    friend class list;
    friend class iterator;};

List.h

# include <string>
# include "iterator.h"
# include<iostream>
using std::string;
class list
{public:
    list();void push_back();
    iterator begin();
    iterator end();

    private:
    node* first; node* last;};

And this is where I have my problems (like 'node' no appropriate default constructor, and 'first' undeclared identifier)

# include "node.h"
using std::string;

node::node(node* , node* , node* , node* , node* , node* , node* , node* , node* , node*)
{node* t1 = new node();
        t1->previous = NULL;
        t1->data = "mark";
        t1->next = NULL;
        first = t1;

        node *t2 = new node();
        t2->previous = t1;
        t2->data = "ray";
        t2->next = NULL;
        t1->next = t2;

        node *t3 = new node();
        t3->previous = t2;
        t3->data = "cst";
        t3->next = NULL;
        t2->next = t3;

        node *t4 = new node();
        t4->previous = t3;
        t4->data = "rat";
        t4->next = NULL;
        t3->next = t4;

        node *t5 = new node();
        t5->previous = t4;
        t5->data = "pig";
        t5->next = NULL;
        t4->next = t5;

        node *t6 = new node();
        t6->previous = t5;
        t6->data = "rex";
        t6->next = NULL;
        t5->next = t6;

        node *t7 = new node();
        t7->previous = t6;
        t7->data = "pen";
        t7->next = NULL;
        t6->next = t7;

        node *t8 = new node();
        t8->previous = t7;
        t8->data = "sheep";
        t8->next = NULL;
        t7->next = t8;

        node *t9 = new node();
        t9->previous = t8;
        t9->data = "sun";
        t9->next = NULL;
        t8->next = t9;

        node *t10 = new node();
        t10->previous = t9;
        t10->data = "Dog";
        t10->next = NULL;
        t9->next = t10;
}

Dani AI

Generated

The reported errors have three root causes: the class node declares a non-default constructor so calls like new node() fail; the node constructor in the implementation is trying to modify first (a member of the list class) even though node methods cannot access list instance members; and responsibilities are mixed — node construction and whole-list linking belong in the list implementation, not inside a node ctor. pointed to forum rules and correctly suggested the STL alternative (std::list::sort) as an easier, safer path.

Recommended fixes and practical notes:

  • Give node a simple, self-contained constructor with default args so new node() works, and do not touch list members from inside it. Keep node as a POD-like element and let list manage links and ownership.
  • Build and maintain the sorted order by inserting each new node into the correct place from the list interface (or use std::list / std::vector + std::sort if allowed). String comparisons are case-sensitive; normalize case or use locale-aware compare when needed.
  • Header hygiene: add include guards or #pragma once, avoid using namespace std; in headers, include <string>, and compile with -Wall -Wextra to catch common mistakes. Prefer nullptr (C++11+) and consider smart pointers for ownership.

Example (concept only — not the original code):

struct Node {
    std::string data;
    Node* prev;
    Node* next;
    Node(const std::string& s = "", Node* p = nullptr, Node* n = nullptr)
        : data(s), prev(p), next(n) {}
};

void insert_sorted(Node*& head, Node* n) {
    if (!head || n->data < head->data) { n->next = head; if (head) head->prev = n; head = n; return; }
    Node* cur = head;
    while (cur->next && cur->next->data < n->data) cur = cur->next;
    n->next = cur->next; if (cur->next) cur->next->prev = n;
    cur->next = n; n->prev = cur;
}

This approach separates concerns, resolves the constructor/linker errors, and makes sorted insertion straightforward.

Recommended Answers

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.