Function does not take 1 arguments when passing this into it - c++

I'm trying to pass an instance of this (the instance in question being the GameEngine class) into my PlayerBrain.run method which takes a GameEngine * const argument corresponding to what this is. I'm curious as to what would cause such an error to be thrown in this case, especially when there are no alternative function definitions for run() or anything like that. PlayerBrain is its own class, not descending from anything. So there's no alternative definitions to run that might cause problems. Here's the relevant code:
All public methods in PlayerBrain.h
#pragma once
#include "GameEngine.h"
#include "Species.h"
#include "Neuron.h"
#include "Genome.h"
#include "GenePool.h"
#include "Gene.h"
#include <sys/stat.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
class PlayerBrain {
private:
int popSize;
int stalenessThreshold;
int timeoutConstant;
int currentTimeout;
int maxNodes;
int farthestDistance;
bool buttons[6]; // JASON - shoot, left, right, up, down, jump in that order
GenePool p;
bool fileExists(string filename);
bool fitnessAlreadyMeasured();
void nextGenome();
void evaluateCurrent(Genome * g);
void evaluateNetwork(vector<Neuron*> * network, vector<int> inputs);
void loadPool(string filename);
double sigmoid(double sum);
vector<int> getInputs();
Genome templateGenome();
void pressAKey();
void unPressAKey();
vector<INPUT *> oldbuttons;
public:
PlayerBrain();
void intialize();
void run(GameEngine const *);
void close();
};
The actual implementation in its .cpp
void PlayerBrain::run(GameEngine const * g) {
Genome * current = p.getCurrentGenome();
int xPos = 0;
int bossLives = 0;
double fitness;
double timeoutBonus;
unPressAKey();
if ((p.getCurrentFrame() % 5) == 0)
evaluateCurrent(current);
pressAKey();
xPos = g->getXPosAckVar();
bossLives = g->getBossLivesAckVar();
if (xPos > farthestDistance) { farthestDistance = xPos; currentTimeout = timeoutConstant; }
currentTimeout--;
timeoutBonus = p.getCurrentFrame() / 4;
if ((currentTimeout + timeoutBonus) <= 0) {
fitness = (farthestDistance - (p.getCurrentFrame() / 2)) + 2000 * bossLives;
if (fitness == 0) { fitness = -1; }
current->setFitness(fitness);
if (fitness > p.getMaxFitness()) { p.setMaxFitness(fitness); }
p.setCurrentSpecies(0);
p.setCurrentGenome(0);
while (fitnessAlreadyMeasured()) { nextGenome(); }
}
p.setCurrentFrame(p.getCurrentFrame() + 1);
}
The function call in GameEngine.cpp`
while (true)
{
......//irrelevant stuff
if (tickTimeNow > tickTimeTrigger)
{
if (dTimeTrigger > m_PaintTimeTrigger)
{
....
brain->run(this);
....
}
else Sleep(1);
}
else WaitMessage();
}
}
The error I get is as follows:
Error C2660 'PlayerBrain::run': function does not take 1 arguments

Related

How to determine a template parameter at runtime in c++

I define a ThreadPoolclass, and it has a memeber: std::array<Worker, ThreadNum> Workerlist.
The code is as follows:
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <pthread.h>
#include <memory>
#include "Worker.h"
#include <vector>
#include <array>
const int MAX_THREAD_NUM = 16;
class ThreadPool
{
private:
const unsigned int ThreadNum;
std::shared_ptr<EventLoop> MainLoop;
std::array<std::shared_ptr<Worker>, ThreadNum> WorkerList;
std::array<std::shared_ptr<EventLoop>, ThreadNum> EventLoopList;
unsigned int NextLoopIndex;
public:
ThreadPool(std::shared_ptr<EventLoop> loop, int threadNum = 12);
~ThreadPool();
void RunThreadPool();
std::shared_ptr<EventLoop> GetNextEventLoop();
}
#endif
ThreadPool.cpp
ThreadPool::ThreadPool(std::shared_ptr<EventLoop> loop, int threadNum): MainLoop(loop), ThreadNum(threadNum), NextLoopIndex(0)
{
if (ThreadNum<=0 || ThreadNum> MAX_THREADS)
{
LOG << "The num of threads is out of range.\n";
}
}
ThreadPool::~ThreadPool() {}
void ThreadPool::RunThreadPool()
{
WorkerList.fill(std::make_shared<Worker>());
for (auto i = 0; i < ThreadNum; i++)
{
EventLoopList.at(i) = WorkerList.at(i)->ReturnEventLoopPtr();
}
}
std::shared_ptr<EventLoop> ThreadPool::GetNextEventLoop()
{
if (!EventLoopList.empty())
{
std::shared_ptr<EventLoop> nextLoop = EventLoopList[NextLoopIndex];
NextLoopIndex = (NextLoopIndex + 1) % ThreadNum;
return nextLoop;
}
return;
}
The error message is:
invalid use of data member ThreadPool::ThreadNum
In my opinion, the template parameter ThreadNum should be a constant, but now I need to infer its value when the class is constructed. Any solutions? Thank you very much.

classes not recognied inside another classeven with headers

I have to make a snake and ladders game with classes (MyGame, Board, Player, Dice). MyGame needs all the other classes at some point or another thus I have the headers for the other classes in the MyGame.h file. Yet I get 3 errors that read:
Line 18 -----"error: ‘Board’ has not been declared."
Line 18 -----"error: ‘Player’ has not been declared."
Line 19 -----"error: ‘Player’ has not been declared."
An object MyGame is initialized in my main (skanes.cpp), and then inside the function MyGame::start() the other objects are created. I thought that maybe the classes Board or Player require something from MyGame in order to be build thus cycling but Player and Board are not dependent of MyGame besides the initialization of the obejct. HELP!
MyGame.h
#ifndef MYGAME_H
#define MYGAME_H
#include "Board.h"
#include "Player.h"
#include "Dice.h"
#include "Player.h"
class MyGame
{
protected:
static const int numPlayers = 2;
public:
MyGame();
~MyGame();
void start();
void play(Player[], Dice, Board); <-------Line 18
void win(Player[]); <-------Line 19
int getNumPLayers();
};
#endif
MyGame.cpp
#include <iostream>
#include <vector>
#include "MyGame.h"
#include "Board.h"
#include "Player.h"
#include "Dice.h"
MyGame::MyGame()
{
}
MyGame::~MyGame()
{
}
void MyGame::start()
{
Board brd;
Player plyr[numPlayers];
Dice dc;
while (plyr[0].getPosition() != brd.getBoardSize() && plyr[1].getPosition() != brd.getBoardSize() && plyr[numPlayers - 1].getTurn() <= plyr[numPlayers - 1].getMaxTurn())
play(plyr, dc, brd);
win(plyr);
}
void MyGame::play(Player p[], Dice d, Board b)
{
for (int i = 0; i < b.getBoardSize(); i++)
{
p[i].setPosition(d.roll());
if(p[i].getPosition() > b.getBoardSize())
{
p[i].setPosition( (b.getBoardSize() - p[i].getPosition()) * 2 );
}
if (b.getType(p[i].getPosition()) == 'S')
p[i].setPosition(-b.getSnakeLadderMove());
else if (!b.getType(p[i].getPosition()) == 'L')
p[i].setPosition(b.getSnakeLadderMove());
p[i].setTurn();
}
}
void MyGame::win(Player p[])
{
for (int i = 0; i > numPlayers; i++)
{
if (p[i].getPosition() == 30)
std::cout << "Payer " << i << "wins!!" << std::endl;
}
}
Board.h
#ifndef BOARD_H
#define BOARD_H
#include "MyGame.h"
class Board
{
public:
Board();
~Board();
bool getType(int);
int getNumeber(int);
int getSnakeLadderMove();
int getBoardSize();
private:
struct tile
{
char type;
int number;
};
static const int boardSize = 30;
static const int snakeLadderMove = 3;
tile place[boardSize];
};
#endif
Board.cpp
#include <stdlib.h>
#include <time.h>
#include "Board.h"
Board::Board()
{
int count = 0;
//initialize random seed to randomize snakes and ladders.
srand(time(NULL));
for (int k = 0; k < boardSize; k++)
{
place[k].type = 'N';
place[k].number = k + 1;
}
while(count <= 3)
{
int index = rand() % boardSize + 1;
while (index < 4)
{
index = rand() % boardSize + 1;
// Makes sure it only replaces tiles with type = 'N'
while(getType(index) != 'N')
index = rand() % boardSize + 1;
}
place[index].type = 'S';
while (index > boardSize - 3)
{
index = rand() % boardSize + 1;
// Makes sure it only replaces tiles with type = 'N'
while(getType(index) != 'N')
index = rand() % boardSize + 1;
}
place[index].type = 'L';
count++;
}
}
Board::~Board()
{
}
int Board::getNumeber(int index)
{
return place[index].number;
}
bool Board::getType(int index)
{
return place[index].type;
}
int Board::getBoardSize()
{
return boardSize;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "MyGame.h"
#include "Board.h"
class Player
{
public:
Player();
~Player();
void setPosition(int);
void setTurn();
int getPosition();
int getTurn();
int getMaxTurn();
int getNumPlayers();
private:
static const int maxTurn = 20;
int position;
int turn;
};
#endif
Player.cpp
#include <iostream>
#include "Player.h"
#include "Board.h"
Player::Player()
{
/*
In order for the setters to work position and turn
have to be equal to 1;
*/
position = 1;
turn = 1;
}
Player::~Player()
{
}
void Player::setPosition(int move)
{
//Assumes constructor setted the value to 0
position += move;
;
}
void Player::setTurn()
{
//Assumes constructor sette4d the value to 0
turn++;
}
int Player::getPosition()
{
return position;
}
int Player::getTurn()
{
return turn;
}
int Player::getMaxTurn()
{
return maxTurn;
}
Dice.h
#ifndef CDADO_H_INCLUDED
#define CDADO_H_INCLUDED
#include <ctime>
#include <cstdlib>
class Dice{
public:
Dice();
int roll();
};
#endif
Dice.cpp
#include <iostream>
#include "Dice.h"
using namespace std;
Dice::Dice()
{
srand(time(0));
}
int Dice::roll()
{
return (rand() % 6) + 1;
}
skanes.cpp //It was supposed to be snakes.
#include <iostream>
#include "MyGame.h"
using namespace std;
int main()
{
MyGame snakes;
snakes.start();
}
#define BOARD_H
#include "MyGame.h" // <--- here lies the problem
Do not include MyGame.h in Board.h and Player.h. You have a circular dependency.
Pretend that you are a C++ compiler that's compiling your Player.cpp:
#include <iostream>
#include "Player.h"
At this point the compiler starts reading Player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include "MyGame.h"
Now your C++ compiler goes to read MyGame.h. Remember that this is all that your compiler has processed up to now. It has not processed anything else.
In MyGame.H there's another #include "Player.H", however it does absolutely nothing whatsoever, since the include guard was defined, so the second inclusion of Player.H becomes a big fat nothing.
Your compiler continues to process MyGame.H, and finds a reference to some mysterious class named Player that has never been defined anywhere. That's the explanation for your compilation error.
There does not appear to be any need for Player.H to include MyGame.H, so just get rid of that include.
It's a circular reference that's completely unneeded, and easily fixable by getting rid of it. If you do need real circular references between header files, your C++ textbook should have a good explanation of what forward references are, and how to use them.

Error invalid new-expression of abstract class type FancyPlayer cpp

i have this code at main.cpp :
#include <iostream>
#include <limits>
#include <conio.h>
#include "Ghost/SmartGhost/SmartGhost.h"
#include "Player/Player.h"
#include "PlayerInfo/PlayerInfo.h"
#include "GameBoard/GameBoard.h"
#include "ManualPlayer/ManualPlayer.h"
#include <vector>
using namespace std;
class Position {
public:
int x;
int y;
Position();
Position(int xo,int yo) {x=xo; y=yo;} };
class FancyPlayer : public Player
{
private: vector<Position> visited; //vector of visited nodes int size[2]; //width and height of maze Position start; vector <Position> path; //last visited node(backtrack) public:
void init(int width,int height,int x,int y) {
size[0]=width;
size[1]=height;
start.x=x; //starting position
start.y=y; visited.push_back(Position(start.x,start.y)); path.push_back(Position(start.x,start.y)); }
bool isValid(char ** ViewAround,int x,int y) {
return (ViewAround[x][y]!='#' && ViewAround[x][y]!='*'); }
bool isvisited(int x,int y){
bool f=false;
for(int i=0;i<visited.size();i++) {
if (visited[i].x==x && visited[i].y==y)
f=true;
} return f; }
int getMove(char** ViewAround) {
if (isValid(ViewAround,1,2) && !isvisited(start.x-1,start.y))
{
visited.push_back(Position(start.x-1,start.y));
path.push_back(Position(start.x-1,start.y));
start.x--;
return 0; }
else if (isValid(ViewAround,3,2) && !isvisited(start.x+1,start.y))
{
visited.push_back(Position(start.x+1,start.y));
path.push_back(Position(start.x+1,start.y));
start.x++;
return 1; } else if (isValid(ViewAround,2,3) && !isvisited(start.x,start.y+1))
{
visited.push_back(Position(start.x,start.y+1));
path.push_back(Position(start.x,start.y+1));
start.y++;
return 2; } else if (isValid(ViewAround,2,1) && !isvisited(start.x,start.y-1))
{
visited.push_back(Position(start.x,start.y-1));
path.push_back(Position(start.x,start.y-1));
start.y--;
return 3; } else
{
if (path[path.size()-1].x<start.x){
path.pop_back();
start.x++;
return 1;
}
if (path[path.size()-1].x>start.x){
path.pop_back();
start.x--;
return 0;
}
if (path[path.size()-1].y<start.y){
path.pop_back();
start.y++;
return 2; }
if (path[path.size()-1].y>start.y){
path.pop_back();
start.y--;
return 3;
}
}
}
std::string getName() { return std::string("mplampla"); }
std::string getId() { return std::string("cs141065"); }
};
int main() {
std::vector<ObjectInfo *> PlayersVector; //vector with players engaged
PlayersVector.push_back(new PlayerInfo(*new FancyPlayer(), 'A')); //Erase the comments to play with keyboard
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'B')); //Fool Standby player
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'C')); //Fool Standby player
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'D')); //Fool Standby player
GameBoard *MainGameObject; //Main Game Object
InfoBoard *ptrToInfoBoard = new InfoBoard();
for (int j = 0; j < PlayersVector.size(); ++j) {
ptrToInfoBoard->addPlayer(*static_cast<PlayerInfo *>(PlayersVector[j]));
}
std::ostringstream he;
std::vector<std::string>*ptr = GameBoard::getMapFileNames(DEVCPP_PATH);
for (unsigned int i = 0; i < ptr->size(); ++i) {
he<<DEVCPP_PATH<<ptr->operator[](i);
for (int j = 0; j < EATCH_MAP_PLAY_TIMES; ++j) {
MainGameObject=new GameBoard(true,he.str().c_str(),PlayersVector,EXETASI_MODE,ptrToInfoBoard);
MainGameObject->StartGame();
delete(MainGameObject);
getchar();
}
he.str("");
}
while(1); }
Player.h code :
#ifndef GHOST_PLAYER_H
#define GHOST_PLAYER_H
#include <iostream>
#include <windows.h>
class Player{
private:
public:
Player(){}
virtual std::string getName()=0 ;
virtual std::string getId()=0 ;
virtual int getMove(const char **ViewAround)=0;
virtual void init(int width,int height,int CurrentX,int CurrentY )=0;
};
#endif //GHOST_PLAYER_H
When i am putting at main the
*new FancyPlayer(), 'A')) i am getting an error that telling "[Error] invalid new-expression of abstract class type 'FancyPlayer'" , if i put this line in comment the code works properly...
the stupidplayer code :
StupidPlayer.h :
#ifndef GHOST_STUPIDPLAYER_H
#define GHOST_STUPIDPLAYER_H
#include "../Player/Player.h"
#include "../Move.h"
class StupidPlayer: public Player {
int getMove(const char **ViewAround);
void init(int width,int height,int x,int y);
std::string getName();
std::string getId();
};
#endif //GHOST_STUPIDPLAYER_H
StupidPlayer.cpp :
#include <cstdlib>
#include "StupidPlayer.h"
#include <fstream>
int StupidPlayer::getMove(const char **ViewAround) {
std::ofstream he("inner.txt");
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
he<<ViewAround[i][j];
}
he<<'\n';
}
return STAND;
}
void StupidPlayer::init(int width, int height, int x, int y) {
}
std::string StupidPlayer::getName() {
return std::string("StupidPlayer");
}
std::string StupidPlayer::getId() {
return std::string("cs161119");
}
i cant see any difference between stupidplayer and fancy... what can i do... in order to make fancy works properly... it may be a problem that have 2 classes in use for fancy?

Class Not Declared in Scope

Getting a "throttle not declared in this scope" error when I attempt to compile the main.cpp. I am very new to c++, so bear with me. I have the #include "throttle.h" in the headers for both cpp files, so I am not sure why when i try to create a throttle object is is not declared...
main.cpp file:
#include <stdio.h>
#include "throttle.h"
using namespace std;
int main(int argc, char **argv)
{
throttle throt1(5, 0);
throttle throt2(4, 0);
return 0;
}
throttle.h file:
#ifndef MAIN_SAVITCH_THROTTLE
#define MAIN_SAVITCH_THROTTLE
namespace main_savitch_2A
{
class throttle
{
public:
// CONSTRUCTORS
//throttle( );
//throttle(int size);
throttle(int size = 1, int start = 0); //by adding this default
//constructor the other two
//are not needed
// MODIFICATION MEMBER FUNCTIONS
void shut_off( ) { position = 0; }
void shift(int amount);
// CONSTANT MEMBER FUNCTIONS
double flow( ) const
{
return position / double(top_position);
}
bool is_on( ) const
{
return (position > 0);
}
int get_top_position()const;
int get_position()const;
friend bool operator <(const throttle& throt1, const throttle& throt2);
//postcondtion: returns true if flow of throt1 < flow of throt2.
//return false if flow of throt1 > flow of throt2
private:
int top_position;
int position;
};
}
#endif
throttle.cpp file :
#include <cassert> // Provides assert
#include "throttle.h" // Provides the throttle class definition
using namespace std; // Allows all Standard Library items to be used
namespace main_savitch_2A
{
//throttle::throttle( )
//{ // A simple on-off throttle
//top_position = 1;
//position = 0;
//}
//throttle::throttle(int size)
// Library facilities used: cassert
//{
//assert(size > 0);
//top_position = size;
//position = 0;
//}
throttle::throttle(int size, int start)
{
assert(size > 0);
assert(start = 0);
top_position = size;
position = start;
}
void throttle::shift(int amount)
{
position += amount;
if (position < 0)
position = 0;
else if (position > top_position)
position = top_position;
}
bool operator <(const throttle& throt1, const throttle& throt2)
{
return(throt1.flow() < throt2.flow());
}
int throttle::get_top_position()const
{
return top_position;
}
int throttle::get_position()const
{
return position;
}
}
In your main, it should be main_savitch_2A::throttle throt1(5, 0);.
The same for throt2.
See namespaces for further details.

What does "duplicate symbol _heating_unit in BangBangControlTest.o and BangBangControl.o" mean?

Im receiving this error when trying to compile my code.
$ g++ -o BangBangControlTest BangBangControl.o BangBangControlTest.o
ld: duplicate symbol _heating_unit in BangBangControlTest.o and BangBangControl.o for architecture x86_64
collect2: ld returned 1 exit status
I am new to C++ and can't find out what is wrong. I've searched through many tutorials and looked at similar error messages received by other stack users. Here are my classes.
"BangBangControlTest.cpp"
// Test function
#include <iostream>
#include "BangBangControl.h"
using namespace std;
int main(){
BangBangControl control(50, true, 75);
for(int i = 0; i < 50; i++){
std::cout << "Temp = " << control.update() << endl;
}
return 0;
}
"BangBangControl.cpp"
#include <iostream>
#include "BangBangControl.h"
using namespace std;
BangBangControl::BangBangControl(int temp, bool isOn, int initialTemp){
heating_unit = HeatingUnit(isOn, initialTemp);
temp_to_maintain = temp;
}
void BangBangControl::setTemp(int temp){temp_to_maintain = temp;}
int BangBangControl::getTemp(){return temp_to_maintain;}
int BangBangControl::update(){
int b=heating_unit.tick();
if (b > temp_to_maintain + 2) heating_unit.turnOff(); if (b < temp_to_maintain - 2) heating_unit.turnOn();
return b;
}
"BangBangControl.h"
// BangBangControl header
#include <iostream>
#include "HeatingUnit.h"
using namespace std;
HeatingUnit heating_unit;
int temp_to_maintain;
class BangBangControl{
public:
BangBangControl(int, bool, int);
void setTemp(int);
int getTemp();
int update();
};
"HeatingUnit.cpp"
// HeatingUnit class implementation
#include <iostream>
#include "HeatingUnit.h"
using namespace std;
HeatingUnit::HeatingUnit(bool a, int b){
isOn = a;
temp = b;
}
void HeatingUnit::turnOn(){isOn = true;}
void HeatingUnit::turnOff(){isOn = false;}
int HeatingUnit::tick(){
if(isOn && temp <= 100){
return ++temp;
}
else if((!isOn) && temp >= 0){
return --temp;
}
else{
return temp;
}
}
"HeatingUnit.h"
#include <iostream>
using namespace std;
class HeatingUnit{
public:
bool isOn;
int temp;
HeatingUnit();
HeatingUnit(bool, int);
void turnOn();
void turnOff();
int tick();
};
You see that HeatingUnit heating_unit; in your header file? You need to put extern in front of it, and copy the original version without the extern to the .cpp file, optionally specifying an initial value there.
You can read more about this here: How do I use extern to share variables between source files?