Hey guys, thanks for all the help thus far. Let's tackle this one.
Here is my struct I'm trying to create:
struct Map {
int32_t width;
int32_t height;
std::vector<Coordinate> map;
Coordinate& sPos;
Coordinate& ePos;
};
And here is the function that creates the struct:
Map parse_input(std::istream& stream) {
std::vector<Coordinate> map;
int32_t height = 0;
int32_t width = 0;
Coordinate sPos;
Coordinate ePos;
for(std::string line; std::getline(stream, line); ) {
std::istringstream iss(line);
char temp;
width = 0;
while(iss >> temp) {
if(temp == 'S') {
sPos = Coordinate{ width, height, 0, temp };
map.push_back(Coordinate{ width, height, 0, temp });
}
else if(temp == 'E') {
ePos = Coordinate{ width, height, 100, temp };
map.push_back(Coordinate{ width, height, 100, temp });
} else {
map.push_back(Coordinate{ width, height, temp-'a', temp });
}
width++;
}
height++;
}
auto& test = map[(sPos.y * height) + sPos.x];
auto value = (sPos.y * height) + sPos.x;
return Map{ width, height, map, test, map[(ePos.y * height) + ePos.x] };
}
The goal here is that I want to create my struct in place in the return with references to those objects in the map vector. REMEMBER map is not a <map> it is a VECTOR!!!!
For some fucking reason, after the return Map, the object after inspection has a correct reference to ePos but some random fucking UB data for sPos. Guess what? test returns the correct reference, value evaluates to 0. How the fuck can ePos work and not sPos?