如何在定义类之前使用它?

时间:2021-11-19 21:33:25
class Node
{
    string name;
    Node previous;
};

 Error: Node::previous uses "Node" which is being defined.

How can I get this to work in C++? It works in C#.

如何让它在c++中工作?它在c#中工作。

EDIT:

编辑:

Why Node* previous works?

为什么节点*以前作品?

6 个解决方案

#1


10  

Use pointers. Node* previous; would solve the problem.

使用指针。节点*之前;将解决这个问题。

As you're doing it now, you actually try to make your class infinitely large.

当你现在这样做的时候,你实际上是想让你的类无限的大。

#2


8  

Use a pointer or a reference.

使用指针或引用。

For example:

例如:

Node* previous;

The only restriction is that a class cannot have an actual field of itself, likely for construction reason. Think about it, if every Box contains another Box inside it, how would you end up with a finite number of boxes?

唯一的限制是一个类不能有它自己的实际字段,很可能是由于构造原因。想想看,如果每个盒子里都有另一个盒子,你怎么能得到一个有限的盒子呢?

If I'm not mistaken, C# only uses references for class types (as does Java), so you're using a reference there, just not seeing it.

如果我没有弄错的话,c#只使用类类型的引用(和Java一样),所以您在那里使用引用,只是没有看到它。

#3


5  

For the edit, think about it, your node contains another node into it which contains another one inside. So you have an infinite sequence of nodes contained in one another. What you want is a pointer to the location where the next node is stored. So you should change it to Node*.

对于编辑,考虑一下,您的节点包含另一个节点,其中包含另一个节点。所以你有一个无限的节点序列包含在另一个中。您需要的是指向下一个节点存储位置的指针。所以你应该把它改成Node*。

#4


2  

In C#, declaring previous like that gives you a reference. In C++, declaring previous like that gives you an object, embedded in each object of class Node.

在c#中,声明前面的内容可以为您提供参考。在c++中,像这样声明before就会得到一个对象,嵌入到类节点的每个对象中。

An object can't have an object of the same class embedded in it, recursively. previous.name would have to exist (and have storage allocated to it), as would previous.previous.name, and so on infinitely, all in a single memory allocation. The size of a Node object would have to be at least as much as the size of a string object plus the size of a Node object (which is impossible since string is more than 0 bytes). And other such contradictions.

对象不能递归地将同一个类的对象嵌入其中。name必须存在(并有分配给它的存储),正如previous.previous.name,等等,所有这些都在一个内存分配中。节点对象的大小必须至少等于字符串对象的大小加上节点对象的大小(这是不可能的,因为字符串大于0字节)。等矛盾。

As others say, use a pointer: Node *previous or a reference Node &previous. Or a std::deque<std::string> ;-)

正如其他人所说,使用指针:Node *previous或reference Node &previous。或std::双端队列< std::string >;-)

You might like to know that C++ differs from C# in other ways which will spoil your day if you're trying to write a singly-linked list by porting C# code. No garbage collection, for example. You can't write correct C++ code without understanding the C++ object model.

您可能想知道c++与c#的不同之处在于,如果您试图通过移植c#代码来编写一个单独链接的列表,那么它将破坏您的一天。例如,没有垃圾收集。如果不理解c++对象模型,就不能编写正确的c++代码。

#5


2  

Think about a Car object "in the real world". It can contain a reference to another Car object via the VIN number. It could even contain a reference to itself, or to the VIN value of 0 (NULL, or NO CAR).

想想“现实世界”中的汽车物体。它可以通过VIN号包含对另一个Car对象的引用。它甚至可以包含对自身的引用,或对0的VIN值(NULL,或NO CAR)的引用。

But what if you literally wanted another Car OBJECT inside every Car Object? How would that work? What Car object would the containing Car object contain?

但是,如果你真的想要在每个车里都有一个车的物体呢?如何工作?包含汽车的物体会包含什么?

Java and C# don't actually place THE OBJECT inside the containing class, but a REFERENCE to the object. Languages like C++ allow you to specify whether there is an OBJECT contained within the class, a REFERENCE to another OBJECT contained within the class, or even a POINTER to another OBJECT contained within the class:

Java和c#实际上并没有将对象放在包含的类中,而是对象的引用。c++等语言允许您指定类中是否包含一个对象、类中包含的另一个对象的引用,甚至是类中包含的另一个对象的指针:

class Car
{
   Engine engine;  // Car CONTAINS an Engine object
};

class Car
{
   Engine &engine;  // Car contains a REFERENCE to an Engine object
};

class Car
{
  Engine *pEngine;  // Car contains a POINTER to an Engine object
};

Eventually you'll get to the point where you know when and why to use the first, the second, or the third, but that's a different question altogether.

