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;
}