inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

홍정모의 따라하며 배우는 C++

12.3 override, final, 공변 반환값

상속-base class undefined

396

의지

작성한 질문수 3

1

안녕하세요, 제가 며칠 잠을 못자서 그런지 계속 상속에서 에러가 나네요..

개념도 다시 한번 정비하려고 강의도 정주행 중인데, 며칠 째 못풀어서 답답해서 일단 질문드립니다.ㅠ

조금 긴 코드이지만 염치 불구하고 혹시 도와주신다면 정말로 감사할 것 같습니다.

 

// 우선 userMenu.h 와 .cpp 입니다.
#pragma once
#ifndef USER_MENU_H
#define USER_MENU_H
#include "menu.h"
#include "gameDisplay.h"

class UserMenu : public Menu {
public:
    UserMenu();
    void u_run();  
    void handleEvents() override;
protected:
    sf::Text subText;
    sf::Text buttonSecond;
    sf::RectangleShape borderSecond;
    sf::Text buttonThird;
    sf::RectangleShape borderThird;
};
#endif 

// .cpp
#include "userMenu.h"

UserMenu::UserMenu() : Menu() {
	subText.setString(" - user mode - ");
	subText.setFont(font);
	subText.setCharacterSize(30);
	subText.setFillColor(sf::Color::White);
	sf::FloatRect managerRect = subText.getLocalBounds();
	subText.setPosition((window.getSize().x - managerRect.width) / 2, (window.getSize().y - managerRect.height) / 2 - 170);

	// 2
	buttonSecond.setString("2. WORD GAME");
	buttonSecond.setFont(font);
	buttonSecond.setCharacterSize(50);
	buttonSecond.setFillColor(sf::Color::White);
	sf::FloatRect buttonSecondRect = buttonSecond.getLocalBounds();        // left                                                  // down
	buttonSecond.setPosition((window.getSize().x - buttonSecondRect.width) / 2, (window.getSize().y - buttonSecondRect.height) / 2 + 10);

	// 3
	buttonThird.setString("3. PROFILE & USERS");
	buttonThird.setFont(font);
	buttonThird.setCharacterSize(50);
	buttonThird.setFillColor(sf::Color::White);
	sf::FloatRect buttonThirdRect = buttonThird.getLocalBounds();        // left                                                  // down
	buttonThird.setPosition((window.getSize().x - buttonThirdRect.width) / 2, (window.getSize().y - buttonThirdRect.height) / 2 + 110);

	window.draw(buttonSecond);
	window.draw(buttonThird);

	u_run();
}

void UserMenu::u_run() {
	while (window.isOpen()) {
		createDisplay();
		createButton();
		handleEvents();
		render();
	}
}

void UserMenu::handleEvents() {
	sf::Event event;
	while (window.pollEvent(event)) {
		sf::Vector2f mousePos(event.mouseButton.x, event.mouseButton.y);
		if (event.type == sf::Event::Closed) {
			window.close();
		}
		else if (event.type == sf::Event::MouseButtonPressed) {
			if (buttonSecond.getGlobalBounds().contains(mousePos)) {
				playClick();
				std::cout << "Second button clicked! Opening new window..." << std::endl;
				window.close();
				GameDisplay gameDisplay;
				gameDisplay.run();
			}
			else if (buttonThird.getGlobalBounds().contains(mousePos)) {
				playClick();
				std::cout << "Third button clicked! Opening new window..." << std::endl;
				window.close();
				CurrentProfile currentProfile; // later must be replaced
				currentProfile.run();
			}
		}
		else if (event.type == sf::Event::MouseMoved) {
			if (buttonFirst.getGlobalBounds().contains(sf::Vector2f(event.mouseMove.x, event.mouseMove.y))) {
				buttonFirst.setFillColor(sf::Color::Red);
			}
			else {
				buttonFirst.setFillColor(sf::Color::White);
			}
			if (buttonSecond.getGlobalBounds().contains(sf::Vector2f(event.mouseMove.x, event.mouseMove.y))) {
				buttonSecond.setFillColor(sf::Color::Red);
			}
			else {
				buttonSecond.setFillColor(sf::Color::White);
			}
			if (buttonThird.getGlobalBounds().contains(sf::Vector2f(event.mouseMove.x, event.mouseMove.y))) {
				buttonThird.setFillColor(sf::Color::Red);
			}
			else {
				buttonThird.setFillColor(sf::Color::White);
			}
		}
	}
}
// menu.h와 .cpp입니다.
#ifndef MENU_H
#define MENU_H

