Object method error says non-const must be an lvalue.

Hi so I'm receiving an error that I have not yet encountered that says " Initial value of reference must be an lvalue" I am trying to write the implementation of a Stack with an ADT write-up that I have for linked list. Can someone please steer me in the right direction as to how to solve this error

1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp

#include <iostream>
#include "List.h"
#include "Stack.h"
using namespace std;
int main()
{
	Stack obj1;
	obj1.push(2);//Initial value of reference must be an lvalue
	cout << obj1.top();//Too few arguments in function call
}


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

//Stack.h file
#include "List.h"

class Stack
{
	public:
		struct Node
		{
			int   value;
			Node* next;
			Node(int v, Node* n) : value(v), next(n) {}
		};

		Node*   head;
		int     size;
	
		Stack() :head(nullptr), size(0) 
		{}
	
		void push(List<int> &l_dummy, int * const &v) 
		{ 
			l_dummy.PushFront(*v); 
		}

		void pop(List<int> &l_dummy)
		{ 
			l_dummy.PopFront();
		}

		int  top(List<int> &l_dummy) 
		{ 
			l_dummy.Front();
		}

		int  size(List<int> &l_dummy) 
		{ 
			l_dummy.Size();
		}

		~Stack()
		{
			for (Node* next = head;next;)
			{
				Node* old = next;
				next = old->next;
				delete old;
			}
		}
};


The class function Stack::push takes the following parameters:
(List<int> &l_dummy, int * const &v)

You are trying to pass it the following:
2

2 is not a (List<int> &l_dummy, int * const &v)


While I'm here, this Stack class makes no sense at all. It doesn't seem to be a stack; it's just some inconvenient functions for a user to use with a completely separate C++ list, that they're using as a stack.
Last edited on
Topic archived. No new replies allowed.