-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.hand的代码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

void 函数不能返回值。您从 API getValue() 获得一个 int 值,因此它应该返回一个 int。

于 2013-04-18T09:19:04.437 回答
1

您的类定义与实现不匹配:

在您的标头中,您已按以下方式声明它(顺便说一句,您可能需要查看一些命名约定)。

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

您已声明getValue()void,即没有回报。gettera什么都不返回没有多大意义,不是吗?

但是,在.cpp您实现的文件中,getValue()如下所示:

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

您需要更新getValue()标头类型中的方法签名,使其返回类型与实现 ( int) 匹配。

于 2013-04-18T09:20:17.330 回答