-2

I keep getting compiler errors specifically about my functions not being apart of a class.

Here is my header file declaration

    #ifndef LL_H
    #define LL_H

    // include this library to use NULL, otherwise use nullptr instead
    #include <cstddef>

    // include iostream so anything that includes this file can use cout
    #include <iostream>

    // Struct which will be the building block of our list
    struct node{

        int val;
        node* next;
        node* prev;
    };

Here is the linked list class definition

    // Linked list class definition
    class LL{
    public:
        LL();
        void prepend(int);
        void append(int);
        void remove(int);
        bool removeFront();
        bool removeBack();
        node* search(int);
        void print();
        void deleteList();
        private:
        node* head;
    };

    #endif

Here is the header file that contains the linked list functions.

  #include "ll.h"

LL::LL()
{
    head = NULL;
}

void LL::prepend(int num)
{
    node* newNode = new node;
    newNode->val = num;
    newNode->next = NULL;

    if (head == NULL)
    {
        head = newNode;
        return;
    }
    else
    {
        newNode->next = head;
        head = newNode;
        newNode = NULL; 
    }

}

void LL::append(int num)
{
    node* newNode = new node;
    newNode->val = num;
    newNode->next = NULL;

    //If there are no nodes, make newNode the first node
    if (head == NULL)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else 
    {
        newNode->next = head;
    }
}

void LL::remove(int num){

    //This function searches for a node with num as its value, if found, it is deleted from the list
    node* second = head;
    node* first = head->next;
    if (head == NULL)
    {
        return;
    }
    else if (head->val == num)
    {
        node* temp = head;
        head = head->next;
        delete temp;
    }   


    while (first&&first->val != num)
     {
        second = first;
        first = first->next;
     }
    if (first)
    {
        second->next = first->next;
        delete first;
    }

}
bool LL::removeFront()
{
    node* newNode = new node;
    head = head->next;

    if (head == NULL)
    {
        newNode = head;
        head = head->next;
        delete newNode;
    }

}

bool LL::removeBack()
    {   

        if (head == NULL)
        {
            return (false);
        }   
        else 
        {
            node* newNode = new node;
            while(newNode->next)
            {
            newNode->next = newNode;
            }

            delete newNode;
            return (true);
        }   
    }   

 node* LL::Search(int num)
    {
        node* newNode = head;
        while (newNode == NULL)
        {
            if (newNode->num == num) 
            {
                return newNode;
            }
        newNode = newNode->next;
        }
        return NULL;
    }  

void LL::print()
    {
        node* temp = head;
        while(temp !=NULL)
        {
            temp = temp->next;
        }

    }

void LL::deleteList()
    {
        node* temp = head->next;
        while(temp != NULL)
        {   
            delete(head);
            head = temp;
            temp = temp->next;
        }

    }   
Do I turn my header bool functions into integer passes? or do I do       something else to prevent the compile errors? I can't use any standard functions in a standard library.

I'm using this main.cpp to test the code where we're inputting values from the first column followed by a number which indicates the function followed by an integer that gets passed to the function to manipulate the linked list.

#include <fstream>          // Include to use ifstream
#include "ll.h"             // Include so can access our class
using namespace std;        // Include so we don't need to put std:: infront
                        // of cout and endl

int main()
{
LL myList;
ifstream input;
int cmd, argument, ret;
node* searchResult = NULL;
input.open("cmd.txt");

// while there is something to read from the file, read
while (input >> cmd)
{
    // switch on the command we read from the file
    switch (cmd)
    {
    // if the cmd requires a parameter, read it from the file and call the 
    // associated function
    case 1:
        input >> argument;
        myList.prepend(argument);
        cout << "Prepended " << argument << endl;
        break;
    case 2:
        input >> argument;
        myList.append(argument);
        cout << "Apended " << argument << endl;
        break;
    case 3:
        if(myList.removeFront())
        {
            cout << "Succesfully removed the front of the list\n";
        }
        else
        {
            cout << "List is empty, nothing to remove\n";
        }
        break;
    case 4:
        if(myList.removeBack())
        {
            cout << "Succesfully removed the back of the list\n";
        }
        else
        {
            cout << "List is empty, nothing to remove\n";
        }
        break;
    case 5:
        input >> argument;
        searchResult = myList.search(argument);
        if(NULL != searchResult)
            cout << "Found " << searchResult->val <<" in list!"<<endl;
        else
            cout << "Did not find " << argument << " in the list"<<endl;
        break;
    case 6:
        myList.print();
        break;
    case 7:
        input >> argument;
        cout << "Attempting to remove " << argument << endl;
        if(myList.remove(argument))
        {
            cout << "Succesfully removed the element from the list\n";
        }
        else
        {
            cout << "Could not remove the element from the list\n";
        }
        break;
    }
}
input.close();

return 0;
    }
  • 2
    You need to add specific details of the error such as the original output from the compiler – leyanpan Aug 31 '17 at 02:08
  • 1
    You also need to fix your keyboard. Your keyboard's TAB key appears to be broken, and without proper and logical indentation the shown code is hard to follow. If you want people to help you with your question, the least you can do is make your question as easy to understand, and as readable as possible. If you don't bother to properly format your code so that others can read it, why should anyone bother to spend some time to help you out? – Sam Varshavchik Aug 31 '17 at 02:42
  • 1
    Don't write so much code at once if you have a basic error .. try compiling much smaller parts – M.M Aug 31 '17 at 05:37
  • @SamVarshavchik I can read the format of my code currently, in fact it follows proper formatting I've seen in several books and what is taken directly from both g++ and C++ dev compilers, what I don't understand is indenting everything 4 spaces as is required when I submit the question. If the code isn't formatted properly then why is the website accepting the format? – Beckster256 Aug 31 '17 at 12:01
  • Of course you can read your own code. But the same cannot be said for others. This website is not a text editor and formatting validator. There are other websites out there that do that. Not this one. Most of the shown code lacks proper indentation. For example, the `remove` method lacks proper indentation, and is mostly unreadable. That is not the only thing. Fixing only that won't fix everything. Most other questions here manage to show readable, properly indented code. If everyone else figured out how to do that, so can you. – Sam Varshavchik Aug 31 '17 at 12:08
  • You declare functions that should return a `bool` but then don't return a Boolean value - I'm not sure what you expect to happen in that case? (Also why are you adding `return;` at the end of your functions with `void` return type?) – UnholySheep Aug 31 '17 at 13:08
  • The only compile error I'm getting now is [Error] no 'void LL:Search(int)' member function declared in class "LL" – Beckster256 Aug 31 '17 at 13:22
  • That's because you declared the function prototype as `node* search(int);` - so both your casing and your return type is wrong – UnholySheep Aug 31 '17 at 13:28
  • Thanks, that fixed that error but introduced 'struct node' has no member named 'num' – Beckster256 Aug 31 '17 at 13:38
  • Then add that member - it's your code, you should know what it's supposed to do. Also SO is not for teaching you programming basics, please read [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for that – UnholySheep Aug 31 '17 at 13:47
  • But it is declared node* LL::Search(int num) – Beckster256 Aug 31 '17 at 19:16

1 Answers1

0

This is the correct way to define the search function

node* LL::search(int num)
    {
        node* newNode = new node;
        newNode = head;
        while (newNode->next != NULL)
        {
            if (newNode->val == num) 
            {
                return newNode;
            }
        newNode = newNode->next;
        }
        return NULL;
    }