错误:类型“长*”不能分配给类型的实体“长”

问题描述:

所以我做了我的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

+0

您需要包括结构的定义'history_node'除了上面的代码 – 2010-12-12 09:24:00

+0

@保罗 - [R说什么,但我们当然可以推断出'hist_year'声明'长*';问题是为什么,以及这是否正确,这似乎不大可能。 – Clifford 2010-12-12 10:57:37

你显然是想一个指针赋给长到长。

temp_year = temp_ptr->hist_year;似乎是两种情况下的错误行。指针是hist_year吗?

您能否发布定义此history_node类型的代码?

好像你正在分配一个长指针。 您可以请在错误即将到来的帖子中填写几行内容,以及您正在访问的类别或结构。

它看起来像hist_year结构元素被定义为long *,应该可能只是long。您需要发布实际的代码,而不是屏幕截图。

如果更改指示行:

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。使指针修饰符'拥抱'的基本类型比拥抱标识符更有意义。

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

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

#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); 
}