【问题标题】:Linked list, Class struct?链表,类结构?
【发布时间】:2016-04-15 21:44:27
【问题描述】:

你能用类类型数据成员构建一个结构吗?

我有一个类文件,我的目标是构建一个链接的字符 lint,每个字符都有自己的名称和其他属性。

现在要构建列表,我需要一个保存数据的成员和一个节点指针 我的问题是我可以建立这样的东西。所有这些都是在一个类文件中完成的,每当我运行它时,我都会在结构中得到一个错误,BigHero 没有命名类型。

帮助这个菜鸟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

【问题讨论】:

  • #include ?
  • 这个结构在类文件中
  • 编译好的类文件?
  • 我认为您应该遵循教程。我很难看到 SO 对指导人们了解 C++ 的复杂性有什么好处
  • 让我编辑帖子...将发布整个课程文件

标签: c++ class linked-list


【解决方案1】:

Node 包含一个BigHero,所以要分配一个NodeNode 需要知道BigHero 的大小才能确定需要多少存储空间。这意味着必须完全定义BigHero,并且知道它的大小,然后才能定义Node

class BigHero
{
     // Rest of BigHero omitted to save space
     Node* head;
     Node* tail;
};

struct Node{
    BigHero data;
    Node* Next;
};

但是...

BigHero 需要知道Node 的概念存在并且将在别处定义以便能够包含指向Node 的指针。这可以通过Node 的前向定义来解决。所以

struct Node;
class BigHero
{
     // Rest of BigHero omitted to save space
     Node* head;
     Node* tail;
};

struct Node{
    BigHero data;
    Node* Next;
};

【讨论】:

  • @William "u the man" 是什么让您确定这是男性用户?我从他们的个人资料中看不到任何证据。
【解决方案2】:

我怀疑问题是 C++ 编译器在到达结构定义时还没有看到 BigHero 类,所以它不知道如何处理它。

通过这样的方式向前声明 BigHero 类:

class BigHero;

在节点结构之前。

【讨论】:

  • 错误的方法;他在 Node 中使用 BigHero 作为值,但在 BigHero 中只使用 Node 作为指针。
  • 不是说他需要BigHero的前向声明才能定义Node吗?
  • 意味着他需要将node 的定义移到BigHero 之后,并将Node 定义在BigHero 之前。
猜你喜欢
  • 1970-01-01
  • 2018-05-20
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 2011-05-30
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多