#include "loginSystem.h"
#include "currentProfile.h"

class Menu {
public:
    Menu();
    void run();
    void playClick();

    sf::SoundBuffer clickBuffer;
    sf::Sound clickSound;

    virtual void createDisplay();
    void createButton();
    virtual void handleEvents(); // = 0 해도 결과는 같음(에러)
    void render();

    sf::RenderWindow window;
    sf::Font font;
    sf::Text mainText;
    sf::Text backButton;
    sf::RectangleShape borderRect;
    bool isManager;
    sf::Font m_font;
    sf::Text buttonFirst;
    sf::RectangleShape borderFirst;
};

#endif 
#include "menu.h"

Menu::Menu() : window(sf::VideoMode(1400, 700), "Menu Display") {
}

void Menu::run() {
    while (window.isOpen()) {
        createDisplay();
        createButton();
        handleEvents();
        render();
    }
}

void Menu::createDisplay() {
    if (!font.loadFromFile("font/hangthedj.ttf")) {
        std::cerr << "Failed to load font!" << std::endl;
    }
    mainText.setString("let's practice together");
    mainText.setFont(font);
    mainText.setCharacterSize(70);
    mainText.setFillColor(sf::Color::White);
    sf::FloatRect textRect = mainText.getLocalBounds();
    mainText.setPosition((window.getSize().x - textRect.width) / 2, (window.getSize().y - textRect.height) / 2 - 240);

    // Back button
    backButton.setString("BACK");
    backButton.setFont(font);
    backButton.setCharacterSize(30);
    backButton.setFillColor(sf::Color::White);
    sf::FloatRect loginButtonRect = backButton.getLocalBounds();        // left                                                  // down
    backButton.setPosition((window.getSize().x - loginButtonRect.width) - 250, (window.getSize().y - loginButtonRect.height) / 2 + 180);
    borderRect.setSize(sf::Vector2f(loginButtonRect.width + 10, loginButtonRect.height + 10));
    borderRect.setPosition((window.getSize().x - loginButtonRect.width) - 253, (window.getSize().y - loginButtonRect.height) / 2 + 180);
    borderRect.setFillColor(sf::Color::Transparent);
    borderRect.setOutlineThickness(4);
    borderRect.setOutlineColor(sf::Color::White);
}

void Menu::createButton() {
    if (!m_font.loadFromFile("font/RobotoSlab-Bold.ttf")) {
        std::cerr << "Failed to load font!" << std::endl;
    }
    // 1
    buttonFirst.setString("1. VOCABULARY");
    buttonFirst.setFont(m_font);
    buttonFirst.setCharacterSize(50);
    buttonFirst.setFillColor(sf::Color::White);
    sf::FloatRect buttonFirstRect = buttonFirst.getLocalBounds();        // left                                                 
    buttonFirst.setPosition((window.getSize().x - buttonFirstRect.width) / 2, (window.getSize().y - buttonFirstRect.height) / 2 - 70);
}

void Menu::playClick() {
    if (clickBuffer.loadFromFile("media/click_sound.wav")) {
        clickSound.setBuffer(clickBuffer);
        clickSound.play();
    }
    else {
        std::cerr << "Failed to load click sound file!" << std::endl;
    }
}

void Menu::handleEvents() {
    sf::Event event;
    while (window.pollEvent(event)) {
        sf::Vector2f mousePos(event.mouseButton.x, event.mouseButton.y);
        if (event.type == sf::Event::Closed) {
            window.close();
        }
        else if (event.type == sf::Event::MouseButtonPressed) {
            if (buttonFirst.getGlobalBounds().contains(mousePos)) {
                playClick();
                std::cout << "First button clicked! Opening new window..." << std::endl;
                window.close();
                // insert run
            }
            else if (backButton.getGlobalBounds().contains(mousePos)) {
                playClick();
                std::cout << "BACK button clicked! Opening previous window..." << std::endl;
                window.close();
                LoginSystem loginSystem;
                loginSystem.run();
            }
        }
        else if (event.type == sf::Event::MouseMoved) {
            if (buttonFirst.getGlobalBounds().contains(sf::Vector2f(event.mouseMove.x, event.mouseMove.y))) {
                buttonFirst.setFillColor(sf::Color::Red);
            }
            else {
                buttonFirst.setFillColor(sf::Color::White);
            }
            if (backButton.getGlobalBounds().contains(sf::Vector2f(event.mouseMove.x, event.mouseMove.y))) {
                backButton.setFillColor(sf::Color::Red);
                borderRect.setOutlineColor(sf::Color::Red);
            }
            else {
                backButton.setFillColor(sf::Color::White);
                borderRect.setOutlineColor(sf::Color::White);
            }
        }
    }
}

