Convertion Problem

Jun 8, 2013 at 3:46pm
Write your question here.
I am writing a home assignment. I came a cross a problem that i can't figure out how to fix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Menu::start()
{
	List<int> list;

	if(!list.isEmpty()) 
		cout<<"Error"<<endl;
	list.add_front(5);
	cout<<list.size()<<endl; //size is 1
	if(list.isEmpty()) 
		cout<<"Error"<<endl;
	cout<<list.pop_back()<<endl;
	if(!list.isEmpty()) 
		cout<<"Error"<<endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	inline void add_front(T& t){
		if(m_head ==  NULL)
		{
			m_head = &t;
			m_size += 1;
		}
		else
		{
			Node* tmp = new Node;
			tmp -> m_data = *t;
			tmp -> m_next = m_head;
			m_head -> m_prev = tmp;
			m_head = tmp;
			m_size += 1;
		}
	}


i am getting these errors and can't figure out what to do:
error C2664: 'List<T>::add_front' : cannot convert parameter 1 from 'int' to 'int &'

IntelliSense: initial value of reference to non-const must be an lvalue
Jun 8, 2013 at 4:12pm
See keskiverto's answer
Last edited on Jun 8, 2013 at 4:24pm
Jun 8, 2013 at 4:18pm
The error is clear; you cannot take a non-const address of literal 5. Literals are const, your reference is not. If your add const qualifier to the parameter t, then that should be ok.


However, you try to make the m_head point to the parameter. I bet you want a Node there.
Furthermore, you should not dereference a reference on line 10.
Topic archived. No new replies allowed.