shadoweye14
shadoweye14

Reputation: 813

Why is this method of forward declaration producing an error?

// practice.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;

class one;
int main()
{
    one alpha;
    cin.get();
}

class one
{

};

I have no idea why the above method is producing an error: object uses undefined class. Any help would be nice.

Upvotes: 0

Views: 58

Answers (3)

Neko Coder
Neko Coder

Reputation: 1

Compiler need to know the full definition of the class before you define an object,if you want to define the class lately, you can define a pointer or reference first, and initlize it after you define the class

Upvotes: 0

Srikanth Kavoori
Srikanth Kavoori

Reputation: 11

You need to define class before you use it. forward declaration works only for pointers, but that is not the way you are using.

Upvotes: 0

Neil Kirk
Neil Kirk

Reputation: 21763

Forward-declaring allows you to create a pointer or reference to a class. However, in order to use the class, which includes creating an instance of it, the full class definition is required by the compiler at that point.

Upvotes: 3

Related Questions