views:

33

answers:

1

I'm trying to do some CppUnit testing on my program using Ubuntu NetBeans but I keep encountering similar errors (invalid use of void expression). I'm not sure what went wrong. Any help will be greatly appreciated.

The error goes like this:

g++    -c -O2 -I/usr/include/cppunit -MMD -MP -MF build/Release/GNU-Linux-x86/AssignmentTest.o.d -o build/Release/GNU-Linux-x86/AssignmentTest.o AssignmentTest.cpp
AssignmentTest.cpp: In member function ‘void AssignmentTest::testTitle()’:
AssignmentTest.cpp:10: error: invalid use of void expression

My AssignmentTest.cpp:

#include "AssignmentTest.h"
#include "GetInfo.h"

CPPUNIT_TEST_SUITE_REGISTRATION (AssignmentTest);

void AssignmentTest::testTitle()
{
    //info2 = "";
    //CPPUNIT_ASSERT(info2.testTitle(info2));
    CPPUNIT_ASSERT_EQUAL(info2, info2.GetTitle());
}

My AssignmentTest.h:

#ifndef _ASSIGNMENTTEST_H
#define _ASSIGNMENTTEST_H
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include <vector>
#include "GetInfo.h"

class AssignmentTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE (AssignmentTest);
    CPPUNIT_TEST (testTitle);
    CPPUNIT_TEST (testDirector);
    CPPUNIT_TEST (testReleaseDate);
    CPPUNIT_TEST (testPlot);
    CPPUNIT_TEST (testRunTime);
    CPPUNIT_TEST_SUITE_END ();
private:
    GetInfo info1;
    GetInfo info2;
        GetInfo info3;
        GetInfo info4;
        GetInfo info5;
        GetInfo info6;
public:

protected:
    void testTitle();
    void testDirector();
    void testReleaseDate();
        void testPlot();
    void testRunTime();
};
#endif

My GetInfo.h:

#include <string>
#ifndef _GETINFO_H
#define _GETINFO_H

using namespace std;

class GetInfo
{
public:
    GetInfo();
        void    GrabMovie(void);
    void    GetTitle(void);
    void    GetDirector(void);
    void    GetReleaseDate(void);
    void    GetPlot(void);
    void    GetRunTime(void);        
private:

};
#endif
+1  A: 

What are you trying to compare for equality? Right now, you are comparing info2 against info2.GetTitle(). The former is an object. The latter, according to GetInfo.h, returns void -- that is, nothing.

Chances are, your test should look more like

CPPUNIT_ASSERT_EQUAL("Expected Title", info2.GetTitle());

and all of your "Get" functions should actually return whatever you are getting.

Jon Reid
Incidentally, putting `using namespace std` in a header file is poor form, because anything that includes that header gets that "using" declaration whether they want it or not. (It's fine in a .cpp file.)
Jon Reid
@Jon Reid: I'm trying to compare the values that I get from, against a hardcoded value.So if my info2 is hardcoded as "Avatar", my info2.GetTitle() returns "Avatar" and it will match.But it's giving invalid use of void... which I don't know how to go from there.
Wallace
That is what I tried to show in my example: comparison against a hardcoded value. But your GetTitle returns void (which is the problem indicated by the error). What do you want it to return?
Jon Reid