How to determine a template parameter at runtime in c++ - 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.

Related

Error C2511 - overloaded member function not found

In the code sample below, class Die is just a base class that AttackDie inherits. When AttackDie is instantiated, it needs data stored in an instance of class StatSys and an integer value. As a result, the constructor for AttackDie has been designed to take an instance of StatSys and a integer as inputs. However, the compiler is throwing a C2511 error on the line containing AttackDie::AttackDie(StatSys * LocalSystem, int Type)
The error is as follows:
AttackDie::AttackDie(StatSys *,int)': overloaded member function not found in 'AttackDie
I don't understand why this error is showing up. I've checked the header and source. The declaration and implementation interfaces both seem to match. Could someone direct me to the problem?
Code Samples
Dice.cpp
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <Dice.h>
#include <StatisticsSystem.h>
using namespace std;
AttackDie::AttackDie(StatSys * LocalSystem, int Type)
{
DataIntegrity = true;
if (Type > -1)
{
switch (Type)
{
case 0:
DieType = "Red";
DieClass = "Damage";
break;
case 1:
DieType = "Green";
DieClass = "Balanced";
break;
case 2:
DieType = "Blue";
DieClass = "Accuracy";
break;
case 3:
DieType = "Yellow";
DieClass = "Surge";
break;
default:
cout << "Unrecognized TypeNum value (>). Initialization of key AttackDie variables failed./n";
DieType = "Error";
DieClass = "Error";
DataIntegrity = false;
}
}
else
{
cout << "Unrecognized TypeNum value (<). Initialization of key AttackDie variables failed./n";
DieType = "Error";
DieClass = "Error";
DataIntegrity = false;
}
if (DataIntegrity)
{
const int GROUP_SIZE = 3;
int Iterator = 0;
int Test[3];
bool CSVDataValid = true;
for (int OuterCounter = 0; OuterCounter <= LocalSystem->NUM_OF_SIDES - 1; OuterCounter++)
{
Test[0] = LocalSystem->AccessCSVData(Type, Iterator);
Test[1] = LocalSystem->AccessCSVData(Type, Iterator + 1);
Test[2] = LocalSystem->AccessCSVData(Type, Iterator + 2);
Iterator += GROUP_SIZE;
if (Test[0] <= -1 || Test[1] <= -1 || Test[2] <= -1)
{
CSVDataValid = false;
}
else
{
Sides[OuterCounter].Damage = Test[0];
Sides[OuterCounter].Surges = Test[1];
Sides[OuterCounter].Accuracy = Test[2];
}
}
if (!CSVDataValid)
{
cout << "Side specific parameters were not set. CSV data not valid.";
DataIntegrity = false;
}
else
{
Total = { 0, 0, 0 };
for (int SideCounter = 0; SideCounter <= 5; SideCounter++)
{
Total.Damage += Sides[SideCounter].Damage;
Total.Surges += Sides[SideCounter].Surges;
Total.Accuracy += Sides[SideCounter].Accuracy;
}
Averages.Damage = Total.Damage / LocalSystem->NUM_OF_SIDES;
Averages.Surges = Total.Surges / LocalSystem->NUM_OF_SIDES;
Averages.Accuracy = Total.Accuracy / LocalSystem->NUM_OF_SIDES;
}
}
}
Dice.h
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>
using namespace std;
struct AttackStatAverages
{
double Damage;
double Surges;
double Accuracy;
};
struct DefenseStatAverages
{
double Blocks;
double Evades;
int Dodges;
};
class Die
{
public:
string GetDieType();
string GetDieClass();
protected:
string DieType;
string DieClass;
bool DataIntegrity;
};
class AttackDie : public Die
{
public:
AttackDie(StatSys * LocalSystem, int Type);
int GetSides(int SideNum, int Parameter);
double GetAverages(int Parameter);
int GetTotal(int Parameter);
~AttackDie();
private:
AttackStats Sides[6];
AttackStatAverages Averages;
AttackStats Total;
};
StatisticsSystem.h
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>
#include <Dice.h>
#include <vector>
using namespace std;
struct DicePackage
{
int Mode;
int Quantity;
int NumberOfPossibilities;
bool Error;
AttackDie* AttackDice[4];
DefenseDie* DefenseDice[3];
};
class StatSys
{
friend class AttackDie;
friend class DefenseDie;
public:
StatSys();
~StatSys();
const double VERSION = 0.1;
int AccessCSVData(int Row, int Column);
private:
static const int MAX_NUM_OF_DICE = 4;
const int METHOD_ERROR_SIZE = 10;
const int NUM_OF_SIDES = 6;
const int GROUP_SIZE = 3;
const int DAMAGE = 0;
const int SURGES = 1;
const int ACCURACY = 2;
const int BLOCKS = 0;
const int EVADES = 1;
const int DODGE = 2;
const int ITERATIONS = 3;
DicePackage DiceSet;
bool CSVDataState;
bool ErrorDataState;
int CSVData[6][18];
vector<string> Errors;
vector<string> ErrorDescriptions;
int StringToInt(string Value);
void LoadCSV();
#include <Dice.h>
#include <StatisticsSystem.h>
should be
#include "Dice.h"
#include "StatisticsSystem.h"
<> is primarily for system headers
"" are for header files in the same directory
Looks like user4581301 was correct. Adding #include <StatisticsSystem.h> to Dice.h fixed the issue since Dice.h does in fact need data from that header.

C++ Thread: terminate called without an active exception

I'm trying to create one array of integers without repeat. To get arrays of length more than 1000, it takes a lot of time to make. So, I thought using thread would be a good decision. But I'm writing something wrong. So far following are my code:
utils.h
#ifndef UTILS_H
#define UTILS_H
typedef long long int64; typedef unsigned long long uint64;
class utils
{
public:
utils();
virtual ~utils();
static int getRandomNumberInRange(int min, int max);
static int* getRandomArray(int size, bool isRepeatAllowed);
protected:
private:
};
#endif // UTILS_H
utils.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
#include <algorithm> // for std::find
#include <sys/time.h>
#include <cctype>
#include <string>
#include <thread>
#include <vector>
#include "utils.h"
utils::utils()
{
}
utils::~utils()
{
}
int utils::getRandomNumberInRange(int min, int max)
{
if (min > max) {
int aux = min;
min = max;
max = aux;
}
else if (min == max) {
return min;
}
return (rand() % (max - min)) + min;
}
void getUniqueInteger(int* arr, int last, int* newVal)
{
int val = *newVal;
while(std::find(arr, arr+last, val) != arr+last)
{
val = utils::getRandomNumberInRange(10, 10000);
}
arr[last] = val;
}
int* utils::getRandomArray(int size, bool isRepeatAllowed)
{
int* arr = new int[size], newVal = 0;
std::vector<std::thread *> threadArr;
for (int i = 0; i < size; i++)
{
newVal = utils::getRandomNumberInRange(10, 1000);
if(!isRepeatAllowed)
{
std::thread newThread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( &newThread);
}
else
{
arr[i] = newVal;
}
}
int spawnedThreadCount = threadArr.size();
if (spawnedThreadCount > 0)
{
for (int j = 0; j < spawnedThreadCount; j++)
{
threadArr[j]->join();
//delete threadArr[j];
}
}
return arr;
}
And calling this in:
main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "utils.h"
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 2 && utils::isInteger(argv[1]))
{
cout << "You have to provide an integer input to this program!!!" << endl;
return 0;
}
int size = stoi( argv[1] );
srand(time(NULL));
int* arr = utils::getRandomArray(size, false);
return 0;
}
Compiling by: g++ -Wall -g -std=c++11 -pthread -o a.out ./utils.cpp ./main.cpp
But, whenever I'm running the program by ./a.out 10, it's terminating by giving the output:
terminate called without an active exception
Aborted (core dumped)
Please help. Thanks in advance.
Your code that creates the thread creates a stack variable that is immediately destroyed. You need to change this:
if(!isRepeatAllowed)
{
std::thread newThread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( &newThread);
}
to this:
if(!isRepeatAllowed)
{
std::thread* newThread = new std::thread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( newThread);
}
Then uncomment your delete line later on.
You create your thread inside the if statement. Then you push a pointer to it by getting a reference. This pointer will not keep the thread object alive, rather when the if is exited your object's destructor is called.
This means that std::terminate is called to terminate the running thread and you're left with a dangling pointer.

