main后的类声明
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()
{
}
};
我在main之后声明类,我如何告诉编译器我的类是?我试着
类计算机;在main之前,但它不起作用。
你不能按照你写的去做。编译器在使用类之前必须能够看到类的定义。您需要将类放在main
函数之前,或者最好放在单独的头文件中,其中包括
您可以在预声明之后使用指向Calculator的指针。问题是构造函数(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
}
把类定义放在main前面:
#include "stdafx.h"
using namespace System;
class Calculater
{
public:
Calculater()
{
}
~Calculater()
{
}
};
int main(array<System::String ^> ^args)
{
Calculater *calculater = new Calculater();
return 0;
}
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- "Main"已在当前范围内声明
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 如何从不同的类中获取函数来调用在 Main 中声明的对象?
- 我们可以在main函数中声明嵌套类对象吗
- 在函数 'int main(int, char**) 中,没有声明 'MIN'
- 内存浪费?如果main()应该只返回0或1,那么为什么main是用int而不是短int甚至char声明的
- 为什么在 main() 中声明的指针没有更改
- 如何在不指定 C++ 中的所有参数的情况下声明程序的 main() 入口点
- 未声明的标识符和类型"int"从 main 调用类时出现意外错误
- int main() 是否需要对C++进行声明
- 如何在另一个函数中初始化在 main 中声明的数组
- 我需要创建一个函数在用户下打印"=",但由于变量是使用 main() 声明的,因此函数看不到参数
- 为什么不使用函数try块声明main()?
- C 外部阵列在标头中声明,在main.cpp中不可用
- C++声明"main"作为对功能的引用?
- 是否有可能在c++中声明main函数中的全局变量?如果可能的话,怎么做