2

この学校の課題を作成するときに、複数の紛らわしいエラーが発生し、問題の可能性について何らかの方向性を期待しています。通常はこのように書くことはありませんが、これをデバッグしようとすると、すべてを1つのファイルにまとめます。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

さまざまなクラスを使用しようとしている時点では、コンパイラはまだそれらを認識していません。main()関数をファイルの最後に移動します。または、クラス定義を独自のヘッダーファイルに分割してから、メインのソースファイルにインクルードすることをお勧めします。

他にも関連する問題があります。たとえばTimerEventArgs、コンパイラがクラスを認識する前に、クラスを使用しようとしています。したがって、クラス定義を上に移動する必要があります。これが、各クラスを独自のヘッダーファイルに入れ、必要に応じてインクルードするのが最適な理由です。厳密には不要ではありませんが、すべてを正しい順序で宣言/定義すれば。

于 2012-10-22T01:09:12.197 に答える
0

宣言の順序が間違っていることを除けば、問題はコンパイラが^ビットを認識しないことであるように見えます。これは、C ++/CLIとしてコンパイルしていないことを示しています。ソリューションエクスプローラーでプロジェクトを右クリックし、[構成のプロパティ]-> [全般]に移動して、[共通言語ランタイムサポート]が[共通言語ランタイムサポート(/ clr)]に設定されていることを確認します。

于 2012-10-22T01:54:43.120 に答える
0

他の誰か(他の初心者)の利益のために:結局のところ、問題は、いくつかのクラスが互いに「#include」しているという事実にあるという私の疑いが問題でした。前方宣言を使用し、変数ストレージハンドラーとして機能するために完全に別のクラスを作成する必要があることと組み合わせて、私の問題の解決策でした。

これが私に最大の問題を与えていた2つのクラスであり、正しく機能するように修正されました。

/*
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 に答える