I'm making a simple checkers game and have a grid system set up, I just want to start setting the parameters shown in the class below;
#include <iostream>
#include <vector>
using namespace std;
class Tracker {
private:
int type_;
int positionx_;
int positiony_;
int checkID_;
public:
Tracker(int type, int positionx, int positiony, int checkID) : type_(type), positionx_(positionx), positiony_(positiony), checkID_(checkID)
{
}
void setType(int type) {
type_ = type;
}
void setPosX(int posx) {
positionx_ = posx;
}
void setPosY(int posy) {
positiony_ = posy;
}
void setCheckID(int ID) {
checkID_ = ID;
}
int getType(int ID) {
return type_;
}
int getPosX(int ID) {
return positionx_;
}
int getPosY(int ID) {
return positiony_;
}
int getCheckID(int positionx, int positiony) {
return checkID_;
}
bool AddPeice(int positionx, int positiony, int checkID) {
}
};
I'm just trying set each pieces location and type in my main class so I'll be able to manipulate them later. The error is
Error (active) no default constructor exists for class "Tracker"
This occurs after I set each piece like so:
vector<Tracker> setup;
Tracker checker;
checker.setType(1);
checker.setCheckID(20 + i);
checker.setPosX(xx);
checker.setPosY(yy);
setup.push_back(checker);
What do i need to add to my tracker class in order to use the Tracker object in other classes?
You should use:
Tracker checker(1, xx, yy, 20 + i);
Instead of:
Tracker checker;
checker.setType(1);
checker.setCheckID(20 + i);
checker.setPosX(xx);
checker.setPosY(yy);
This is because the only constructor that your Tracker
class has is:
Tracker(int type, int positionx, int positiony, int checkID)
So the line:
Tracker checker;
Causes the error:
Error (active) no default constructor exists for class "Tracker"
Also, since you are adding the Tracker
to a vector, you could use emplace_back
if you have access to c++11 features:
std::vector<Tracker> setup;
setup.emplace_back(1, xx, yy, 20 + i);
This can be more efficient than constructing the Tracker
first and then using push_back
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.