错误是什么意思"void-value is not ignored"以及如何删除它?

What does "void-value is not ignored" error mean and how to remove it?

本文关键字:何删除 删除 void-value 意思 是什么 is not 错误 ignored      更新时间:2023-10-16

我尝试编译以下代码:

#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

EDDIT

为了使示例完整,我发布了tested.htested.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;
}

您在测试的类中声明void getValue();。。变为CCD_ 6。

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

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

在您的头中,您以以下方式声明了它(顺便说一句,您可能需要研究一些命名约定(。

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

您已将getValue()声明为void,即不返回。getter不返回任何内容没有多大意义,是吗?

然而,在.cpp文件中,您已经实现了getValue(),如下所示:

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

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