2010-12-12 59 views
0

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

回答

4

你顯然是想一個指針賦給長到長。

temp_year = temp_ptr->hist_year;似乎是兩種情況下的錯誤行。指針是hist_year嗎?

您能否發佈定義此history_node類型的代碼?

0

好像你正在分配一個長指針。 您可以請在錯誤即將到來的帖子中填寫幾行內容,以及您正在訪問的類別或結構。

1

它看起來像hist_year結構元素被定義爲long *,應該可能只是long。您需要發佈實際的代碼,而不是屏幕截圖。

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。使指針修飾符'擁抱'的基本類型比擁抱標識符更有意義。

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); 
} 
相關問題