更新时间:2023-11-29 09:55:40
You have two different variables both called coords
. One is a private member variable, the other is local to the constructor. Because the local variable you create in the constructor shadows the member variable, the constructor never initializes the member variable.
Try this instead:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
class Ship{
private:
int length;
bool direction; //false = left, true = down
vector< pair <int,int> > coords; // *** CHANGE HERE
public:
Ship(int x, int y, bool, int);
void printship();
};
Ship::Ship(int x, int y, bool dir, int l){
length = l;
if (dir){
for (int i = 0; i < l; i++){
coords.push_back(make_pair(x, y+i)); // *** CHANGE HERE
}
}
else{
for (int i = 0; i < l; i++){
coords.push_back(make_pair(x+i, y)); // *** CHANGE HERE
}
}
}
void Ship::printship(){
for (int i = 0; i < length; i++){
cout << "x: " << coords[i].first << ", y: " << coords[i].second << endl;
}
}
int main(){
Ship tests(2,3,true,3);
tests.printship();
return 0;
}