【问题标题】:Making a structure in class在课堂上制作结构
【发布时间】:2013-06-22 18:37:56
【问题描述】:

我正在迈出学习 OOP 的第一步。这是我无法解决的第一个问题。 此类中的 max 函数应返回两个数字中的最大值。我想将数字保留在私有范围内,将功能保留在公共范围内。但是当我想在公共范围内使用来自struct data{} 的变量时,编译器会说这些变量没有声明。请告诉我为什么会出现这些错误。

class myclass{
private:
   struct data{
       int q ;
       int w;
   };

public:
   void get(int a, int b){
      struct data = {a , b}; // here I want to pass the variables to data struct
   }
   int max (){              // this function returns the biggest number 
      if(q>w)
         return q;
         else 
            return w;
   } 

};

【问题讨论】:

  • 你为什么要在课堂上这样做?我想一个函数模板会更合适。无论您需要data 结构的instance 作为myclass 的成员变量;不作为 get() 函数的局部范围变量。
  • 我在课堂上这样做只是为了了解和了解它是如何工作的。我知道它可以用一行来写:D

标签: c++ class constructor


【解决方案1】:
struct data{
    int q ;
    int w;
};

只声明一个type,而不是一个object,所以在class 实例中的任何地方都没有qw 成员。您需要声明struct实例

struct {
     int q;
     int w;
} data;

那么,你可以把max写成:

int max()
{
    if (data.q > data.w)
        return data.q;
    else 
        return data.w;
}

(我不知道你的 get 方法应该做什么,所以我没有替代品。)

【讨论】:

  • get 看起来应该改为 set ;)
【解决方案2】:

在 C++ 中,“类”和“结构”几乎是同义词(同一件事)。唯一的区别是“结构”默认为“公共”可访问性,而“类”默认为私有。

一旦你理解了这一点,你所做的就是在你的类中定义一个子类型应该会变得很明显。

class myclass {
private: // <- not required, you already said that by saying "class".
    struct data {
        // <-- this is a class definition with "public:" just here.
        ...
    };
};

C++ 允许您嵌套类/结构定义,例如,您可以创建编组参数或返回值的结构。

class Database {
    class Result { ... };
};

...

class Exam {
    class Result { ... };
};

这两个结果类避免了命名空间冲突,因为它们是 Database::Result 和 Exam::Result 而不仅仅是“Result”。

但是 - 这些只是定义。如图所示,它们不会对外围类产生任何影响,即:它们不被用于向类添加成员。

您的代码:

class myclass{
private:
   struct data{  // <-- this is a TYPE declaration, struct myclass::data
       int q ;   //
       int w;    // 
   };            // <-- no member name here so does not affect myclass itself.

public:
   void get(int a, int b){
      struct data = {a , b}; // here I want to pass the variables to data struct
   }
   int max (){              // this function returns the biggest number 
      if(q>w)
         return q;
         else
            return w;
   }

};

声明类型“myclass::data”但不向类添加类型“myclass::data”的成员。 “struct data =”这一行是非法的,您正在尝试为 TYPE 赋值。

应该写成

class MyClass {
    int m_q;
    int m_w;

public:
    void set(int q, int w) {
        m_q = q;
        m_w = w;
    }

    int max() const {
        return (m_q > m_w) ? m_q : m_w;
        // or #include <algorithm> and return std::max(m_q, m_w);
    }
};

如果您要在类范围之外重用该结构定义,则只需将 q & w 提升到结构中,例如在派生类或并行类中,您可能希望添加更多相同类型的事物,在这种情况下,您或许可以执行以下操作,但如果您这样做this你最终会因为打破封装而责备自己的确切方式:

class MyClass {
public:
    struct Data {
        int m_q;
        int m_w;
    };

private:
    Data m_data;

    void set(int q, int w) {
        m_data.m_q = q;
        m_data.m_w = w;
    }

    int max() const {
        return (m_data.m_q > m_data.m_w) ? m_data.m_q : m_data.m_w;
    }
};

如果这种成员耦合需要在某种程度上在外部可见,更好的方法是:

class MyClass {
public:
    class Data {
        int m_q;
        int m_w;
    public:
        Data() : m_q(0), m_w(0) {}
        Data(int q, int w) : m_q(0), m_w(0) {}

        void set(int q, int w) {
            m_q = w;
            m_w = w;
        }

        int q() const { return m_q; }
        int w() const { return m_w; }

        int max() const { return (m_q > m_w) ? m_q : m_w;
    };

private:
    Data m_data;

public:
    MyClass() : m_data() {} // or = default
    MyClass(int q, int w) : m_data(q, w) {}
    MyClass(const Data& data) : m_data(data) {}

    // Read-only access
    const Data& data() const { return m_data; }
    // To allow write access, e.g. for set:
    Data& data() { return m_data; }
};

对于这样一个简单的案例来说有点矫枉过正,但欢迎使用 C++:样板语言。

【讨论】:

  • np - 修正了一些错别字等 - 希望他们澄清
【解决方案3】:

您已定义结构,但没有该类型的对象。你应该声明一个对象,你不会得到任何错误。

class myclass{
private:
   struct data{
       int q ;
       int w;
   }var;

public:
   void get(int a, int b){
     var .q= a;
    var.w=b; // here I want to pass the variables to data struct
   }
   int max (){              // this function returns the biggest number 
      if(var.q>var.w)
         return var.q;
         else 
            return var.w;
   } 

};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-22
    • 2017-03-10
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多