Illegal indirection



  • Ich erhalte die Fehlermeldung illegal indirection, wenn ich versuche die Folgende Aktion ausführen möchte: Snake.push_back({ x, y});
    Woran liegt das?

    std::vector<std::map<short, short>> Snake; // Global
    
    void setupGame()
    {
    	short x, y;
    	x = rand() % (BOARD_WIDTH - 1) + (BOARD_X + 1);
    	y = rand() % (BOARD_HEIGHT - 1) + (BOARD_Y + 1);
    	Snake.push_back({ x, y });
    }
    

  • Mod

    CodeBlox schrieb:

    Woran liegt das?

    Dass die Typen hinten und vorne nicht passen. Snake nimmt Objekte vom Typ std::map<short, short> auf. Du möchtest { x, y } einfügen. Ich bin mir gerade so spontan nicht einmal 100% sicher, was für ein Typ das hier genau wäre, aber es ist ganz sicher keine std::map<short, short> noch etwas, aus dem man eine std::map<short, short> erzeugen könnte.

    Da mir nicht klar ist, was du hier überhaupt möchtest, kann ich dir leider nicht sagen, wie es richtig geht. Das passt einfach nicht zusammen, dass du einerseits einen Vector von std::map<short, short> hast und andererseits wohl irgendwie Paare von short dort einfügen möchtest. Bist du sicher, dass du weißt, was ein Vector und eine Map jeweils sind? Es scheint mir eher, als wolltest du eventuell einen Vector von short-pairs oder short-Tupeln haben, oder aber nur eine Map (ohne Vector) von short-Paaren. Vermutlich ist eher die Map falsch, da es hier wohl um Koordinatenpaare geht, eine Map aber hingegen eine Schlüssel-Wert Beziehung darstellt.

    Globale Variablen sind pfui. Ich sehe hier keinen Grund, warum man hier irgendetwas global machen müsste.



  • Es handelt sich um das Spiel Snake, welches ich gerne nachprogrammieren wollte.
    Hier der source code:

    #include <vector>
    #include <map>
    #include <time.h>
    #include <iostream>
    
    #include "ic.hpp"
    
    #define BOARD_WIDTH 50
    #define BOARD_HEIGHT 20
    #define BOARD_X 2
    #define BOARD_Y 2
    
    std::vector<std::map<short, short>> Snake;
    std::map<short, short> Food;
    
    void resetFood(); 
    void addLength();
    void drawBoard();
    void drawSnake();
    void drawFood();
    
    void moveSnakeUp();
    void moveSnakeDown();
    void moveSnakeLeft();
    void moveSnakeRight();
    
    void checkSnakeCollision(); 
    void Game();
    
    // Directions
    bool moveSnakeUp_b = false;
    bool moveSnakeDown_b = false;
    bool moveSnakeLeft_b = true;
    bool moveSnakeRight_b = false;
    
    void setupGame();
    
    int main()
    {
    	//setupGame();
    	drawBoard();
    
    	getchar();
    }
    
    void Game()
    {
    	while (true)
    	{
    		if (GetAsyncKeyState(VK_LEFT))
    		{
    			if (!moveSnakeLeft_b)
    			{
    				moveSnakeLeft();
    				checkSnakeCollision();
    			}
    		}
    		else if (GetAsyncKeyState(VK_RIGHT))
    		{
    			if (!moveSnakeRight_b)
    			{
    				moveSnakeRight();
    				checkSnakeCollision();
    			}
    		}
    		else if (GetAsyncKeyState(VK_UP))
    		{
    			if (!moveSnakeUp_b)
    			{
    				moveSnakeUp();
    				checkSnakeCollision();
    			}
    		}
    		else if (GetAsyncKeyState(VK_DOWN))
    		{
    			if (!moveSnakeDown_b)
    			{
    				moveSnakeDown();
    				checkSnakeCollision();
    			}
    		} 
    		else if (moveSnakeDown_b)
    		{
    			moveSnakeDown();
    			checkSnakeCollision();
    		}
    		else if (moveSnakeUp_b)
    		{
    			moveSnakeUp();
    			checkSnakeCollision();
    		}
    		else if (moveSnakeRight_b)
    		{
    			moveSnakeRight();
    			checkSnakeCollision();
    		}
    		else if (moveSnakeLeft_b)
    		{
    			moveSnakeLeft();
    			checkSnakeCollision();
    		}
    
    		Sleep(500);
    	}
    }
    
    void checkSnakeCollision()
    {
    	bool collided = false;
    
    	for (const auto s : ::Snake)
    	{
    		if (s.begin()->first == BOARD_X || s.begin()->first == BOARD_WIDTH ||
    			s.begin()->second == BOARD_Y || s.begin()->second == BOARD_HEIGHT)
    		{
    			collided = true;
    		}
    	}
    
    	int i = 0; // Position in vector
    	for (const auto s : ::Snake)
    	{
    		++i;
    		int k = 0; // Position in vector
    		for (const auto s_a : ::Snake)
    		{
    			++k;
    			if (s_a.begin()->first == s.begin()->first &&
    				s_a.begin()->second == s.begin()->second
    				&& i == k)
    			{
    				collided = true;
    			}
    		}
    	}
    
    	if (collided)
    		exit(1);
    
    }
    
    void moveSnakeRight()
    {
    	if (!moveSnakeUp_b)
    	{
    		moveSnakeUp_b = false;
    		moveSnakeDown_b = false;
    		moveSnakeLeft_b = true;
    		moveSnakeRight_b = false;
    	}
    
    	short tempX, tempY;
    	short temp2X, temp2Y;
    	bool first = true;
    
    	short operation = 1;
    
    	for (auto& s : ::Snake)
    	{
    		if (first)
    		{
    			tempX = s.begin()->first;
    			tempY = s.begin()->second;
    			::Snake.begin()->clear();
    			::Snake.begin()->insert({ tempX - 1, tempY });
    			first = false;
    		}
    		else
    		{
    			if (operation == 1)
    			{
    				temp2X = s.begin()->first;
    				temp2Y = s.begin()->second;
    
    				s.clear();
    				s.insert({ tempX, tempY });
    				operation = -1;
    			}
    			else if (operation == -1)
    			{
    				tempX = s.begin()->first;
    				tempY = s.begin()->second;
    
    				s.clear();
    				s.insert({ temp2X, temp2Y });
    				operation = 1;
    			}
    		}
    	}
    }
    
    void moveSnakeLeft()
    {
    	if (!moveSnakeLeft_b)
    	{
    		moveSnakeUp_b = false;
    		moveSnakeDown_b = false;
    		moveSnakeLeft_b = true;
    		moveSnakeRight_b = false;
    	}
    
    	short tempX, tempY;
    	short temp2X, temp2Y;
    	bool first = true;
    
    	short operation = 1;
    
    	for (auto& s : ::Snake)
    	{
    		if (first)
    		{
    			tempX = s.begin()->first;
    			tempY = s.begin()->second;
    			::Snake.begin()->clear();
    			::Snake.begin()->insert({ tempX + 1, tempY });
    			first = false;
    		}
    		else
    		{
    			if (operation == 1)
    			{
    				temp2X = s.begin()->first;
    				temp2Y = s.begin()->second;
    
    				s.clear();
    				s.insert({ tempX, tempY });
    				operation = -1;
    			}
    			else if (operation == -1)
    			{
    				tempX = s.begin()->first;
    				tempY = s.begin()->second;
    
    				s.clear();
    				s.insert({ temp2X, temp2Y });
    				operation = 1;
    			}
    		}
    	}
    }
    
    void moveSnakeDown()
    {
    	if (!moveSnakeDown_b)
    	{
    		moveSnakeUp_b = false;
    		moveSnakeDown_b = true;
    		moveSnakeLeft_b = false;
    		moveSnakeRight_b = false;
    	}
    
    	short tempX, tempY;
    	short temp2X, temp2Y;
    	bool first = true;
    
    	short operation = 1;
    
    	for (auto& s : ::Snake)
    	{
    		if (first)
    		{
    			tempX = s.begin()->first;
    			tempY = s.begin()->second;
    			s.begin()->second += 1;
    			first = false;
    		}
    		else
    		{
    			if (operation == 1)
    			{
    				temp2X = s.begin()->first;
    				temp2Y = s.begin()->second;
    
    				s.clear();
    				s.insert({ tempX, tempY });
    				operation = -1;
    			}
    			else if (operation == -1)
    			{
    				tempX = s.begin()->first;
    				tempY = s.begin()->second;
    
    				s.clear();
    				s.insert({ temp2X, temp2Y });
    				operation = 1;
    			}
    		}
    	}
    }
    
    void moveSnakeUp()
    {
    	if (!moveSnakeUp_b)
    	{
    		moveSnakeUp_b = true;
    		moveSnakeDown_b = false;
    		moveSnakeLeft_b = false;
    		moveSnakeRight_b = false;
    	}
    
    	short tempX, tempY;
    	short temp2X, temp2Y;
    	bool first = true;
    
    	short operation = 1;
    
    	for (auto& s : ::Snake)
    	{
    		if (first)
    		{
    			tempX = s.begin()->first;
    			tempY = s.begin()->second;
    			s.begin()->second -= 1;
    			first = false;
    		}
    		else
    		{
    			if (operation == 1)
    			{
    				temp2X = s.begin()->first;
    				temp2Y = s.begin()->second;
    
    				s.clear();
    				s.insert({ tempX, tempY });
    				operation = -1;
    			}
    			else if (operation == -1)
    			{
    				tempX = s.begin()->first;
    				tempY = s.begin()->second;
    
    				s.clear();
    				s.insert({ temp2X, temp2Y });
    				operation = 1;
    			}
    		}
    	}
    }
    
    void drawFood()
    {
    	ic::shorties::gotoxy(::Food.begin()->first, ::Food.begin()->second);
    	std::cout << "?";
    }
    
    void setupGame()
    {
    	short x, y;
    	x = rand() % (BOARD_WIDTH - 1) + (BOARD_X + 1);
    	y = rand() % (BOARD_HEIGHT - 1) + (BOARD_Y + 1);
    	Snake.push_back({ x, y });
    }
    
    void drawSnake()
    {
    	for (const auto& s : ::Snake)
    	{
    		for (const auto& s_pos : s)
    		{
    			ic::shorties::gotoxy(s_pos.first, s_pos.second);
    			std::cout << "O";
    		}
    	}
    }
    
    void addLength()
    {
    	short x, y;
    
    	if (moveSnakeUp_b)
    	{
    		x = ::Snake.end()->end()->first;
    		y = ::Snake.end()->end()->second - 1;
    		::Snake.push_back({ x, y });
    	}
    	else if (moveSnakeDown_b)
    	{
    		x = ::Snake.end()->end()->first;
    		y = ::Snake.end()->end()->second + 1;
    		::Snake.push_back({ x, y });
    	}
    	else if (moveSnakeLeft_b)
    	{
    		x = ::Snake.end()->end()->first + 1;
    		y = ::Snake.end()->end()->second;
    		::Snake.push_back({ x, y });
    	}
    	else if (moveSnakeRight_b)
    	{
    		x = ::Snake.end()->end()->first - 1;
    		y = ::Snake.end()->end()->second;
    		::Snake.push_back({ x, y });
    	}
    }
    
    void resetFood()
    {
    	short x, y;
    	x = rand() % (BOARD_WIDTH - 1) + (BOARD_X + 1);
    	y = rand() % (BOARD_HEIGHT - 1) + (BOARD_Y + 1);
    	::Food.clear();
    	::Food.insert({ x, y });
    }
    
    void drawBoard()
    {
    	// Draw upper line
    	for (short i = BOARD_X; i < BOARD_WIDTH; ++i)
    	{
    		ic::shorties::gotoxy(i, BOARD_Y);
    		std::cout << "#";
    	}
    
    	// Draw lower line
    	for (short i = BOARD_X; i < BOARD_WIDTH; ++i)
    	{
    		ic::shorties::gotoxy(i, BOARD_HEIGHT);
    		std::cout << "#";
    	}
    
    	// Draw left line
    	for (short i = BOARD_Y; i < BOARD_HEIGHT; ++i)
    	{
    		ic::shorties::gotoxy(BOARD_X, i);
    		std::cout << "#";
    	}
    
    	// Draw right line
    	for (short i = BOARD_Y; i <= BOARD_HEIGHT; ++i)
    	{
    		ic::shorties::gotoxy(BOARD_WIDTH, i);
    		std::cout << "#";
    	}
    }
    

  • Mod

    Und?



  • void setupGame()
    {
    	short x, y;
    	x = rand() % (BOARD_WIDTH - 1) + (BOARD_X + 1);
    	y = rand() % (BOARD_HEIGHT - 1) + (BOARD_Y + 1);
    	std::map<short, short> pos;
    	pos.insert({ x, y });
    	Snake.push_back(pos);
    }
    

    Jetzt funktioniert es, Dankeschön 😃


  • Mod

    Bist du wirklich sicher, ob du weißt, was eine Map ist?

    Programmieren heißt nicht, dass man so lange diverse Sonderzeichen im Sourcecode einfügt, bis etwas compiliert. Du musst genau verstehen, was du da schreibst. Ein Vector von Maps kommt mir sehr, sehr merkwürdig vor und deine Benutzung desselben erst recht.

    Globale Variablen sind immer noch pfui. Falls das zu subtil sein sollte: Benutz sie nicht!



  • CodeBlox schrieb:

    #include <vector>
    #include <map>
    #include <time.h>
    #include <iostream>
    
    #include "ic.hpp"
    
    #define BOARD_WIDTH 50
    //seitenweise ganz übler code
    

    Einfachste Reparatur fängt wohl so an:

    #include <vector>
    #include <map>
    #include <time.h>
    #include <iostream>
    
    #define map pair
    
    #include "ic.hpp"
    
    #define BOARD_WIDTH 50
    //seitenweise ganz übler code
    

    Besser wäre ein Anfang mit Strg+A, Entf, Strg+s, Alt+F4, Y, Enter.


Anmelden zum Antworten