C++ 有什么方法可以在 main() 函数之后放置类定义?

标签 c++ visual-studio

我正在编写一个小型 C++ 程序,需要将所有代码保存在一个文件中。在其中,我有一个类定义。我想将类定义放在 main() 函数之后。尝试这样做时,visual studio 中的编译器会提示,因为类定义似乎必须出现在 main() 函数之前。

有没有一种方法可以将类定义放在 main() 函数之后?也许使用某种宏或预编译器技巧?如果代码在 main 之前使用类 def 进行编译,那很好,但在原始源代码中,我需要在 main 之后使用类 def。

int main(){

    someClass object;
    //remainder of code

}


class someClass{

    //class code
}

最佳答案

这会起作用,但它真的很糟糕。如果我可以否决自己的答案,我会的。请不要在生产环境中这样做。

假设文件是​​test.cc

#if defined(guard)
int main(){
    someClass object;
}
#endif

#if !defined(guard)
class someClass
{

};
#define guard
#include "test.cc"
#endif

关于C++ 有什么方法可以在 main() 函数之后放置类定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239320/

相关文章:

c++ - 特征值:t 为 1s 的随机二元 vector

c++ - 从函数 C++ 返回 float 组

c++ - 类中数组指针的赋值

visual-studio-2010 - VS2010升级后无法链接到lib(__cdecl vs __thiscall?)

c++ - 检查鼠标左键是否被按下?

c# - 当我尝试使用作为链接添加到项目中的图像文件时,为什么会收到 IOException?

c++ - dlib和windows函数冲突

visual-studio - TFS 合并特定变更集 - 变更集丢失

C++ Visual Studio "Non-standard syntax; use ' &' to create a pointer to member"

c++ - 在 Win32 错误中使用整数