-1

I try to compile the following code:

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

As a result I get:

testing.cpp:18:32: Error: void-value is not ignored where it should be

EDDIT

To make the example complete I post the code of the tested.h and 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;
}
Roman
  • 124,451
  • 167
  • 349
  • 456

3 Answers3

4

you declare void getValue(); in the class tested.. change to int getValue();.

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
1

A void function cannot return a value. You are getting a value of int from the API getValue(), hence it should return an int.

bhuwansahni
  • 1,834
  • 1
  • 14
  • 20
1

Your class definition doesn't match the implementation:

In your header you've declared it in the following way (as an aside, you might want to look into some naming conventions).

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

You've declared getValue() as void, i.e no return. Doesn't make much sense for a getter to return nothing, does it?

However, in the .cpp file you've implemented getValue() like so:

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

You need to update the getValue() method signature in the header type so that its return type matches the implementation (int).

Jason Larke
  • 5,289
  • 25
  • 28