2

在构建这项学校作业时,我遇到了多个令人困惑的错误,并希望就可能出现的问题提供一些指导。我通常不会这样写,但我在尝试调试时将所有内容都放入一个文件中。使用 Visual Studios Express 2012。我在构建时遇到了 30 多个错误,所以我确信有一些基本的东西我只是忽略了。只是一个建议,不要找人做我的功课。谢谢

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include "MessageDisplayClass.h"
#include "LogMessageClass.h"
#include "TimerEventArgs.h"

using namespace System;

ref class CustomTimerClass
{

private:
static bool stopFlag = false;

// create instance of TimerEventArgs
TimerEventArgs^ timerEvent;

public:
CustomTimerClass(void)
{
}
delegate void CustomTimerClass::TimerAlarmHandler(/*Object^ sender, TimerEventArgs^ args*/);
event CustomTimerClass::TimerAlarmHandler^ OnTimerAlarm;

property bool StopFlag
{
    bool get(void)
    {
        return stopFlag;
    }

    void set(bool b)
    {
        stopFlag = b;
    }
}

void run()
{
    Sleep(1000);
    raiseTimerAlarm();
}

void OnStart()
{
    // create instances of DisplayMessageClass and LogMessageClass classes
    DisplayMessageClass^ messageDisplayer = gcnew DisplayMessageClass(this);
    LogMessageClass^ messageLogger = gcnew LogMessageClass(this);

    // display and log messages concerning this event
    messageDisplayer->displayMessage(this, timerEvent);
    messageLogger->logMessage(this, timerEvent);
}

void raiseTimerAlarm()
{
    // create instance of TimerEventArgs and get time of instance creation
    timerEvent = gcnew TimerEventArgs();
    String^ eventTime = timerEvent->EventTime;

    // tie this instance of CustomTimerClass to OnTimerAlarm event and start event
    this->OnTimerAlarm += gcnew TimerAlarmHandler(this, &CustomTimerClass::OnStart);
    OnTimerAlarm();
}
};

ref class MainProgram
{
int main(array<System::String ^> ^args)
{
    CustomTimerClass^ timerClass = gcnew CustomTimerClass();
    DisplayMessageClass^ messageClass = gcnew DisplayMessageClass();
    LogMessageClass^ logerClass = gcnew LogMessageClass();
    timerClass->run();
    return 0;
}
};
4

3 回答 3

0

为了其他人(其他新手)的利益:事实证明,我怀疑问题在于某些类彼此“#include”是问题所在。使用前向声明,再加上必须完全创建一个单独的类来充当变量存储处理程序,是解决我的问题的方法。

以下是给我带来最大问题的两个类,已更正以正常运行:

/*
CustomTimerClass.h
*/

#include "StdAfx.h"
#include "LogMessageClass.h"
#include "MessageDisplayClass.h"
#include "TimerEventArgs.h"
#include "Variables.h"

//ref class MessageDisplayClass;
//ref class Variables;

using namespace System;

ref class CustomTimerClass
{

private:
static bool stopFlag = false;

// create instance of TimerEventArgs
TimerEventArgs^ timerEvent;

// create instance of MessageDisplayClass and LogMessageClass
MessageDisplayClass^ messageDisplayer;
LogMessageClass^ messageLogger;

Variables^ flagVariable;

public:
CustomTimerClass(void)
{
}

delegate void CustomTimerClass::TimerAlarmHandler();
event CustomTimerClass::TimerAlarmHandler^ OnTimerAlarm;

property bool StopFlag
{
    bool get(void)
    {
        return stopFlag;
    }

    void set(bool b)
    {
        stopFlag = flagVariable->Flag;
    }
}

void run()
{
    Sleep(1000);
    raiseTimerAlarm();
}

void OnStart()
{
    // create instances of DisplayMessageClass and LogMessageClass classes
    messageDisplayer = gcnew MessageDisplayClass(this, flagVariable);
    messageLogger = gcnew LogMessageClass(this);

    // display and log messages concerning this event
    messageDisplayer->displayMessage(this, timerEvent);
    messageLogger->logMessage(this, timerEvent);
}

void raiseTimerAlarm()
{
    // create instance of TimerEventArgs and get time of instance creation
    timerEvent = gcnew TimerEventArgs();
    String^ eventTime = timerEvent->EventTime;

    // tie this instance of CustomTimerClass to OnTimerAlarm event and start event
    this->OnTimerAlarm += gcnew TimerAlarmHandler(this, &CustomTimerClass::OnStart);
    OnTimerAlarm();
}
};

/*
MessageDisplayClass serves to display a message that 
represents the time at which the TimerEventArgs class is 
instantiated.  This time is returned through a function
of TimerEventArgs class.
*/

#pragma once

#include "stdafx.h"
#include <iostream>
#include "TimerEventArgs.h"
#include "Variables.h"

using namespace System;

ref class CustomTimerClass; // FORWARD DECLARATION HERE  CAN
                        // ONLY BE USED FOR REFERENCE.  CANNOT
                        // BE USED WHEN METHODS OF THE     CLASS
                        // ARE CALLED

ref class MessageDisplayClass
{

private:
CustomTimerClass^ customTimerRef;

// Variables CLASS CREATED SOLELY TO ACT AS GO-BETWEEN BETWEEN
// MessageDisplayClass and CustomTimerClass
Variables^ variableRef;         

static int counter;
public:

// constructor
MessageDisplayClass(CustomTimerClass^ CustomTimerClassInput, Variables^ variableReference)
{
    customTimerRef = CustomTimerClassInput;
    variableRef = gcnew Variables (CustomTimerClassInput); 
}

void displayMessage(Object^ sender, TimerEventArgs^ timer)
{
    counter ++;
    if (counter > 0)
    {
        variableRef->Flag = true;
        Console::WriteLine("Message:  an event occured at time stamp: " + timer->EventTime);
    }
}
};
于 2012-10-23T19:50:54.027 回答
0

除了错误的声明顺序之外,问题似乎是编译器无法识别 ^ 位,这表明您没有编译为 C++/CLI。右键单击解决方案资源管理器中的项目并转到Configuration Properties -> General,并确保Common Language Runtime Support设置为Common Language Runtime Support (/clr)

于 2012-10-22T01:54:43.120 回答
0

在您尝试使用各种类时,编译器还不知道它们。将您的main()函数移动到文件的末尾。或者更好的是,将您的类定义拆分到它们自己的头文件中,然后将它们包含在您的主源文件中。

还有其他相关的问题。例如,您试图TimerEventArgs在编译器知道之前使用该类。因此,您需要将类定义向上移动。这就是为什么最好将每个类放在自己的头文件中,然后在需要的地方包含它。虽然这不是绝对不必要的,但如果您以正确的顺序声明/定义所有内容。

于 2012-10-22T01:09:12.197 回答