void Menu::render() {
    window.clear();
    window.draw(mainText);
    window.draw(backButton);
    window.draw(borderRect);
    window.draw(buttonFirst);
    window.draw(borderFirst);
    window.display();
}

c++

답변 1

1

Soobak

안녕하세요, 답변 도우미 Soobak 입니다.

 

강의와 무관한 코드는 질문 답변을 해드리기 어렵습니다.

교수님께서 운영하시는 커뮤니티인, 네이버 카페와 디스코드에서 다른 수강생분들과 질문 답변을 나눠보시는 것을 추천드립니다.


네이버카페 - 홍정모 연구소 (링크)
디스코드 채널 - 홍정모 연구소 (링크)

 

그래도, 저도 공부할 겸 꼼꼼히 읽어보며 도움을 드려보고자 하였으나, 기타 파일들이 없어서 온전히 실행을 할 수 없는 코드라 도움을 드리는 데에 한계가 있네요...

두 에러 중, C3668 'UserMenu::handleEvents': method with override specifier 'override' did not override any base class methods 오류와 관련해서는, Menu 클래스의 handleEvents 멤버 함수가 virtual 로 잘 선언되어 있고, UserMenu 클래스에서 handleEvents 멤버 함수의 정의(반환형, 매개변수 등)도 Menu 클래스의 멤버 함수와 정확히 일치합니다...

따라서, 제 생각에는 C2504 'Menu':base class undefined 에러, 즉, Menu 클래스를 UserMenu 클래스에서 찾을 수 없다는 에러가 문제의 주요 원인 같습니다.

질문자님의 작업 환경을 제가 잘 모르지만, 우선 해당 파일들의 위치와 include 경로를 확인해보시고, 경로도 일치한다면 서로 순환 참조를 하지는 않는지, 각 파일들의 헤더 가드에는 문제가 없는지 확인해보시면 좋을 것 같습니다.

1

의지

정말 죄송합니다. 다음부턴 강의 무관 질문은 하지않도록 조심하겠습니다.

그럼에도 불구하고 도움주신점, 빠르게 답변 해주신점 정말 감사드립니다.

좋은 하루 보내시길 바랍니다:)

강의자료는 어디서 받을 수 있죠?

1

20

2

교재 있나요?

1

139

2

11:11 부근에 Something::temp와 Something::getValue의 앞에 &를 붙이는 이유가 뭔가요? (함수 이름은 포인터(주소)가 아닌가요?)

1

92

3

using namespace std; 선언 후에 std::를 하는 이유가 궁금합니다

1

103

2

cstr직접구현

0

117

3

BubbleSort

1

79

2

숙제 마지막 부분

1

80

2

강의와 똑같이 진행했는데 링킹 에러가 발생합니다.

1

96

2

수업할때 레퍼런스로 사용하는 도서는 어떤 도서인가요??

1

165

2

공변반환형 관련 문의 드립니다.

1

92

2

170강 유니크 포인터에대해 질문있습니다

1

82

1

섹션 5 퀴즈의 답이 이상합니다

1

85

2

이중포인터와 배열이 이해가 안됩니다.

1

159

2

5분 17~5분 34초 객체 잘림 질문

1

80

1

Resource.h 코드 알려주세요

1

73

1

char name[] 배열의 길이와 관련해 일부 궁금점이 생겨서 질문합니다

1

95

2

화면좌측 숫자 보이기

1

116

1

화면 좌측 숫자 보이기

0

68

1

처음 c++ 수강하려는데요. 비주얼스튜디오 2022 다운로드해서 설치하면 되는건가요??

1

139

3

46강 string 버퍼 질문입니다

1

82

2

프로그래머스 수열과 구간 쿼리 2 문제 질문입니다.

1

125

2

[] 범위 검사시 assert 사용 관련 질문

1

91

2

Lecture 클래스 멤버변수 명명 관련

0

93

2

프로그래머스의 대소문자 바꿔서 출력하기 문제를 푸는데요

0

75

1