C++ 指向另一个类中的不同类的指针
C++ Pointer to different class within another class
我一直在搜索文档并检查我的类给出的示例代码,但无法弄清楚这里出了什么问题。我正在尝试构建一个链表结构,需要可以指向节点但似乎无法正确初始化它们的指针。节点相互指向很好,是带有第一个/最后一个/当前指针的总体类给我带来了问题。
当我尝试创建节点类的指针时,下面的代码给了我一堆错误。我在第 19、20 和 21 行收到 C2238"';"前面的意外标记",以及 C2143"在"*"之前缺少';"和 C4430"缺少类型说明符..."同样是第 19、20 和 21 行(linkedList 的受保护部分(。
template <class Type>
class linkedList
{
public:
//constructors
linkedList();
//functions
void insertLast(Type data); //creates a new node at the end of the list with num as its info
void print(); //steps through the list printing info at each node
int length(); //returns the number of nodes in the list
void divideMid(linkedList sublist); //divides the list in half, storing a pointer to the second half in the private linkedList pointer named sublist
//deconstuctors
~linkedList();
protected:
node *current; //temporary pointer
node *first; //pointer to first node in linked list
node *last; //pointer to last node in linked list
bool firstCreated; //keeps track of whether a first node has been assigned
private:
};
template <class Type>
struct node
{
Type info;
node<Type> *next;
};
按如下所示更改受保护部分也会留下 C2238 和 C4430 错误,但将 C2143 更改为"<"之前缺少';">
"node<Type> *current; //temporary pointer
node<Type> *first; //pointer to first node in linked list
node<Type> *last; //pointer to last node in linked list
您需要在linkedList
之前为node
进行前向声明:
template <class Type>
struct node;
请在此处查看工作版本。
相关文章:
- C 指针和对象作为不同类的一部分
- 如何在它们之间分配和使用 2 个不同类的函数指针?
- C++ 指向另一个类中的不同类的指针
- 泛型方法指针.reinterpret_cast指向不同类的方法指针,这是 UB 吗?
- (C++) 指向不同类中对象的指针的指针
- 我可以将指针作为指针转向课程,以便我可以使用分配给同类指针的任何内容
- 函数指针数组,其中包含不同类的类函数
- 未定义的引用:试图初始化来自不同类的对象的指针数组
- 指向不同类方法的函数指针
- 链接的不同类的列表,如何使用指针访问特定类功能
- 指向从 1 个基类继承的不同类对象的指针数组
- 在成员函数中声明指向不同类的指针
- 指向相同类类型设计可行性的指针向量
- 指向不同类的成员函数的指针
- C++将动态方法指针作为不同类的方法参数传递
- 指向不同类的函数的函数指针数组
- 在不同类中使用和设置静态函数指针
- 映射不同类的成员函数指针
- C++:在不同类的派生对象的基指针上调用静态成员函数
- 重载指向两个不同类的指针的赋值运算符