0

私は自分でオライリーの教科書を完成させており、現在はクラスに参加しています。以下は、単語ごとにコピーされたプログラミング演習です。

"演習 13-1: パリティ クラスを記述します。このクラスは put という名前のメンバー関数を提供します。このメンバー関数は、提供された要素の数をカウントします。もう 1 つのメンバー関数 test は、put 呼び出しが偶数回行われた場合は true を返し、それ以外の場合は false を返します。メンバーfunctions: void parity::put( );// 別の要素をカウントします bool parity::test( );//偶数回trueのプットが行われた場合に戻りますfalse。奇数の場合に戻ります。"

私は自分のプログラムについて正しい考えを持っていると信じています。完了しましたが、エラーはありませんが、正しく動作せず、ブレークポイントがあります。これが私のコードです:

#include <iostream>
#include <cstdlib>
class parity {

    private:
        int count; //number of puts done.

    public:
        void init();//initialize the put
        void put(const int item); //count another element
        bool test( ); //return true of even number of puts have been done. Return false for an odd number
};

inline void parity::init()
{
    count = 0;
}

inline void parity::put(const int item)
{
    count = item;
    ++count;
}

inline bool parity::test()
{
    if (count % 2 == 0)
    {
        return true;
        std::cout << "Amount of puts is even";
    }
    else
    {
        return false;
        std::cout << "Amount of puts is not even";
    }
}

int main ()
{
    parity a_parity;

    a_parity.init();

    a_parity.put(1);
    a_parity.put(2);
    a_parity.put(3);

    a_parity.test();

    return 0;
}

エラーはparity::putメンバー関数にあると思いますが、わかりません。どんな提案でも大歓迎です。ありがとう

4

1 に答える 1

2

count = item;が問題です。

inline void parity::put(const int item)
{
    // count = item; 
    ++count;
}

countを呼び出すたびに の値をリセットしないため、動作するはずですput

于 2013-06-01T04:42:14.920 に答える