nonlvalue error

C

Christopher

I have the following errors, can someone explain how I accomplish the
pointer assignment here?

Error Message :
CQueue.cpp: In member function `void
cs3358Fall2003::circular_queue::push(const
int&)':
CQueue.cpp:63: non-lvalue in assignment
CQueue.cpp:72: non-lvalue in assignment
CQueue.cpp: In member function `void cs3358Fall2003::circular_queue::pop()':
CQueue.cpp:97: non-lvalue in assignment

// Function with errors
//////////////////////////////////////////////////////////////////////////
void circular_queue::push(const value_type& entry)
{
// If the list has at least one entry
if(rear_ptr)
{
// Insert a new node and adjust pointers
node *new_ptr = new node(entry, rear_ptr->link());
// ERROR HERE! rear_ptr->link() = new_ptr;
rear_ptr = new_ptr;
}

// Otherwise the list was empty
else
{
// Insert a new node and point to itself
rear_ptr = new node(entry, NULL);
// ERROR HERE! rear_ptr->link() = rear_ptr;
}

// Increment the count
count ++;
}

// Header File for circular_qeue
#ifndef CIRCULAR_QUEUE_H
#define CIRCULAR_QUEUE_H

#include <cstdlib> // provides size_t
#include "Node0.h"

namespace cs3358Fall2003
{

class circular_queue
{
public:
//TYPEDEF
typedef node::value_type value_type;
typedef size_t size_type;

// CONSTRUCTORS and DESTRUCTOR
circular_queue();
circular_queue(const circular_queue& source);
~circular_queue();

// MODIFICATION functions
void push(const value_type& entry);
void pop();
const circular_queue& operator=(const circular_queue& source);

// CONSTANT functions
size_type size() const;
bool empty() const;
value_type front() const;
value_type peek(size_type n) const;

private:
node *rear_ptr; // points to rear of queue
size_type count; // number of items in the queue
};
}

#endif

// Header file for Node0
#ifndef NODE0_H
#define NODE0_H

namespace cs3358Fall2003
{

class node
{
public:
// TYPEDEF
typedef int value_type;

// CONSTRUCTOR
node(const value_type& init_data = value_type(),
node* init_link = 0);

// Member functions to set the data and link fields:
void set_data(const value_type& new_data);
void set_link(node* new_link);

// Constant member function to retrieve the current data:
value_type data() const;

// Two slightly different member functions to retreive
// the current link:
const node* link() const;
node* link();

private:
value_type data_field;
node* link_field;
};
}

#endif

Thanx,
Chris
 
K

Karl Heinz Buchegger

Christopher said:
// Function with errors
//////////////////////////////////////////////////////////////////////////
void circular_queue::push(const value_type& entry)
{
// If the list has at least one entry
if(rear_ptr)
{
// Insert a new node and adjust pointers
node *new_ptr = new node(entry, rear_ptr->link());
// ERROR HERE! rear_ptr->link() = new_ptr;

Lets look at link()

node* link();

Hmm. link returns a pointer. Even if you did not show the implementation,
I guess it does:

node* node::link()
{
return link_field;
}

Right?

So what does link() do? It returns the current value in link_field.
What to do with that value? You try to assign a new value to it.
But hey, the value returned by link() is just a number, a temporary.
What good does it, if you assign a new value to it? You certainly
will not change the link_field by doing this, but that's what your
code seems to need to do.

So why not:

rear_ptr->set_link( new_ptr );
 
R

Rolf Magnus

Christopher said:
I have the following errors, can someone explain how I accomplish the
pointer assignment here?

Error Message :
CQueue.cpp: In member function `void
cs3358Fall2003::circular_queue::push(const
int&)':
CQueue.cpp:63: non-lvalue in assignment
CQueue.cpp:72: non-lvalue in assignment
CQueue.cpp: In member function `void
cs3358Fall2003::circular_queue::pop()': CQueue.cpp:97: non-lvalue in
assignment

// Function with errors
//////////////////////////////////////////////////////////////////////////
void circular_queue::push(const value_type& entry)
{
// If the list has at least one entry
if(rear_ptr)
{
// Insert a new node and adjust pointers
node *new_ptr = new node(entry, rear_ptr->link());
// ERROR HERE! rear_ptr->link() = new_ptr;

rear_ptr->link() returns a temporary node*, which is not an lvalue, i.e.
you can't assign to it. Anyway, why would you want to? The temporary is
destroyed immediately afterwards, so the assignment would be useless
anyway.
rear_ptr = new_ptr;
}

// Otherwise the list was empty
else
{
// Insert a new node and point to itself
rear_ptr = new node(entry, NULL);
// ERROR HERE! rear_ptr->link() = rear_ptr;

Same as above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top