C ++ Visual Studio中的错误“C3145”和“C2061”

时间:2012-07-17 02:39:16

标签: visual-studio-2010 compiler-errors c++-cli

编辑:什么是C ++ / CLI?我在Visual Studio中编程,据我所知使用C ++ ...另外,第一个错误是由Peter的评论解决的,但我仍然坚持第二个错误。

我是C ++世界的新手,之前我用Java完成了所有工作。我不熟悉指针和垃圾收集的使用(虽然我相信我理解这个概念),我相信这可能是我的问题的根源。我收到以下错误消息:

1>Runner.cpp(6): error C3145: 'formOutOfTime' : global or static variable may not have managed type 'System::Windows::Forms::Form ^'
1>          may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>Runner.cpp(22): error C2061: syntax error : identifier 'FormOutOfTime'

我的代码是这样的:

PurpleHealth.cpp(这是我认为系统调用以启动它的文件):

#include "FormOutOfTime.h"

#include "FormParentalOverride.h"
#include "Runner.h"

using namespace PurpleHealth;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    //Application::Run(gcnew FormOutOfTime());
    Runner* runner = new Runner();

    //delete runner;

    return 0;
}

Runner.h(这是我想运行所有主要代码的头文件,并启动表单。我也很难解决头文件背后的目的)

#include "stdafx.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"

class Runner
{
public:

    Runner();
    ~Runner();

    // functions

private:

    void Go();
    // member variables

};

最后Runner.cpp:

#include "stdafx.h"
#include "Runner.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
//Variable Dclaration
System::Windows::Forms::Form^ formOutOfTime;//Error Here***************************

Runner::Runner()
{
    // Do stuff if you need to

    this->Go();
}

Runner::~Runner()
{
    // Clear memory if you need to
}

void Runner::Go()
{
     formOutOfTime = gcnew FormOutOfTime();//Error Here***************************
    formOutOfTime->ShowDialog();
}

请帮我解决这些消息,甚至对表格的批评表示赞赏。感谢。

1 个答案:

答案 0 :(得分:2)

托管指针不能在静态或全局范围内声明。它们只能在函数范围内声明。将formOutOfTime的声明从runner.cpp文件的顶部移动到Go方法