exercism / cpp

Exercism exercises in C++.
https://exercism.org/tracks/cpp
MIT License
254 stars 207 forks source link

queen-attack: compile error without useful information #722

Closed nozomirin closed 11 months ago

nozomirin commented 11 months ago

Here is my solution: queen_attack.h

#if !defined(QUEEN_ATTACK_H)
#define QUEEN_ATTACK_H
#include <utility>
namespace queen_attack {
    class chess_board {
    public:
        chess_board(std::pair<int, int> white, std::pair<int, int> black);
        std::pair<int, int> white() const;
        std::pair<int, int> black() const;
        bool can_attack() const;
    private:
        bool is_valid(std::pair<int, int> chess) const;
        bool at_diagonal() const;
        std::pair<int, int> white_;
        std::pair<int, int> black_;
    };
}  // namespace queen_attack

#endif // QUEEN_ATTACK_H

queen_attack.cpp

#include "queen_attack.h"
#include <stdexcept>
#include <cmath>
namespace queen_attack {
chess_board::chess_board(std::pair<int, int> white, std::pair<int, int> black): white_(white), black_(black) {
    if (!is_valid(white) || !is_valid(black) || white == black) {
        //throw std::domain_error("The range should be in [1-8]");
    }
}

std::pair<int, int> chess_board::white() const {
    return white_;
}
std::pair<int, int> chess_board::black() const {
    return black_;
}

bool chess_board::can_attack() const {
    return white_.first == black_.first || white_.second == black_.second || at_diagonal();
}

bool chess_board::is_valid(std::pair<int, int> chess) const{
    if (chess.first <= 8 && chess.first > 0 && chess.second <= 8 && chess.second > 0) {
        return true;
    }
    return false;
}
bool chess_board::at_diagonal() const {
    return std::abs(white_.first - black_.first) == std::abs(white_.second - black_.second);
}
}  // namespace queen_attack

If I just commented the line that throws the domain_error exception, it compiles but can't pass the test. But if I throw the exception there, I got this: We received the following error when we ran your code: make[2]: [CMakeFiles/test_queen-attack.dir/build.make:70: CMakeFiles/test_queen-attack] Error 3 make[1]: [CMakeFiles/Makefile2:111: CMakeFiles/test_queen-attack.dir/all] Error 2 make: *** [Makefile:91: all] Error 2

And somehow, the Test page get wired... test

github-actions[bot] commented 11 months ago

