简体   繁体   English

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

[英]Error: a value of type “long *” cannot be assigned to an entity of type “long”

So I'm doing a Lab assignment for my C++ class, but I'm stuck on this error: 因此,我正在为我的C ++类进行Lab作业,但是我陷入了这个错误:

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 这是我的IDE中的代码和错误消息的屏幕截图

You're apparently trying to assign a pointer to a long to a long. 您显然正在尝试将指针分配给long到long。

temp_year = temp_ptr->hist_year; seems to be the error line in both cases. 在这两种情况下似乎都是错误行。 Is hist_year a pointer? hist_year是指针吗?

Can you post the code which defines this history_node type? 您可以发布定义此history_node类型的代码吗?

It looks like the hist_year struct element is defined as long * and should probably just be long . 看起来hist_year struct元素定义为long *并且可能应该只是long You need to post the actual code though, rather than a screen shot. 但是,您需要发布实际的代码,而不是屏幕截图。

Seems like you are assigning a long pointer to a long. 似乎您正在将long指针分配给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. 您能在帖子中写几行来确切地指出错误所在,以及您正在访问的类或结构。

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* . 更有可能应该将history_node::hist_year的成员声明为long而不是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 . 没有看到更多有关成员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 . history_node*是一种数据类型不同于history_node作为long*是从不同的long Making the pointer modifier 'cuddle' its base type makes far more sense than cuddling the identifier. 将指针修饰符“拥抱”为其基本类型比拥抱标识符更有意义。

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. 无论如何,这是代码,而且,我没有使用long *,这就是为什么我如此困惑的原因。 At least, as far as I know I'm not using long*. 至少据我所知,我没有使用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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法将类型“ double *”的值分配给类型“ double”的实体 - Error: a value of type “double*” cannot be assigned to an entity of type “double” 如何修复类型的值不能分配给错误类型的实体? - How to fix Value of type cannot be assigned to entity of type ERROR? 不能将类型为“ Struct *”的值分配给类型为“ Struct *”的实体 - a value of type “Struct*” cannot be assigned to an entity of type “Struct*” “const char *”类型的值不能分配给“LPSTR”类型的实体 - Value of type "const char *" cannot be assigned to an entity of type "LPSTR" IntelliSense:不能将类型为“ void”的值分配给类型为“ double”的实体 - IntelliSense: a value of type “void” cannot be assigned to an entity of type “double” IntelliSense:无法将类型为“ IDeviceServer *”的值分配给类型为“ IDeviceServer *”的实体 - IntelliSense: a value of type “IDeviceServer *” cannot be assigned to an entity of type “IDeviceServer *” “void *”类型的值不能分配给“double *”类型的实体 - A value of type "void *" cannot be assigned to an entity of type "double *" 不能将“void”类型的值分配给 CPP 中“int”类型的实体 - a value of type “void” cannot be assigned to an entity of type “int” in CPP 不能将 const char* 类型的值分配给 char* 类型的实体? - A value of type const char* cannot be assigned to an entity of type char*? “void *”类型的值不能分配给“TCHAR”类型的实体 - a value of type “void *” cannot be assigned to an entity of type “TCHAR”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM