So, with about a semesters worth of C knowledge, I decided to get into C++ in the past few weeks. After a much smaller program that prints a few numbers (without the use of classes), I decided to figure out just how far I can get before I get stuck somewhere with my next program.
Ended up hitting a problem at std::thread, and could not figure out how to fix the compile time error.
A few reference pieces of code (no errors here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
enum buildingstatus
{
NONE,
BUILDING,
BUILT,
};
class BuildTimerStruct
{
public:
int TotalTime;
int* timelefttimer;
int buildingid;
BuildTimerStruct();
~BuildTimerStruct();
};
BuildTimerStruct::~BuildTimerStruct()
{
delete this;
}
class Planet
{
public:
Planet();
~Planet();
void build();
void DisplayStats();
int PlanetID;
static int globalplanets;
void BuildingBuilt(int buildingtype);
void DisplayBuildings();
void Planet::BuildingTimer (BuildTimerStruct);
void Planet::TimeUntilCompleteion(const int , const std::string );
private:
std::string name;
buildingstatus *building;
int timeleft;
friend class GameSystem;
};
|
Compile time error occurs at the std::thread declaration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
void Planet::build()
{
int buildinginput=-1;
std::cout << "Building menu initiated on planet " << name << "." << std::endl;
DisplayBuildings();
for(int ii=0; ii < MaxBuildingSelection; ii++)
std::cout << "To build " << BLDGID(ii) << ", " << "please type in \"" << ii << "\"." << std::endl;
std::cout << "To exit the building menu, please type in 0." << std::endl;
while(buildinginput!=0)
{
std::cin >> buildinginput;
if (buildinginput>9000)
{
DisplayBuildings();
for(int iii=0; iii < MaxBuildingSelection; iii++)
std::cout << "To build " << BLDGID(iii) << ", " << "please type in \"" << iii << "\"." << std::endl;
}
else if (buildinginput>0&&buildinginput<MaxBuildingSelection)
{
if (building[buildinginput]!=NONE)
{
BuildingBuilt(buildinginput);
}
else
{
BuildTimerStruct* tempBLDGTIMER = new BuildTimerStruct;
tempBLDGTIMER->TotalTime=(BLDGTIME(buildinginput));
tempBLDGTIMER->timelefttimer=&timeleft;
tempBLDGTIMER->buildingid=buildinginput;
std::thread foo(&Planet::BuildingTimer, *tempBLDGTIMER); //Compile time error
foo.detach();
buildinginput=0;
break; //to skip the rest of the junk
}
}
else
std::cout <<"No building or invalid number selected, ";
std::cout << "please enter a meaningful number to build or 0 to exit building menu." << std::endl;
std::cout << "To display building menu, type a number over 9000." << std::endl;
buildinginput=-1;
}
}
|
The timer std::thread is trying to get working:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void Planet::BuildingTimer (BuildTimerStruct Starter)
{
this->building[Starter.buildingid]=BUILDING;
this->timeleft=Starter.TotalTime;
int InitialTime=clock();
while(Starter.timelefttimer>0)
{
this->timeleft=this->timeleft-(clock()-InitialTime)/CLOCKS_PER_SEC;
}
this->building[Starter.buildingid]=BUILT;
if (DebugMode==true)
std::cout << BLDGID(Starter.buildingid) <<" has been built on " << name << "." <<std::endl;
}
|
std::thread did not like functions taking in more than 1 argument, so I made a class (instead of a struct so I can use "delete this;") to pass on arguments. A few things need to be pointers to allow the separate thread/function to change variables as it works.
When I try to compile this program, it gives me this error:
Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 |
which leads me to this:
_VARIADIC_EXPAND_0X(_CLASS_BIND, , , , )
I'm clueless on what's causing std::thread to fail.