5
#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.

0

3 Answers 3

6

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.

2
  • 1
    why is this not the case for functions? Mar 31, 2013 at 21:03
  • 1
    It is, you can't use a function before you've defined it to the compiler either. The difference is that the body of the function can be anywhere because it's the linker that resolves where the actual code lives that you are calling. But the compiler still has to know what the function definition looks like. Mar 31, 2013 at 21:04
6

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
}
0
1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.