add/push item to stack list error

ok this is from my main file (.cpp file)
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "AcctListStack.h"
using namespace std;

void displayMenu();

int main()
{
    bool menuActive = true;
    char menuChoice;
    Stack stackList;
    while (menuActive)
    {
        displayMenu ();   // call displayMenu function
        cin >> menuChoice; cin.ignore(80,'\n');
        cout << endl;
        
        switch (menuChoice)
        {
            case '1':
            {
                string stackItemName;
                cout << "Enter stack item: ";
                getline(cin, stackItemName);
                stackList.push(stackItemName); 
                //this is where im getting errors it underlines stackItemName, im using xcode, and says "No viable conversion from 'string' to 'StackItem *" what does this mean?
            }
           //more stuff here but they work, im getting errors in case 1 only. 


and this is from my header file (.h file)

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
struct StackItem
{
    string name;
    StackItem *next;
};

typedef StackItem* nodePtr;


class Stack
{
private:
    
    int      numElements;
    nodePtr  top; 
    int count;  
    
public:
    
    Stack ()
    {
        numElements = 0;
        top = NULL;
    }
    
    
    bool listEmpty ()
    {  return (numElements == 0);  }
    
    void linkNodeByAcctNum (StackItem *newNode, StackItem *prevNode)
    {
        newNode->next = top;
        top = newNode;
    }
    
  
    void push (StackItem* newItem) //the error might be because of this function? 
    {
        newItem->next = top;
        top = newItem;
        nodePtr   p, prev = nullptr;
        p = new StackItem;  // default constructor
        p->name = newItem->name;
        numElements++;
        linkNodeByAcctNum(p, prev);
    }
    
};
#endif 


i put comments on where im getting my errors, thanks!
Last edited on
stackList.push(stackItemName);

what is stackItemName's type? If it is a string, it shouldn't be.

You could be passing a string to your push function, which takes aStackItem* type.
You are trying to pass a string to a function that takes a StackItem* as parameter

You could do
1
2
3
4
StackItem* newItem = new StackItem();
cout << "Enter stack item: ";
getline(cin, newItem->name);
stackList.push(newItem); 
You are trying to pass a string to a function that takes a StackItem* as parameter

Just said that.
Uh, yes... I started writing before you posted
Ø
Last edited on
Topic archived. No new replies allowed.