c++visual-studio-2010googletest

ASSERT_TRUE() return type does not match function type in gtest


When I am using ASSERT_TRUE() provided in Gtest I am getting below error. return type does not match function type with an underline in VS 2010..

abc.h

#include "gtest\gtest.h"

class abc {
pubilc:
    bool fun();
    private:
    bool fun1();
};

abc.c

bool abc::fun()
{
    ASSERT_TRUE(fun1()); // Getting error: return type does not match function type
}

bool abc::fun1()
{
    return true; // True or false depanding on operation
}

Solution

  • There is no return statement specified in fun() but it returns a bool. Add a return false; or return true; to fun() or change its return type to void:

    void abc::fun()
    {
        ASSERT_TRUE(fun1());
    }
    

    Based on My compiler complains that a constructor (or destructor) cannot return a value. What's going on? which states (verbatim):

    Due to a peculiarity of C++, in order to support the syntax for streaming messages to an ASSERT_*, e.g.

    ASSERT_EQ(1, Foo()) << "blah blah" << foo;
    

    we had to give up using ASSERT* and FAIL* (but not EXPECT* and ADD_FAILURE*) in constructors and destructors. The workaround is to move the content of your constructor/destructor to a private void member function, or switch to EXPECT_*() if that works. This section in the user's guide explains it.

    the return type must be void in functions that use ASSERT_*() macros.