【发布时间】:2018-07-13 18:04:34
【问题描述】:
我是 C++ 初学者,我的 C++ 实验室作业有问题。 我不知道如何从指向结构的指针返回指向 int 的指针。
我的头文件
class list {
public:
/* Returns a pointer to the integer field
pointing to the first node found in list
with value val. Returns 0 otherwise */
int *find(int val);
private:
list_node *the_list;
}
我的 cpp 文件
int* list::find(int val)
{
while(the_list)
{
if(the_list->value == val)
{
// i try to return the pointer that is type pointer to int.
// the_list is a pointer to a struct type call list_node.
int * ptr = the_list;
return ptr;
}
the_list = the_list->next;
}
return 0;
}
struct list_node
{
int value; // data portion
list_node *next; // pointer next portion
list_node *previous; // pointer previous portion
};
【问题讨论】:
-
请发布可编译的代码。你错过了
;。 -
对不起,我不能发布整个代码,这只是其中的一部分。
-
然后做一个小例子。