c++ - 函数返回结构体指针

标签 c++ class

我在返回指向类内声明的结构的指针的代码时遇到一些问题。这是到目前为止我的代码:

排序列表.h

#ifndef SORTEDLIST_H
#define SORTEDLIST_H

class SortedList{

 public:

    SortedList();

 ...

 private:

    struct Listnode {    

      Student *student;

      Listnode *next;

    };

    static Listnode *copyList (Listnode *L);

};

#endif

排序列表.cpp

#include "SortedList.h"

...

// Here is where the problem lies

Listnode SortedList::*copyList(Listnode *L)

{

    return 0; // for NULL

}

显然,复制列表方法无法编译。我正在使用 Microsoft Visual Studio,编译器告诉我“Listnode”无法识别。当我尝试编译时,我得到的是:

1>------ Build started: Project: Program3, Configuration: Debug Win32 ------

1>  SortedList.cpp

sortedlist.cpp(159): 错误 C2657: 在语句开头找到“SortedList::*”(您是否忘记指定类型?)

sortedlist.cpp(159):错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持default-int

sortedlist.cpp(159): 错误 C2065: 'L': 未声明的标识符

sortedlist.cpp(159):错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持default-int

sortedlist.cpp(159): fatal error C1903: 无法从之前的错误中恢复;停止编译

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我们将不胜感激...尽快

最佳答案

在cpp文件中,函数应定义为:

SortedList::Listnode* SortedList::copyList(ListNode* L)
{
    return 0; //For NULL
}

此外,struct Listnode 应该声明为 public 或在 SortedList 类 之外。

关于c++ - 函数返回结构体指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13355776/

相关文章:

class - Java将具有逗号分隔值的字符串列表转换为对象列表

python - 如何在命令行上测试 python 代码

c++ - 如何在 C++ 中检查空类

c++ - 我应该如何检查基础类型值是否为枚举值?

c# - 将数组从非托管 C++ 传递到 C#

class - 在类和结构之间选择

c++ - 'ifstream' 的初始化没有匹配的构造函数

c++ - 显式使用 main 中的构造函数调用作为函数调用参数

c++ - 在 C++ 中定义类枚举值的 std::vector 的缩写语法

c++ - 为什么我的打印功能不起作用?链表