在课堂上做一个结构

Making a structure in class

本文关键字:一个 结构 课堂      更新时间:2023-10-16

我正在迈出学习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;
   } 
};
struct data{
    int q ;
    int w;
};

仅声明类型,而不是对象。因此,在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方法应该做什么,所以我没有替代品。)

在C++中,"class"和"struct"几乎是同义词(相同的东西)。唯一的区别是"struct"默认为"public"可访问性,而"class"默认为private。

一旦你理解了这一点,你所做的就是在你的类中定义一个子类型。

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&如果你打算在类的范围之外重用该结构定义,例如在派生类或并行类中,你可能想添加更多相同的类型的东西,在这种情况下,你可能会做以下事情,但如果你这样做,你最终会因为破坏封装而自责:

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++:样板语言。

您已经定义了结构,但没有该类型的对象。您应该声明一个对象,这样就不会出现任何错误。

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;
   } 
};
相关文章: