1

私はC++のクラスを理解し、Pythonで見たいくつかの同様のクラスを開発しようとしています。コードは次のとおりです。

#include <iostream>
#include <cmath>
using namespace std;

/*============================================================================*/
/* Define types
/*============================================================================*/
class none_type;
class bool_type;
class int_type;
struct identifier;

/*============================================================================*/
/* Define none type
/*============================================================================*/
class none_type {
  public:
    none_type()                  { /* constructor */ };
    ~none_type()                 { /* destructor */ };
}; /* none_type */

/*============================================================================*/
/* Define bool type
/*============================================================================*/
class bool_type {
  private:
    bool base;
  public:
    bool_type()                  { base = false; };
    ~bool_type()                 { /* destructor */ };
    bool_type(bool init)         { base = bool(init); };
    bool_type(int init)          { base = bool(init); };
    bool_type(long init)         { base = bool(init); };
    bool_type(float init)        { base = bool(init); };
    bool_type(double init)       { base = bool(init); };
    bool_type(bool_type init)    { base = bool(init.base); };
    bool_type(int_type init)     { base = bool(init.base); };
    int get()                    { cout << base << endl; };
}; /* bool_type */

/*============================================================================*/
/* Define int type
/*============================================================================*/
class int_type {
  private:
    long base;
  public:
    int_type()                   { base = 0; };
    ~int_type()                  { /* destructor */ };
    int_type(bool init)          { base = long(init); };
    int_type(int init)           { base = long(init); };
    int_type(long init)          { base = long(init); };
    int_type(float init)         { base = long(init); };
    int_type(double init)        { base = long(init); };
    int_type(bool_type init)     { base = long(init.base); };
    int_type(int_type init)      { base = long(init.base); };
    int get()                    { cout << base << endl; };
}; /* int_type */

コンパイルしようとすると、g++自分の型を使用するすべてのコンストラクターが無効であることがわかります。何が悪いのか説明してもらえますか?クラスのプロトタイプを定義しましたが、他に何をすべきですか?前もって感謝します!

4

3 に答える 3

3

このコンストラクター:

bool_type(int_type init)     { base = bool(init.base); };

この時点でint_typeが不完全であるため、は無効です。

このコンストラクターの実装をクラス定義から移動して、int_typeが完了するポイントに移動する必要があります。

class bool_type {
  bool_type(int_type init);
};
class int_type {};
inline bool_type::bool_type(int_type init)     { base = bool(init.base); };

もう1つの問題は、コピーコンストラクターのふりをするコンストラクターにあります。

bool_type(bool_type init)    { base = bool(init.base); };

ここでは、無限再帰initがあります-パラメーターはコピーであるため-このコピーを作成するには、このコンストラクターを呼び出す必要がありますが、このコンストラクターの次の呼び出しには、init無限またはスタック制限にコピーする必要がある独自のパラメーターがあります。 。

コピーコンストラクタの適切な定義は次のとおりです。

bool_type(const bool_type& init)    { base = bool(init.base); };

Const参照を使用する必要がありますが、これではコンパイラーに依存できます。つまり、コピーコンストラクターが生成されるため、完全に削除するだけです。

于 2012-10-28T01:08:05.127 に答える
2

G ++はすでに何が問題なのかを教えてくれます:

エラー: コンストラクターが無効です。あなたはおそらく「bool_type (const bool_type&)」を意味していました

代わりにbool_type (bool_type)を使用する必要がありますbool_type (const bool_type&)。これは、オブジェクトを値渡しすると、コンパイラがコピー コンストラクターを使用してオブジェクトをスタックに置くためです。したがって、をコピー コンストラクターに渡すにはbool_typebool_type(bool_type)コピー コンストラクター自体を使用する必要があります。それは不可能です。

についても同様ですint_type(int_type)

コンストラクター 'bool_type::bool_type(int_type)': エラー: 'init' の型が不完全です

この時点で、G++ は がどのようにint_type見えるかわかりません。int_typeメンバーがいるか分からないのでbase使えません。コンストラクタを宣言するだけです:

bool_type(int_type init);

の宣言の後に定義しますint_tpye

....
class int_type {
....
};

...
inline bool_type(int_type init) { base = bool(init.base); }

大きなオブジェクトがある場合は、参照渡しを使用することをお勧めします。値渡しはオブジェクトをスタックにコピーすることを意味するためです。これは、この大きなオブジェクトへの参照を渡すだけよりもはるかにコストがかかります (大きなオブジェクトの場合)。小さなオブジェクトの場合、これはそれほど重要ではありません。

そして最後のエラー:

コンストラクター 'int_type::int_type(bool_type)': エラー: 'bool bool_type::base' はプライベートです

baseのメンバーをbool_typeとして宣言しましたprivate:。つまり、bool_typeこのメンバーへのアクセスのみが許可されます。手に入れるには、base次のアクセス方法を使用する必要がありますget()

int_type(bool_type init)     { base = long(init.get()); }

同様に、次を定義する必要があります。

inline bool_type(int_type init) { base = bool(init.get()); }

最後に、またはc++を見て、本のリストに従ってください。C++ FAQもかなりまともです。

編集:あなたのget()メソッドがアクセサではないことを見逃しました。それらは次のように定義する必要があります。

class bool_type {
public:
    bool get() const { return base; }
...
};

同じint_type::get()

int get() const { return base; }
于 2012-10-28T11:39:04.770 に答える
1

「LearnC++」以外に具体的なアドバイスを与えるのは難しいですが、「通常の」C++でクラスを設計する方法は次のとおりです。

class int_type;

class none_type { };

class bool_type
{
    bool base;
public:
    bool_type() : base() { }
    explicit bool_type(bool init) : base(init) { }
    explicit bool_type(int_type const &);

    void print() const { std::cout << base << std::endl; }
    bool get() const { return base; }
};

class int_type
{
    int base;
public:
    int_type() : base() { }
    explicit int_type(int init) : base(init) { }
    explicit int_type(bool_type const & init) : base(init.get() ? 1 : 0) { }

    void print() const { std::cout << base << std::endl; }
    int get() const { return base; }
};

inline bool_type::bool_type(int_type const & init) : base(init.get()) { }
于 2012-10-28T01:08:56.783 に答える