错误 2440 无法从"int"转换为"节点"
Error 2440 cannot convert from 'int' to 'node'
好的,我正在做学校的作业。我找到了这个程序,我想用它作为一个例子来帮助我理解链表系统是如何工作的。这个特定的程序应该创建一个链表,然后要求用户输入"点",比如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; }
//...
};
相关文章:
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 将字符串中的 int 转换为字母;
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将矢量<int>转换为字符数组?
- C++ 无法从 const int* 转换为 const_iterator
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 如何将 int[4] 转换为 std::array<int,4>?
- 如何将data[i].int转换为vaible
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 将私有矩阵形式 int 转换为双 C++
- 无法将参数 1 从 'int' 转换为 'int &'
- 将 int 转换为字符串后始终得到 0
- isspace 函数的性能警告,从 int 转换为布尔值
- 将大 int 转换为浮点数,而不舍入 c++
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将未签名的INT转换为BCD
- 在 C++ 中将 int 转换为双精度