最终,你会知道什么时候以及为什么要使用第一个、第二个或第三个,但这是一个完全不同的问题。

#6


1  

Shouldn't Node be a pointer?

节点不应该是指针吗?

#1


10  

Use pointers. Node* previous; would solve the problem.

使用指针。节点*之前;将解决这个问题。

As you're doing it now, you actually try to make your class infinitely large.

当你现在这样做的时候,你实际上是想让你的类无限的大。

#2


8  

Use a pointer or a reference.

使用指针或引用。

For example:

例如:

Node* previous;

The only restriction is that a class cannot have an actual field of itself, likely for construction reason. Think about it, if every Box contains another Box inside it, how would you end up with a finite number of boxes?

唯一的限制是一个类不能有它自己的实际字段,很可能是由于构造原因。想想看,如果每个盒子里都有另一个盒子,你怎么能得到一个有限的盒子呢?

If I'm not mistaken, C# only uses references for class types (as does Java), so you're using a reference there, just not seeing it.

如果我没有弄错的话,c#只使用类类型的引用(和Java一样),所以您在那里使用引用,只是没有看到它。

#3


5  

For the edit, think about it, your node contains another node into it which contains another one inside. So you have an infinite sequence of nodes contained in one another. What you want is a pointer to the location where the next node is stored. So you should change it to Node*.

对于编辑,考虑一下,您的节点包含另一个节点,其中包含另一个节点。所以你有一个无限的节点序列包含在另一个中。您需要的是指向下一个节点存储位置的指针。所以你应该把它改成Node*。

#4


2  

In C#, declaring previous like that gives you a reference. In C++, declaring previous like that gives you an object, embedded in each object of class Node.

在c#中,声明前面的内容可以为您提供参考。在c++中,像这样声明before就会得到一个对象,嵌入到类节点的每个对象中。

An object can't have an object of the same class embedded in it, recursively. previous.name would have to exist (and have storage allocated to it), as would previous.previous.name, and so on infinitely, all in a single memory allocation. The size of a Node object would have to be at least as much as the size of a string object plus the size of a Node object (which is impossible since string is more than 0 bytes). And other such contradictions.

对象不能递归地将同一个类的对象嵌入其中。name必须存在(并有分配给它的存储),正如previous.previous.name,等等,所有这些都在一个内存分配中。节点对象的大小必须至少等于字符串对象的大小加上节点对象的大小(这是不可能的,因为字符串大于0字节)。等矛盾。

As others say, use a pointer: Node *previous or a reference Node &previous. Or a std::deque<std::string> ;-)

正如其他人所说,使用指针:Node *previous或reference Node &previous。或std::双端队列< std::string >;-)

You might like to know that C++ differs from C# in other ways which will spoil your day if you're trying to write a singly-linked list by porting C# code. No garbage collection, for example. You can't write correct C++ code without understanding the C++ object model.

您可能想知道c++与c#的不同之处在于,如果您试图通过移植c#代码来编写一个单独链接的列表,那么它将破坏您的一天。例如,没有垃圾收集。如果不理解c++对象模型,就不能编写正确的c++代码。

#5


2  

Think about a Car object "in the real world". It can contain a reference to another Car object via the VIN number. It could even contain a reference to itself, or to the VIN value of 0 (NULL, or NO CAR).

想想“现实世界”中的汽车物体。它可以通过VIN号包含对另一个Car对象的引用。它甚至可以包含对自身的引用,或对0的VIN值(NULL,或NO CAR)的引用。

But what if you literally wanted another Car OBJECT inside every Car Object? How would that work? What Car object would the containing Car object contain?

但是,如果你真的想要在每个车里都有一个车的物体呢?如何工作?包含汽车的物体会包含什么?

Java and C# don't actually place THE OBJECT inside the containing class, but a REFERENCE to the object. Languages like C++ allow you to specify whether there is an OBJECT contained within the class, a REFERENCE to another OBJECT contained within the class, or even a POINTER to another OBJECT contained within the class:

Java和c#实际上并没有将对象放在包含的类中,而是对象的引用。c++等语言允许您指定类中是否包含一个对象、类中包含的另一个对象的引用,甚至是类中包含的另一个对象的指针:

class Car
{
   Engine engine;  // Car CONTAINS an Engine object
};

class Car
{
   Engine &engine;  // Car contains a REFERENCE to an Engine object
};

class Car
{
  Engine *pEngine;  // Car contains a POINTER to an Engine object
};

Eventually you'll get to the point where you know when and why to use the first, the second, or the third, but that's a different question altogether.

最终,你会知道什么时候以及为什么要使用第一个、第二个或第三个,但这是一个完全不同的问题。

#6


1  

Shouldn't Node be a pointer?

节点不应该是指针吗?