2013-03-31 89 views
3
#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() 
    { 
    } 

}; 

即時聲明main之後的類,我如何告訴編譯器我的類是?我試過
class Calculater;主要之前,但它不工作。後C++類聲明?

回答

3

你不能這樣寫它。編譯器必須能夠在使用它之前看到該類的定義。您需要將您的課程放在您的main函數之前,或者最好在您包含的單獨頭文件中。之前主要

+0

爲什麼這不是功能的情況下? –

+0

也就是說,在將其定義到編譯器之前,您不能使用函數。區別在於函數的** body **可以是任何地方,因爲它是鏈接器,可以解析您調用的實際代碼所在的位置。但編譯器仍然需要知道函數定義是什麼樣的。 –

1

認沽類的定義:

#include "stdafx.h" 
using namespace System; 

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

}; 

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

    return 0; 
} 
6

你可以有你的指針預申報後,計算器。問題在於構造函數(new Calculator()),該函數尚未在此處定義。你可以這樣做:

主前:

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

後主營:

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

謝謝!這是我一直在尋找! –