Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么是;无效值不被忽略";错误的意思和如何删除它?_C++_Compilation_Void - Fatal编程技术网

C++ 什么是;无效值不被忽略";错误的意思和如何删除它?

C++ 什么是;无效值不被忽略";错误的意思和如何删除它?,c++,compilation,void,C++,Compilation,Void,我尝试编译以下代码: #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();

我尝试编译以下代码:

#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);
}
EDDIT

为了使示例完整,我发布了
tested.h
tested.cpp
的代码:

tested.h

#include <iostream>
using namespace std;

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

您声明
void getValue()。。更改为
intgetvalue()

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

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

在标题中,您已经用以下方式声明了它(作为旁白,您可能需要研究一些命名约定)

您已将
getValue()
声明为
void
,即不返回。对于一个
getter
来说什么都不返回没有多大意义,是吗

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

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

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

第18行在哪里,CPPUNIT的值是多少\宏?哪一行是第18行?无论如何,该错误可能表明您正在将调用返回
void
的函数的结果分配给sommile。getValue()的定义是什么?@iammilind,我已更改代码以指示第18行的位置。我不知道CPPUNIT的值是什么。
void getValue()您希望如何将其分配给
int
?此声明与实际定义不匹配。
#include <iostream>
using namespace std;

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

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