0

エラー:

1>------ Build started: Project: SFML_lesson1, Configuration: Debug Win32 ------
1>  Main.cpp
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(5): error C2146: syntax error : missing ';' before identifier 'event'
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2065: 'event' : undeclared identifier
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2228: left of '.type' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2653: 'Event' : is not a class or namespace name
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2065: 'MouseButtonPressed' : undeclared identifier
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2065: 'event' : undeclared identifier
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2228: left of '.mouseButton' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2228: left of '.button' must have class/struct/union
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2653: 'Mouse' : is not a class or namespace name
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\keyevents.h(8): error C2065: 'Left' : undeclared identifier
1>c:\users\steven\documents\visual studio 2010\projects\sfml_lesson1\sfml_lesson1\main.cpp(47): error C3867: 'input::leftclick': function call missing argument list; use '&input::leftclick' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

このようなことをするのはこれが初めてです。過去数週間で C++ を学んでいますが、奇妙なエラーが発生しています。ゲーム開発の基本を理解するために、これらの udemy のチュートリアルに従って SFML を使用しています。左トリガー イベントを使用する簡単な方法を作成し、長い行を書き出すことにしました。

ここに私のKeyEvents.hファイルがあります:

class input{

public:

    Event event;

    int leftclick(){
    event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left;

    return true;
    }

};

ここに私のMain.cppファイルがあります:

//Libraries
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Settings.h" // Includes the Settings.h file located in the "Header Files" folder
#include "KeyEvents.h"
using namespace std;
using namespace sf;


int main(){
    // Declarations
    bool Playing=true; // Used for the Main loop when the game starts up
    //  The datatype holding all the events 
    Event event;

    RenderWindow window(VideoMode(Settings::WIDTH,Settings::HEIGHT), "Project"); // Creates the windows

    window.setFramerateLimit(Settings::FRAME_RATE); // Sets the Frame-rate limit
    window.setKeyRepeatEnabled(Settings::KEY_REPEATE); // Remove spamming keys
    while(Playing==true){ // Main loop


            while (window.pollEvent(event)){

                if(event.type == Event::KeyPressed){ // If a Key has been pressed! 
                        // For certain keyboard buttons it would be 'event.key.code == Keyboard::A'
                            // There are two states for the keyboard 'KeyPressed' and 'KeyRelease'

                    cout << "A Key has been pressed!\n";

                }
                 else if(event.type == Event::KeyReleased){

                        cout << "The key has just been released!\n"; // This message only pops up when they 'A' or 'a' has been released


                    }


                 if(event.type == Event::KeyReleased && event.key.code == Keyboard::Escape){ // This is for when the 'event' is closed the game will exit, by making the bool 'Playing' to false it is set to close by using the escape key

                     cout << "The Game has just been closed when Pressing the 'Escape Key'\n";
                     Playing=false;

                 }

                 if(input::leftclick){ // My custom way of knowing when the user left clicks with his/her mouse inside the class input there is a function called leftclick

                 cout << "YAY I AM LEARNING\n";


                 }


            }

        //  Logic:  


        //  Rendering:

    // Clears the window, then displays what it needs to
    window.clear();
    window.display();

    }

    window.close(); // Closes the window. when Playing is false

// Returns tto the computer that the program is running fine.
return true;
}

私が推測する奇妙なエラーです。

4

2 に答える 2

1

Eventクラスは、使用される前に定義されていない(または宣言されていない)ようです。どこで宣言しますか?

于 2012-10-09T21:55:49.560 に答える
0

コンパイラ エラーは、Event型が定義されていないことを示しています。の先頭に、その定義とともにヘッダーを含める必要がありますKeyEvents.h。そして、名前空間スコープの解決を使用することを忘れないでください:

#include <SFML/Event.hpp>

class input{
public:
   sf::Event event;

   // Other code
   ...
};

usingまた、グローバルディレクティブを使用することはお勧めできません。関数の範囲内でローカルに記述します。

于 2012-10-09T22:01:09.450 に答える