-1

次のコードをコンパイルしようとします。

#include <cppunit/extensions/HelperMacros.h>
#include "tested.h"

class TestTested : public CppUnit::TestFixture
{
        CPPUNIT_TEST_SUITE(TestTested);
        CPPUNIT_TEST(check_value);
        CPPUNIT_TEST_SUITE_END();

        public:
                void check_value();
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);

void TestTested::check_value() {
        tested t(3);
        int expected_val = t.getValue(); // <----- Line 18.
        CPPUNIT_ASSERT_EQUAL(7, expected_val);
}

その結果、次のようになります。

testing.cpp:18:32: Error: void-value is not ignored where it should be

編集

tested.h例を完成させるために、 andのコードを投稿しますtested.cpp

tested.h

#include <iostream>
using namespace std;

class tested {
    private:
        int x;
    public:
        tested(int int_x);
        void getValue();
};

tested.cpp

#include <iostream>
using namespace std;

tested::tested(int x_inp) {
    x = x_inp;
}

int tested::getValue() {
    return x;
}
4

3 に答える 3

4

テストしたクラスで宣言void getValue();します..に変更しint getValue();ます。

于 2013-04-18T09:13:22.577 に答える
1

クラス定義が実装と一致しません:

ヘッダーでは、次のように宣言しています (余談ですが、いくつかの命名規則を調べる必要があるかもしれません)。

class tested {
    private:
        int x;
    public:
        tested(int int_x);
        void getValue();
};

つまり、ノーリターンと宣言getValue()しました。voida が何も返さないのはあまり意味がありgetterませんね。

ただし、.cpp実装したファイルでは次のgetValue()ようになります。

int tested::getValue() {
    return x;
}

getValue()戻り値の型が実装 ( ) と一致するように、ヘッダー型のメソッド シグネチャを更新する必要がありますint

于 2013-04-18T09:20:17.330 に答える