Bhawna Singh
Bhawna Singh

Reputation: 1

Visual Studio 2017 "use of undefined type"

I have the following code which fails to compile with Visual Studio 2017 with error

Error C2027 use of undefined type 'A'

but is compiling fine in Visual Studio 2012 & Visual Studio 2015.

#include <iostream>

    class A;
    std::string s = typeid(A).name();

    class A
    {
        public:
           int a;
    };

    int main()
    {
        std::cout << "Hello World!\n"; 
    }

Can anyone suggest what exactly is the problem with VS 2017 compilation and how to fix this? Is there any rule changes between VS 2015 and VS 2017?

Upvotes: 0

Views: 1771

Answers (1)

pyj
pyj

Reputation: 1499

The errors I get from clang and gcc for this example are more clear:

typeid.cpp:5:17: error: 'typeid' of incomplete type 'A'
std::string s = typeid(A).name();
                ^
typeid.cpp:4:7: note: forward declaration of 'A'
class A;

class A is a forward declaration of the A type, so the full information about that type is not yet known. I suspect behavior of Visual Studio 2012 and 2015 would be considered non-standard. You need to move your string until after the definition class A {...}; so the compiler can see the type definition.

Upvotes: 2

Related Questions