Trouble with linked list function calls

Hey ya'll, this is my first time working with linked list and I have to create a menu for the user to mutate their list. The user is only allowed to enter primary colors(red,yellow,blue). I'm having some trouble with one of my function calls and I can't seem to fix what's wrong with the function. Any hints would be appreciated.

Heres the error the compiler throws:
error: no matching function for call to ‘ColorsList::appendNode(std::string (&)())’
color.appendNode(validateUserInput);

Here's my main:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <iostream>
#include <string>
#include "ColorsList.h"
using namespace std;

//Prototypes
int get_user_choice();
void display_menu();
string validateUserInput();

int main()
{
    //ColorsList test;
    //test.appendNode("Red");
    //cout<<"node appended";
    int user_input;
    ColorsList color;
    
    do{
        display_menu();
        user_input = get_user_choice();
        
        switch(user_input)
        {
            case 1: 
                color.appendNode(validateUserInput);
                break;
            case 2:
                cout<<"InsertNode method";
                break;
            case 3:
                cout<<"deletenode method";
                break;
            case 4:
                cout<<"printList method";
                break;
            case 5:
                cout<<"ReverseList method";
                break;
            case 6:
                cout<< "SearchList method";
                break;
            case 7:
                cout<<"Exit Mehtod";
                break;
        }  
    }while(user_input != 7);

    return 0;
}

int get_user_choice()
{
    //will hold the user's choice
    int user_choice = 0;
    
    //reading in user choice
    cin >> user_choice;
    
    return user_choice;
}

void display_menu()
{
    cout<< "\n"<<"Please enter a value (1-7)\n"<<"1. Append\n" << "2. Insert\n" << "3. Delete\n" <<
                "4. Print\n"<< "5. Reverse\n" <<"6. Search\n"<<"7. Exit\n"<<endl;
}

//Method used for high level validation
string validateUserInput()
{
    string user_input;
    
    cout<<"Please enter a color you'd like to add to your list\n";
    cout<<"Your choices are Red, Yellow, and Blue\n";
    
    getline(cin, user_input);
    
    for(int i =0; i < user_input.length(); i++)
    {
        tolower(user_input[i]);
    }
    
    
    while(user_input != "red" && user_input != "yellow" && user_input != "blue")
    {
        cout<< user_input<< " is not a color that is allowed"<<endl;
    }
    
    return user_input;
}


Here's my .h:
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
#ifndef COLORSLIST_H
#define COLORSLIST_H
#include<string>
using namespace std;

class ColorsList
{
private:
    //declare structure for list
    struct ListNode
    {
        //The value that is stored in node
        string colorType;
        //Pointer which points to next node
        struct ListNode *next;
    };
    
    //Creating head pointer for List
    ListNode *head;
public:
    //Class constructor
    //setting pointer to head of list to nullptr
    ColorsList()
    { head = nullptr; }
    
    //Destructor
    ~ColorsList()
    {
        
    }
    
    //Linked list operations
    void appendNode(string);    //requires validation for input in main
    void insertNode();          //requires validation for input in main
    void deleteNode();          //requires validation for input in main
    void printNode();
    void printReverse();
    void searchNode();          //requires validation for input in main
    void exitMenu();
};
#endif 

Here's my .cpp:
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
#include <iostream>
#include <iomanip>
#include <string>
#include "ColorsList.h"
using namespace std;



void ColorsList::appendNode(string color)
{
    //Will point to a new node
    ListNode *newNode;
    //Pointer used to traverse though list
    ListNode *nodePtr;
    
    //create a new node
    newNode = new ListNode;
    //Mutator validation
    if(color != "red" && color != "yellow" && color !="blue")
    {
        cout<<"Invalid color. Enter a primary color\n. Ex: Red, Yellow, or Blue\n";
        exit(EXIT_FAILURE);
    }
    else
    {
        //strore color in new node
        newNode-> colorType = color; 
    }
    
    //will point to last node of list
    newNode-> next=nullptr;
    
    //If there are no nodes in list
    //Make the new node the first node in list
    if(!head)
    {
        //make newNode the first node in list
       head = newNode; 
    }
    else
    {
        //Set nodePtr to the beginning of list
        nodePtr = head;
        
        //While we haven't reached the end of the list
        while (nodePtr->next)
        {
            //set nodePtr to the next node in list
            nodePtr = nodePtr->next;
            //set newNode to the node nodePtr is pointing to
            nodePtr->next = newNode;
        }
    }
    
    
}
Last edited on
validateUserInput is a function. You want to pass the return value from that function, so you need to call the function. So, add the () needed to call the function.

Line 27 should be color.appendNode(validateUserInput());


By the way, when you encounter an error, go ahead an paste the entire error message here, including file names, line numbers, etc. It will help us locate the problem more quickly.

Edit: Your prompt for colors says "Red, Yellow, and Blue", but your code accepts "red", "yellow" and "blue". Capitalization matters, and your code is inconsistent. You can use tolower() to change characters to lower case, but why not just prompt for "red, yellow, and blue" to get started?

Last edited on
Ah, thanks for the correction. I did use a for loop that converts the user string into lowercase in my validateUserInput method. I'll be sure to fix the promt as well.
Topic archived. No new replies allowed.