Function does not take 1 arguments when passing this into it

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

Incomplete type error when using std::vector with structs

I'm working with c++ STL vectors, and have a vector of structures called projectileList. I'm trying to iterate through the vector, getting and setting values in the struts as I iterate, but my code refuses to compile, with the error 'Incomplete type is not allowed.'
Can anyone please point out what I'm doing wrong:
Code:
ProjectHandeler.h:
#include "stdafx.h"
#include "DataTypes.h"
#include <vector>
class ProjectileHandeler {
private:
int activeObjects;
std::vector<projectile> projectileList;
void projectileUpdater();
public:
ProjectileHandeler(projectile* input[], int projectileCount);
~ProjectileHandeler();
};
#endif
projectileHandeler.cpp
#include "stdafx.h"
#include "DataTypes.h"
#include "ProjectHandeler.h"
#include <vector>
ProjectileHandeler::ProjectileHandeler(projectile* input[], int projectileCount)
{
for (int i = 0; i < projectileCount; i++)
{
projectileList.push_back(*input[i]);
activeObjects += 1;
}
//NO extra slots. Not that expensive.
projectileList.resize(projectileList.size());
}
void ProjectileHandeler::projectileUpdater()
{
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true)
break;
}
}
}
This compiles fine (tested it here: http://codepad.org/cWn6MPJq):
#include <vector>
struct projectile {
bool isEditing;
};
class ProjectileHandeler {
private:
std::vector<projectile> projectileList;
void projectileUpdater()
{
//This bit loops to infinity and beyond! ...or at least untill the handeler is destroyed.
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true) //Throws Incomplete type error
break;
}
}
}
};
int main()
{
}
Notice the removal of *, correct type of loop variable and removal of extra class specifier.

