eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.25k stars 175 forks source link

Cannot compile simple example - C++ requires a type specifier for all declarations #55

Closed theodelrieu closed 7 years ago

theodelrieu commented 8 years ago

Hi, I cannot manage to compile the attached test program, I attached the output I got from clang++ 3.7 Seems GCC/Clang have trouble with macros like When and Verify (With the others too I guess)

I suppose I did something silly, or I missed something obvious, since this is the most basic program I could write.

fakeit_test.zip

eranpeer commented 7 years ago

I can't reproduce the problem. It compiles perfectly fine for me. It could be an old issue that was solved in in one of the updates.

rtimmons commented 6 years ago

Pardon the comment on an old issue. This was happening to me and this is the only thing that came up in googling. Putting this here in case somebody else stumbles across this.

The root of the problem in my case was that catch.hpp was actually empty (zero bytes) due to how it was being vendored in my project. My build/setup script was supposed to be downloading it at build-time but somehow ended up creating an empty file in some edge-case.

For some reason if catch.hpp is an empty file, it manifests with this "error: C++ requires a type specifier for all declarations" issue. I guess that makes sense from a language/grammar perspective...

Real face-palm moment realizing this after 2 hours of trying to debug what I thought was a compiler or cmake bug :)

ran-err commented 10 months ago

This comment is for anyone who stuck with the same problem. Inspired by @rtimmons after tried so many useless methods and googles.

The problem is that the "When..." statement should reside in a function.

Correct version here:

#include "fakeit.hpp"
using namespace fakeit;

struct SomeInterface {
    virtual int foo(int) = 0;
};

Mock<SomeInterface> mock;

int main()
{
    When(Method(mock, foo)).Return(1);  // This will compile
    SomeInterface& i = mock.get();
    std::cout << i.foo(0);

    return 0;
}

Wrong version here:

#include "fakeit.hpp"
using namespace fakeit;

struct SomeInterface {
    virtual int foo(int) = 0;
};

Mock<SomeInterface> mock;

// error goes here
When(Method(mock, foo)).Return(1);
// "Ambiguous symbol mock" in Visual Studio
// "A type specifier is required for all declarations" in CLion

SomeInterface& i = mock.get();

int main()
{
    std::cout << i.foo(0);
    return 0;
}