[英]Linked list, Class struct?
Can u build a struct with a class type data member?你能用一个类类型的数据成员构建一个结构吗?
I have a class file and my goal is to build a linked lints of characters each with their own names and other attributes.我有一个类文件,我的目标是构建一个链接的字符 lints,每个字符都有自己的名称和其他属性。
now to build the list i need a member that will hold the data and a node pointer my question is could i build something like this.现在要构建列表,我需要一个成员来保存数据和一个节点指针,我的问题是我可以构建这样的东西。 all this is being done in a class file and whenever i run it i get an error at the struct, BigHero doesn't name type.所有这些都是在一个类文件中完成的,每当我运行它时,我都会在结构上收到错误,BigHero 没有命名类型。
help this noob XD帮助这个菜鸟 XD
#include <string>
#include <iostream>
using namespace std;
#ifndef BIGHERO_H
#define BIGHERO_H
struct Node{
BigHero data;
Node* Next;
};
class BigHero
{
public:
BigHero();
/* Parameterized constructor
* Set private members to parameter values
* NOTE: level is initialized as the interger division of exp/100
* i.e. if exp = 250 -> level = 2 */
BigHero(string newName, int newExp, int newStr, int newIntel, int newDex);
/* Accessors: each accessor will return the value of the appropriate
private data member */
string getName() const;
int getExp() const;
int getStr() const;
int getInt() const;
int getDex() const;
int getLevel() const;
/* Mutators: each mutator will take one parameter and update the
appropriate private data member
* The domain for each mutator is listed below.
* The mutator should protect against invalid values.
* An Invalid entry should result in the original value remaining unchanged. */
void setName( string newName ); // All strings are valid
void setExp( int newExp ); // 0 <= newExp <= 9000
void setStr( int newStr ); // 0 <= newStr <= 300
void setInt( int newInt ); // 0 <= newInt <= 300
void setDex( int newDex ); // 0 <= newDex <= 300
void setLevel( int newLevel ); // 1 <= newLevel <= 100
bool addExp( int amount );
void levelUp();
bool operator<( const BigHero& rhs ) const;
bool operator>( const BigHero& rhs ) const;
bool operator==( const BigHero& rhs ) const;
friend ostream& operator<< (ostream& os, const BigHero& rhs);
/* Input should be in the format: "Name exp Str Intel Dex"
* Don't forget to update level value to be equal to the integer division of exp/100 */
friend istream& operator>> (istream& is, BigHero& rhs);
~BigHero();
private:
string name; // Hero's name
int exp; // Experience points (100 exp = 1 level)
int level; // Hero's level
int Str; // Hero's strength
int Intel; // Hero's intelligence
int Dex; // Hero's dexterity
Node* head;
Node* tail;
};
#endif // BIGHERO_H
Node
contains a BigHero
, so to allocate a Node
, Node
needs to know the size of BigHero
to be able to determine how much storage is required. Node
包含一个BigHero
,因此要分配一个Node
, Node
需要知道BigHero
的大小才能确定需要多少存储空间。 That means BigHero
must be fully defined, and it's size known, before Node
can be defined.这意味着在定义Node
之前必须完全定义BigHero
,并且它的大小是已知的。
class BigHero
{
// Rest of BigHero omitted to save space
Node* head;
Node* tail;
};
struct Node{
BigHero data;
Node* Next;
};
BigHero
needs to know that the concept of Node
exists and will be defined elsewhere to be able to contain pointers to Node
. BigHero
需要知道Node
的概念存在并将在别处定义以能够包含指向Node
指针。 This is solved with a forward definition of Node
.这可以通过Node
的前向定义解决。 So所以
struct Node;
class BigHero
{
// Rest of BigHero omitted to save space
Node* head;
Node* tail;
};
struct Node{
BigHero data;
Node* Next;
};
I suspect the problem is that the C++ compiler hasn't seen the BigHero class yet when it gets to the struct definition, so it doesn't know what to do with it.我怀疑问题在于 C++ 编译器在到达 struct 定义时还没有看到 BigHero 类,所以它不知道如何处理它。
Forward-declare the BigHero class by putting this:通过将以下内容向前声明 BigHero 类:
class BigHero;
Before the Node struct.在 Node 结构之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.