错误:类型为“long *”的值无法分配给“long”类型的实体

时间:2010-12-12 08:22:27

标签: c++

所以我正在为我的C ++类做一个实验室作业,但是我坚持这个错误:

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

以下是相关代码:

//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;
  }
}

我不知道为什么它会给我这个错误。有人可以解释一下这个问题,并给我一些指示,告诉我如何解决这个问题?

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

5 个答案:

答案 0 :(得分:4)

您显然正在尝试将指针指向一长时间。

在这两种情况下,

temp_year = temp_ptr->hist_year;似乎都是错误行。 hist_year是指针吗?

您可以发布定义此history_node类型的代码吗?

答案 1 :(得分:1)

看起来hist_year struct元素定义为long *,应该只是long。您需要发布实际代码,而不是屏幕截图。

答案 2 :(得分:0)

好像你正在为长指针分配长指针。 你能不能在帖子里放几行确切的错误信息,以及你要访问的类或结构。

答案 3 :(得分:0)

如果您将指示的行更改为:

temp_year = *(temp_ptr->hist_year) ; 

毫无疑问会编译,但这并不是说它在语义上是正确的,或者在执行时不会失败。或许history_node::hist_year的成员应该被宣布为long而不是long*

无论哪种方式,错误信息都意味着它所说的内容;赋值左侧的变量与右侧变量的类型不同。解决方案是在左侧或右侧使用正确的类型;如果没有看到关于成员history_node::hist_year的使用和初始化的更多代码,那么需要纠正是不可能的。

您可能对指针类型感到困惑,编码风格的一个小改动可能会有所帮助;你在哪里:

history_node *temp_ptr;

例如,我建议你更喜欢:

history_node* temp_ptr;

强调*是类型标识符的一部分,而不是变量标识符的一部分。 history_node*是一种与history_node不同的数据类型,因为long*long不同。使指针修饰符“拥抱”它的基本类型比拥抱标识符更有意义。

答案 4 :(得分:0)

这不是答案,但我想这是注册的另一个好处 - 编辑我的帖子。我稍后会注册。

无论如何,这是代码,而且,我没有使用长*这就是为什么我这么困惑。至少,据我所知,我没有使用长*。为了澄清另一件事,代码可能有点陈旧/过时,这是因为我们的导师坚持使用一本真的很旧的书。

#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);
}
相关问题