【发布时间】:2017-08-12 18:07:58
【问题描述】:
我用 C++ 编写了以下代码。函数“change_list”的声明给出了一个错误: “不允许不完整的类型” 并且“标识符”列表“未定义”。你知道为什么吗? 我只是按照这篇文章中的答案:C++ pass list as a parameter to a function
#include <iostream>
#include <list>
typedef struct Point {
int x;
int y;
} Point;
Point* initPoint(int x, int y) {
Point* pt = (Point*)malloc(sizeof(Point));
pt->x = x;
pt->y = y;
return pt;
}
void change_list(list<Point*> &mylist){ // ERROR!!!
mylist.push_back(4);
}
int main()
{
using namespace std;
list<Point*> c1; // initiating a list
c1.push_back(initPoint(1, 1));
c1.push_back(initPoint(2, 2));
c1.push_back(initPoint(3, 3));
change_list(c1);
for (Point* c : c1)
cout << " " << c->x;
cout << "\n";
return 0;
}
【问题讨论】:
-
试试
std::list。 -
Point* pt = (Point*)malloc(sizeof(Point));介于危险和错误之间 - 为什么使用 C 样式分配而不是 C++new? (而且你有一个内存泄漏,因为你从来没有释放分配的内存) -
@songyuanyao,谢谢。请把它写成答案,我会接受。