What is the proper way of passing a vector?

Im on year 10 and our teacher wants us to create an original project and using pointers
What I want to do is to create Members and be able to sort the members by there names and print them
When I run my code it says Invalid Access
Team.h
#ifndef TEAM_H
#define TEAM_H
#include "Staff.h"
#include <vector>
#include <iostream>
using std::vector;
class Team: public Staff
{
public:
Team();
~Team();
vector<Staff *> &getVector();
private:
vector<Staff *> myStaffs;
};
#endif // TEAM_H
Team.cpp
Team::Team()
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
vector<Staff*>& Team::getVector()
{
return myStaffs;
}
Command class will do the sorting of team and print all team members
Command.cpp
void Command::printStaffs(vector<Staff*>&myStaffs)
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
std::cout << "Staff ID number: "<< myStaffs[iStaff]->getStaId() << std::endl
<< "Staff Skills 1: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< "Staff Skills 2: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< "Staff Skills 3: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< std::endl;
}
}
Command.h
#ifndef CommandH
#define CommandH
#include "Team.h"
#include <vector>
#include <iostream>
using std::vector;
class Command: public Team
{
public:
Command(){}
~Command(){}
void sortVector(vector<Staff* >&vectorTemp);
void printStaffs(vector<Staff* >&);
private:
vector<Staff *> vectEmployee;
};
//--------------------------------------------------------------------------
#endif
main.cpp
#include <iostream>
#include <conio.h>
#include "Team.h"
#include "Command.h"
int main()
{
Team t;
Command c;
c.printStaffs(t.getVector());
getch();
return 0;
}
Staff.h
#ifndef STAFF_H
#define STAFF_H
#include <cstdlib>
#include <ctime>
#include <string>
using std::rand;
class Staff
{
public:
Staff();
~Staff();
static Staff* createStaff(int); // creates staffs
int** getStaSkill();
int getStaId(); // returns Staff ID
static int genRanNum(int); //Generate random number
private:
int *staSkill[3];
int staId;
//int staDeptAsigned;
};
#endif
Staff.cpp
#include "Staff.h"
Staff::Staff()
{
*staSkill = new int[3];
}
Staff *Staff::createStaff(int s)
{
Staff *staff = new Staff();
staff->staId = s;
*(staff->staSkill[0]) = genRanNum(10);
*(staff->staSkill[1]) = genRanNum(10);
*(staff->staSkill[2]) = genRanNum(10);
return staff;
}
int** Staff::getStaSkill()
{
return staSkill;
}
int Staff::getStaId()
{
return staId;
}
int Staff::genRanNum(int num)
{
return 1 +(std::rand()%num);
}
Staff::~Staff(){}
When you construct a Team, you have the following constructor:
Team::Team()
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
However, myStaffs is a member of Team and gets default constructed as empty, so nothing happens here since myStaffs.size() == 0.
Calling printStaffs on this Team::getVector() will correctly inform you that the vector is empty:
int main()
{
Command c;
Team t; // t.myStaffs will be empty
c.printStaffs(t.getVector()); // passes an empty vector to printStaffs
return 0;
}
You might want to pass a number to your Team constructor to create that many staffs:
Team::Team(int number_of_staff)
{
for(unsigned int iStaff = 0; iStaff < number_of_staff; iStaff++)
{
myStaffs.push_back(createStaff(iStaff));
}
}
int main()
{
Command c;
Team t(5); // t.myStaffs will contain 5 staff members
c.printStaffs(t.getVector()); // passes vector of 5 staff
return 0;
}