错误是什么意思"void-value is not ignored"以及如何删除它?
What does "void-value is not ignored" error mean and how to remove it?
我尝试编译以下代码:
#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.h
和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;
}
您在测试的类中声明void getValue();
。。变为CCD_ 6。
您的类定义与实现不匹配:
在您的头中,您以以下方式声明了它(顺便说一句,您可能需要研究一些命名约定(。
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
您已将getValue()
声明为void
,即不返回。getter
不返回任何内容没有多大意义,是吗?
然而,在.cpp
文件中,您已经实现了getValue()
,如下所示:
int tested::getValue() {
return x;
}
您需要更新头类型中的getValue()
方法签名,以便其返回类型与实现(int
(匹配。
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 擦除删除成语的性能增益从何而来
- 重载运算符 new/new[] 删除/删除[] 全局C++
- 使用删除 [] 删除由 new [] 创建的数组
- BST 删除/删除节点 - 根
- C++ 使用删除 [] 删除指针
- 删除/删除矢量项的最有效/最快方法
- 删除删除/删除[]
- 使用 std::删除删除错误的字符