0

So I'm doing a Lab assignment for my C++ class, but I'm stuck on this error:

a value of type "long *" cannot be assigned to an entity of type "long"

Here is the code in question:

//function that points current_ptr to node 
//before position where new should be inserted

void sort_inserted_node(long year)
{
  long temp_year;
  history_node *temp_ptr;

  if(head_ptr->next != NULL)
  {
    current_ptr = head_ptr;
    temp_ptr = current_ptr->next;
    temp_year = temp_ptr->hist_year;
    while((current_ptr->next !=NULL) && (year < temp_year))
    {
      current_ptr = temp_ptr;
      temp_ptr = current_ptr->next;
      temp_year = temp_ptr->hist_year;
    }
  }
  else
  {
     current_ptr = head_ptr;
  }
}

I don't know why it is giving me this error. Could someone please explain the problem, and give me some pointers as to how I might fix this?

Here is a screenshot of the code and the error messages in my IDE

2
  • You need to include the definition of the struct history_node in addition to the code above
    – Paul R
    Dec 12, 2010 at 9:24
  • What @Paul R says, except we can certainly infer that hist_year is declared long*; the question is why, and whether that is correct, which seems unlikely.
    – Clifford
    Dec 12, 2010 at 10:57

5 Answers 5

4

You're apparently trying to assign a pointer to a long to a long.

temp_year = temp_ptr->hist_year; seems to be the error line in both cases. Is hist_year a pointer?

Can you post the code which defines this history_node type?

1

It looks like the hist_year struct element is defined as long * and should probably just be long. You need to post the actual code though, rather than a screen shot.

0

Seems like you are assigning a long pointer to a long. Can you please put few lines in the post where exactly the error is coming and also the class or structure from which you are accessing.

0

If you change the indicated lines to:

temp_year = *(temp_ptr->hist_year) ; 

it will no doubt compile, but that is not to say that it is semantically correct or will not fail on execution. It is more likely perhaps that the member of history_node::hist_year should be declared long rather than long*.

Either way, the error message means exactly what it says; the variable on the left hand side of the assignment is not the same type as the variable on the right hand side. The solution is to use the correct type for either the left or right hand side; which needs correction is impossible to tell without seeing more of the code regarding the use and initialisation of the member history_node::hist_year.

You may be confused about pointer types, one small change in your coding style may help; where you have:

history_node *temp_ptr;

for example, I suggest you prefer:

history_node* temp_ptr;

which emphasises that the * is part of the type identifier, not part of the variable identifier. history_node* is a data type distinct from history_node as long* is distinct from long. Making the pointer modifier 'cuddle' its base type makes far more sense than cuddling the identifier.

0

Well this isn't an answer, but I guess that's another benefit of registering - editing my post. I'll register later.

Anyway, here is the code and also, I'm not using long* which is why I was so confused. At least, as far as I know I'm not using long*. And to clarify another thing, the code may be a bit old/outdated, that's because our instructor insisted on using a book that's really old.

#include <iostream>
#include <iomanip>
using namespace std;

const long length = 4; // prevents years larger than 4 figures from being entered
long hist_year[length], hist_event;

// history_node which contains the year and a historical event
struct history_node
{
    long hist_year[length];
    long hist_event;
    history_node *next;
};

history_node *head_ptr;
history_node *current_ptr;

// prototypes
void insert_node(long hist_year[length], long hist_event);
void sort_inserted_node(long hist_year[length]);
void make_node_new_head(history_node *new_rec_ptr);
void move_current_to_end();
void display_list();
void delete_list();

int main()
{
    long year[length], h_event;

    if(get_history_data(year, hist_event))
    {
        head_ptr = new history_node;
        head_ptr->hist_year, year;
        head_ptr->hist_event = h_event;
        head_ptr->next = NULL;

        while(get_history_data(year, h_event))
        {
            insert_node(year, h_event);
        }
        display_list();
        delete_list();
    }

    system("pause");
    return 0;
}

// function that gets data from user
int get_history_data(long year[length], long &h_event)
{
    int keep_data = 1;

    cout << "Enter the year (Press Enter alone to stop): ";
    cin >> *year;
    cin.ignore(80,'\n');
    if(year[0] != 0)
    {
        cout << "Enter the historical event: ";
        cin >> h_event;
        cin.ignore(80,'\n');
    }
    else
    {
        keep_data = 0;
    }
    return(keep_data);
}

// function that inserts new node sorted by year
void insert_node(long year[length], long h_event)
{
    history_node *new_rec_ptr;
    history_node *before_ptr; 
    history_node *after_ptr;

    new_rec_ptr = new history_node;

    new_rec_ptr->hist_event, h_event;
    new_rec_ptr->hist_year, year;

    if(year > head_ptr->hist_year)
    {
        make_node_new_head(new_rec_ptr);
    }
    else
    {
        sort_inserted_node(year);

        before_ptr = current_ptr;
        after_ptr = current_ptr->next;

        before_ptr->next = new_rec_ptr;
        new_rec_ptr->next = after_ptr;
    }
}

// function that points current_ptr to node before position where new should be inserted
void sort_inserted_node(long year)
{
    long temp_year;
    history_node *temp_ptr;

    if(head_ptr->next != NULL)
    {
        current_ptr = head_ptr;
        temp_ptr = current_ptr->next;
        temp_year = temp_ptr->hist_year;
        while((current_ptr->next !=NULL) && (year < temp_year))
        {
            current_ptr = temp_ptr;
            temp_ptr = current_ptr->next;
            temp_year = temp_ptr->hist_year;
        }
    }
    else
    {
        current_ptr = head_ptr;
    }
}

// function makes node new head of linked list
void make_node_new_head(history_node *new_rec_ptr)
{
    new_rec_ptr->next = head_ptr;
    head_ptr = new_rec_ptr;
}

// function that moves current_ptr to end of list
void move_current_to_end()
{
    current_ptr = head_ptr;

    while(current_ptr->next != NULL)
    {
        current_ptr = current_ptr->next;
    }
}

// function displays list
void display_list()
{
    current_ptr = head_ptr;
    do
    {
        cout.setf(ios::left);
        cout << setw(25) << current_ptr->hist_event;
        cout.setf(ios::right);
        cout    << setw(12) << current_ptr->hist_year << endl;
        current_ptr = current_ptr->next;
    } 
    while(current_ptr != NULL);
}

// function frees memory used by linked list
void delete_list()
{
    history_node *temp_ptr;

    current_ptr = head_ptr;

    do
    {
        temp_ptr = current_ptr->next;
        delete current_ptr;
        current_ptr = temp_ptr;
    } 
    while(temp_ptr != NULL);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.