Hello. Thanks for opening an issue on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use [this link](https://forum.exercism.org/new-topic?title=queen-attack:%20compile%20error%20without%20useful%20information&body=Here%20is%20my%20solution:%20%0D%0Aqueen_attack.h%0D%0A%60%60%60cpp%0D%0A#if%20!defined(QUEEN_ATTACK_H)%0D%0A#define%20QUEEN_ATTACK_H%0D%0A#include%20%3Cutility%3E%0D%0Anamespace%20queen_attack%20%7B%0D%0A%20%20%20%20class%20chess_board%20%7B%0D%0A%20%20%20%20public:%0D%0A%20%20%20%20%20%20%20%20chess_board(std::pair%3Cint,%20int%3E%20white,%20std::pair%3Cint,%20int%3E%20black);%0D%0A%20%20%20%20%20%20%20%20std::pair%3Cint,%20int%3E%20white()%20const;%0D%0A%20%20%20%20%20%20%20%20std::pair%3Cint,%20int%3E%20black()%20const;%0D%0A%20%20%20%20%20%20%20%20bool%20can_attack()%20const;%0D%0A%20%20%20%20private:%0D%0A%20%20%20%20%20%20%20%20bool%20is_valid(std::pair%3Cint,%20int%3E%20chess)%20const;%0D%0A%20%20%20%20%20%20%20%20bool%20at_diagonal()%20const;%0D%0A%20%20%20%20%20%20%20%20std::pair%3Cint,%20int%3E%20white_;%0D%0A%20%20%20%20%20%20%20%20std::pair%3Cint,%20int%3E%20black_;%0D%0A%20%20%20%20%7D;%0D%0A%7D%20%20//%20namespace%20queen_attack%0D%0A%0D%0A#endif%20//%20QUEEN_ATTACK_H%0D%0A%60%60%60%0D%0Aqueen_attack.cpp%0D%0A%60%60%60cpp%0D%0A#include%20%22queen_attack.h%22%0D%0A#include%20%3Cstdexcept%3E%0D%0A#include%20%3Ccmath%3E%0D%0Anamespace%20queen_attack%20%7B%0D%0Achess_board::chess_board(std::pair%3Cint,%20int%3E%20white,%20std::pair%3Cint,%20int%3E%20black):%20white_(white),%20black_(black)%20%7B%0D%0A%20%20%20%20if%20(!is_valid(white)%20%7C%7C%20!is_valid(black)%20%7C%7C%20white%20==%20black)%20%7B%0D%0A%20%20%20%20%20%20%20%20//throw%20std::domain_error(%22The%20range%20should%20be%20in%20%5B1-8%5D%22);%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Astd::pair%3Cint,%20int%3E%20chess_board::white()%20const%20%7B%0D%0A%20%20%20%20return%20white_;%0D%0A%7D%0D%0Astd::pair%3Cint,%20int%3E%20chess_board::black()%20const%20%7B%0D%0A%20%20%20%20return%20black_;%0D%0A%7D%0D%0A%0D%0Abool%20chess_board::can_attack()%20const%20%7B%0D%0A%20%20%20%20return%20white_.first%20==%20black_.first%20%7C%7C%20white_.second%20==%20black_.second%20%7C%7C%20at_diagonal();%0D%0A%7D%0D%0A%0D%0Abool%20chess_board::is_valid(std::pair%3Cint,%20int%3E%20chess)%20const%7B%0D%0A%20%20%20%20if%20(chess.first%20%3C=%208%20&&%20chess.first%20%3E%200%20&&%20chess.second%20%3C=%208%20&&%20chess.second%20%3E%200)%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20true;%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20return%20false;%0D%0A%7D%0D%0Abool%20chess_board::at_diagonal()%20const%20%7B%0D%0A%20%20%20%20return%20std::abs(white_.first%20-%20black_.first)%20==%20std::abs(white_.second%20-%20black_.second);%0D%0A%7D%0D%0A%7D%20%20//%20namespace%20queen_attack%0D%0A%0D%0A%60%60%60%0D%0AIf%20I%20just%20commented%20the%20line%20that%20throws%20the%20domain_error%20exception,%20it%20compiles%20but%20can't%20pass%20the%20test.%0D%0ABut%20if%20I%20throw%20the%20exception%20there,%20I%20got%20this:%0D%0AWe%20received%20the%20following%20error%20when%20we%20ran%20your%20code:%0D%0Amake%5B2%5D:%20***%20%5BCMakeFiles/test_queen-attack.dir/build.make:70:%20CMakeFiles/test_queen-attack%5D%20Error%203%0D%0Amake%5B1%5D:%20***%20%5BCMakeFiles/Makefile2:111:%20CMakeFiles/test_queen-attack.dir/all%5D%20Error%202%0D%0Amake:%20***%20%5BMakefile:91:%20all%5D%20Error%202%0D%0A%0D%0AAnd%20somehow,%20the%20Test%20page%20get%20wired...%0D%0A!%5Btest%5D(https://github.com/exercism/cpp/assets/35720515/54588979-e5d7-4b6a-8814-1b82fbc35d47)%0D%0A&category=cpp) to copy this into a new topic there.


Note: If this issue has been pre-approved, please link back to this issue on the forum thread and a maintainer or staff member will reopen it.

siebenschlaefer commented 11 months ago

This error report has three aspects:

nozomirin commented 11 months ago

@siebenschlaefer Thanks for your help! I changed the range and it works. For the weird page, I can't reproduce it. I just modified the code and ran tests. I relaunched the browser before, the problem just gone. If these information can help you,

the browser: chrome Version 116.0.5845.110 (Official Build) (64-bit) os: gentoo core: 6.1.46

nozomirin commented 10 months ago

@siebenschlaefer It seems that the weird test page problem happens every time I open a new exercise. After I clicked back to exercise and continue in editor, it disappeared.

nozomirin commented 10 months ago

eh... I just opened two and it appeared twice but the third one doesn't appear. I don't know whether some build errors in the last exercise triggered it. Sorry.