Anthony Raimondo
Anthony Raimondo

Reputation: 1721

c++ Class declaration after main?

#include "stdafx.h"
using namespace System;

class Calculater; // how to tell the compiler that the class is down there? 

int main(array<System::String ^> ^args)
{
    ::Calculater *calculater = new Calculater();

    return 0;
}

class Calculater
{
public:
    Calculater()
    {
    }
    ~Calculater()
    {
    }

};

Im declaring the class after main, how do i tell the compiler were my class is? i tried
class Calculater; before main but its not working.

Upvotes: 5

Views: 10254

Answers (3)

Dave
Dave

Reputation: 46249

You can have pointers to Calculator after your pre-declaration. The problem is the constructor (new Calculator()), which hasn't been defined at that point. You can do this:

Before main:

class Calculator { // defines the class in advance
public:
    Calculator(); // defines the constructor in advance
    ~Calculator(); // defines the destructor in advance
};

After main:

Calculator::Calculator(){ // now implement the constructor
}
Calculator::~Calculator(){ // and destructor
}

Upvotes: 6

4pie0
4pie0

Reputation: 29724

put class definition before main:

#include "stdafx.h"
using namespace System;

class Calculater
{
public:
    Calculater()
    {
    }
    ~Calculater()
    {
    }

};

int main(array<System::String ^> ^args)
{
    Calculater *calculater = new Calculater();

    return 0;
}

Upvotes: 1

Jonathan Potter
Jonathan Potter

Reputation: 37122

You can't do it how you've written it. The compiler has to be able to see the definition of the class before it can use it. You need to put your class before your main function, or preferably in a separate header file which you include.

Upvotes: 6

Related Questions