잘못된 트리를 완전히 짖어 내 버릴 수는 있지만 컴파일 만하면 충돌이 발생합니다. 나는 단지 C++을 주로하고 있으며, 주로 자바 배경에서 오는 2 주간의 마지막 2 주입니다.C++ 포인터 GUI QT
누구나 객실 클래스가 있습니다. 북쪽, 남쪽, 동쪽, 서쪽에 4 개의 출구를두고 싶습니다. 나는 4 개의 방 포인터를 취하는 setExits() 메소드를 생성 할 생각이 들었다. 각 Room 객체에는 해당 포인터를 호출하는 goNorth() 등의 메서드가 있습니다.
Room::Room(QString name, QString shortD, QString longD, QImage roomImage)
{
this->name = name;
this->shortD = shortD;
this->longD = longD;
this->roomImage = roomImage;
}
QString Room::shortDescription()
{
return shortD;
}
QString Room::longDescription()
{
return longD;
}
QImage Room::showImage()
{
return roomImage;
}
void Room::setExits(Room *north, Room *south, Room *east, Room *west)
{
this->north = north;
this->south = south;
this->east = east;
this->west = west;
}
Room* Room::goNorth()
{
return north;
}
저는 이것을 이렇게 부르고 설정합니다.
void TheIslandMain::createRooms()
{
//Room *forest, *lake; - declare as global in theislandmain.h?
forest = new Room(readRooms("../Island/Rooms/forest.name"), readRooms("../Island/Rooms/forest.short"), readRooms("../Island/Rooms/forest.long"), getImage("../Island/Rooms/forest.jpg"));
//forest->setExits(lake, currentRoom, currentRoom, currentRoom);
lake = new Room(readRooms("../Island/Rooms/lake.name"), readRooms("../Island/Rooms/lake.short"), readRooms("../Island/Rooms/lake.long"), getImage("../Island/Rooms/lake.jpg"));
currentRoom = forest;
}
gui의 버튼을 클릭하면 다음 코드가 사용됩니다.
void TheIslandMain::on_northButton_clicked()
{
ui->roomDesc->clear();
currentRoom = lake;
//currentRoom = currentRoom->goNorth();
ui->actionLabel->setText(currentRoom->shortDescription());
ui->imageArea->setPixmap(QPixmap::fromImage(currentRoom->showImage()));
}
내가 사용하고자하는 코드를 주석 처리 했으므로 오류가 발생합니다. 이제 코드가 정상적으로 작동합니다. 코드의 주석을 제거하면 다시 파일을 컴파일하지만 북쪽 버튼을 클릭하면 255 오류가 발생하고 충돌이 발생합니다.
이것이 합법적 인 것인지 확실하지 않습니다. 다만 생각한 것입니다. 나는 틀린 나무를 완전히 짖고 있니? 제가 말했듯이, 저는 C++ 만 배우고 있으며 실제로 클래스의 포인터에 대해서는 아무 것도하지 않았습니다.
코멘트 된 코드에 대한 이해는 currentRoom 변수의 메모리 참조를 룸 객체에있는 참조 북으로 변경한다는 것이고, 그 자체는 기본 클래스에 정의 된 참조에서 가져온 것입니다.
미안하다면 어리 석다!
'호수'란 무엇입니까? 또한 클래스 멤버 변수에 접두어 나 접미어를 붙이는 것이 일반적이므로, 일반적으로 생성자에서 같은 이름의 arg가있을 때는 모든 곳에서 'this->'할 필요가 없습니다. – cmannett85
미안, 나는 그렇게 말했어 야했다. 나는 현재 2 개의 룸 오브제가 설치되어 있으며, 숲과 호수가 있습니다. 기본 currentRoom 인 숲에서 북쪽으로 가면 호수로 연결됩니다. 그게 우리가 보았던 방식입니다. (이것에 관해서) - 아기 단계, 나는 우리가 자바에서 왔기 때문에 추측합니다! :) –
당신이 게시 한 (주석이 아닌) 코드에서'setExits'를 호출하지 않고, 의심스러워 보이는 주석 된 코드 (당신이 할당하기 전에 currentRoom을 참조). – Mat