gpt4 book ai didi

c++ - 编译错误c++(需要替换),线程有问题吗?

转载 作者:行者123 更新时间:2023-12-02 10:56:21 25 4
gpt4 key购买 nike

当我编译代码时,它给了我一些我认为是我在游戏循环中使用(在Game::StartGame中)线程的错误。从错误中我认为我对Car::myListener方法有一些错误(这是侦听器)键盘输入),我该如何解决?我需要使用类,我在游戏类和汽车类的标题和cpp文件下发布(项目还有其他类)。
Game.h

#ifndef GAME_H
#define GAME_H
#include "Car.h"

class Game
{
public:
bool running;
int count;
int matrix[15][20];
public:
Game();
void gotoXY(int x, int y);
void drawCar(int x, int y);
void drawEnemies(int x, int y);
void drawNails(int x, int y);
void resetBoard();
void creaTab();
int punteggio();
void startGame();
};

#endif // GAME_H

Game.cpp
#include "Game.h"
#include "Nail.h"
#include "EnemiesCar.h"
#include "Car.h"
#include <windows.h>
#include <iostream>
#include <thread>
#include <stdlib.h> /* srand, rand */
#include <ctime>
using namespace std;

Game::Game(){
running = true;
count = 0;
}

void Game::gotoXY(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void Game::drawCar(int x, int y)
{
if(y<20 && y>=-0){
matrix[x][y]=1;
}
}

void Game::drawEnemies(int x, int y)
{
if(y<20 && y>=-0){
matrix[x][y]=2;
}
}

void Game::drawNails(int x, int y)
{
if(y<20 && y>=-0){
matrix[x][y]=3;
}
}

void Game::resetBoard()
{
for(int j=0;j<20;j++){
for(int i=1;i<14;i++){
matrix[i][j]=0;
}
}
}

void Game::creaTab()
{
for(int j=0;j<20;j++){
for(int i=0;i<15;i++){
if(i==0 || i==14){
gotoXY(i,j);
cout<<"b";
}else if(matrix[i][j]==1){
gotoXY(i,j);
cout<<"m";
}else if(matrix[i][j]==2){
gotoXY(i,j);
cout<<"e";
}else if(matrix[i][j]==3){
gotoXY(i,j);
cout<<"c";
}else {
gotoXY(i,j);
cout<<" ";
}
}
}
}

int Game::punteggio()
{
count ++;
return count;
}

void Game::startGame()
{
//Car *mycar = new Car();
Car mycar = Car();
EnemiesCar myEnmCar = EnemiesCar();
Nail nails = Nail();
// starts the second thread (The input listener )
thread myThread(&mycar.myListener, &mycar);

// This is the game engine/game loop
while(running){
resetBoard();
myEnmCar.appear();
myEnmCar.draw();
myEnmCar.move();

nails.appear();
nails.draw();
nails.move();
mycar.draw();
mycar.checkCollusion(&myEnmCar,&running);

creaTab();


punteggio();
gotoXY(20, 0);
cout<<"Point: " << count ;
/*gotoXY(20, 1);
cout<<"Livello: 1" ;*/


Sleep(50);
}
//The game ended

//Show the Game Over
Sleep(1000);
system("cls");
gotoXY(5,4);
cout<<"GAME OVER!!!";
gotoXY(0,0);
Sleep(5000);

// Kills the second thread
myThread.detach();
}

Car.h
#ifndef CAR_H
#define CAR_H
#include "Game.h"
#include "EnemiesCar.h"

class Game;
class EnemiesCar;

class Car
{
public:
int xPos;
int yPos;
Game *games;
public:
Car();
void draw();
void moveLeft();
void moveRight();
void myListener(Car *c);
void checkCollusion(EnemiesCar *EC, bool *running);

};

#endif // CAR_H

Car.cpp
#include "Car.h"
#include "Game.h"
#include "EnemiesCar.h"
#include <windows.h>
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <ctime>
#include <thread>
using namespace std;

Car::Car(){
xPos=2;
yPos=17;
}

void Car::draw(){
this->games->drawCar(xPos,yPos);
this->games->drawCar(xPos-1,yPos+1);
this->games->drawCar(xPos+1,yPos+1);
this->games->drawCar(xPos,yPos+1);
this->games->drawCar(xPos,yPos+2);
}

void Car::moveLeft(){
if(xPos-2 <= 0){
xPos = 2;
}else{
xPos = xPos - 2;
}
}

void Car::moveRight(){
if(xPos+2 >= 14){
xPos = 12;
}else{
xPos = xPos + 2;
}
}

void Car::myListener(Car* c)
{
while(true){
if (GetAsyncKeyState(VK_LEFT) & (0x8000 != 0)){
c->moveLeft();
}
else if (GetAsyncKeyState(VK_RIGHT) & (0x8000 != 0)){
c->moveRight();
}
}
}

void Car::checkCollusion(EnemiesCar *EC, bool *running){
if((EC->xPos == xPos && EC->yPos > 15) || (EC->xPos-1 == xPos+1 && EC->yPos > 17) || (EC->xPos+1 == xPos-1 && EC->yPos > 17)){
*running=false;
}
}

错误
||=== Build: Release in ProgettoProgrammazione (compiler: GNU GCC Compiler) ===|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional||In instantiation of 'struct std::_Bind_check_arity<void (Car::*)(Car*), Car*>':|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional|1538|required from 'struct std::_Bind_simple_helper<void (Car::*)(Car*), Car*>'|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional|1552| required by substitution of 'template<class _Callable, class ... _Args> typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (Car::*)(Car*); _Args = {Car*}]'|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\thread|142|required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Car::*)(Car*); _Args = {Car*}]'|
ProgettoProgrammazione\src\Game.cpp|92|required from here|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional|1426|error: static assertion failed: Wrong number of arguments for pointer-to-member|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional||In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (Car::*)(Car*)>(Car*)>':|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\thread|142|required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Car::*)(Car*); _Args = {Car*}]'|
ProgettoProgrammazione\src\Game.cpp|92|required from here|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional|1505|error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Car::*)(Car*)>(Car*)>'|
MinGW\lib\gcc\mingw32\5.1.0\include\c++\functional|1526|error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Car::*)(Car*)>(Car*)>'|
||=== Build failed: 4 error(s), 7 warning(s) (0 minute(s), 0 second(s)) ===|

最佳答案

错误表明类型不兼容,并且在threadthread myThread(&mycar.myListener, &mycar);的实例化中参数数量错误。
将该行更改为thread myThread(&Car::myListener, &mycar, &blah);。用您想作为参数传递给blah方法的任何Car实例替换mycar.myListener
也就是说,在使用对象的方法构造线程时,第一个参数应该是方法签名,第二个参数应该是对象,然后是该方法的参数。
另请:https://en.cppreference.com/w/cpp/thread/thread/thread

关于c++ - 编译错误c++(需要替换),线程有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63226187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com