Marco Pagliaricci
Marco Pagliaricci

Reputation: 1386

error C2027: the gcc vs msvc case

This simple snippet of code

class S;

class G
{

    void onRead(S & s)
    {
           (void)s;
    }
};

perfectly works on GCC. Using that (void)s is a very useful way to avoid the warning "unused variable 's'". But MSVC sees this as an error, stopping the compilation. It gives the infamous error C2027: use of undefined type 's'. But here 's' its not used at all.

How can I solve this kind of trouble?

I don't want to use the form of void onRead(S &) because you can't see it in this little snippet example, but in my code that 's' name is really meaningful, and useful to understand the parameters.

Upvotes: 0

Views: 223

Answers (2)

Zebra North
Zebra North

Reputation: 11492

You can't use "s" because the class "S" hasn't been fully defined. You can:

1) Disable the warning with #pragma warning(disable:4100)

2) Move the body of the onRead function to a place where "S" has been fully defined

3) Move the definition of "S" so it is before onRead

4) Use a void pointer: (void*)&s;

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308138

There are a couple of alternate ways of avoiding the error.

The most straight-forward is to convert the variable name to a comment:

void onRead(S & /* s */)

Another is to use a macro to remove the variable:

#define UNUSED(x)

void onRead(S & UNUSED(s))

I'm sure you've already thought of just moving the code to a point where S is fully defined and there's some reason you can't do that.

Upvotes: 2

Related Questions