错误 2440 无法从"int"转换为"节点"

Error 2440 cannot convert from 'int' to 'node'

本文关键字:int 转换 节点 2440 错误      更新时间:2023-10-16

好的,我正在做学校的作业。我找到了这个程序,我想用它作为一个例子来帮助我理解链表系统是如何工作的。这个特定的程序应该创建一个链表,然后要求用户输入"点",比如x和y对。它将每个"点"存储在列表中的一个节点中,然后应该能够将它们全部打印出来。当我尝试编译它时,我得到2个错误,它告诉我,我不能从'int'转换为'node',我不知道为什么它这么说。语法看起来和书里的一样,所以我一定是漏掉了一个小细节。

在一个旁注,这个程序是一个随机的东西,我发现使用作为一个例子,所以如果可能的话,谁能告诉我如何使它计算x和y轴在那里结束?在评论里写着,它仍然需要完成。

下面是完整的错误消息:(错误发生在第44行和67行,这两行都说"newNode->info = input;")

错误1错误C2440: '=':无法从'int'转换为'node *'

#include <iostream>
#include <list>
#include <cassert>
#include <ostream>

using namespace std;
struct node
{
node *info;
node *link;
int x, y; 
};
node* getXandY();
int main()
{
return 0;
}//end main
node* getXandY()
{
node *newNode, *first, *last; 
int input;
int count = 0;
cout << "Enter as many points as you like: (x,y)" << endl;
first = NULL;
do 
{
    //x integers
    cout << "Enter an X value: ";
    cin >> input;//read info
    int x = input; //store info (x) into input
    newNode = new node();//allocate memory for the type node
    assert(newNode != NULL);//terminate program is no memory space
    newNode->info = input;//copy value into input
    newNode->link = NULL;
    if (first == NULL)
    {
        first = newNode;
        last = newNode;
    }
    else
    {
        last->link = newNode;
        last = newNode;
    }

    //y integers
    cout << "Enter an Y value: ";
    cin >> input;//get info
    int y = input;//store in for (y) into input
    newNode = new node;//allocate memory for the type node
    assert(newNode != NULL);//terminate program is no memory space
    newNode->info = input;//copy value into input
    newNode->link = NULL;
    if (first == NULL)
    {
        first = newNode;
        last = newNode;
    }//end if
    else
    {
        last->link = newNode;
        last = newNode;
    }//end else 
}//end do
while (input != 999);

for (int i = 0; i < count; i++)
{
    cout << "("  /*place code here x value <<*/  "," << /*place code here y value <<*/   ")" <<endl;    
}//end for
getchar(); 
}

程序是错误的,没有意义。

至于错误,它发生在这样的语句中

newNode->info = input; 

数据成员info的类型为node *。参见info

的定义
node *info;

input的类型为int

int input;

因此编译器无法将int类型的对象赋值给node *类型的对象。此外,变量输入是一个局部变量。所以在任何情况下,这段代码都没有意义。

我将按如下方式定义列表

class list
{
public:
    typedef size_t size_type;
    typedef std::pair<int, int> value_type;
private:
   struct node
   {
      node *next;
      std::pair<int, int> point; 
   };
   node *head = nullptr, *tail = nullptr;
   size_type sz = 0;     
public:
   // Some special member functions and other methods as for example push_back or push_front
   size_type size() const { return sz; }
   //...
};