Empty C++ error message

Hello Everyone,

I am getting an empty error message when run the tests for “Circular Buffer” on the C++ track. Does anyone know why this might be?

Without your solution it’s hard to tell why the error message is empty.

My guess is that the solution does something unexpected, e.g. throw an exception where none was expected or just crashed.

Post your solution here, with a line of three backticks (```) before and after the code.

Hello,

The code that was causing the blank error was as follows:

#if !defined(CIRCULAR_BUFFER_H)
#define CIRCULAR_BUFFER_H

#include <stdexcept>

namespace circular_buffer {

template <typename T>
class circular_buffer{

private:
    int size{0};
    int count{0};
    T* buffer;
    int head {0};
    int tail {0};
public:
    circular_buffer(int buffer_size){
        buffer = new T[buffer_size]{};
        size = buffer_size;
    }

    ~circular_buffer(){
        delete[] buffer;
    }

    T read(){
        if(count == 0){
            throw std::domain_error("buffer is empty");
        }
        T item = buffer[head];
        head = (head + 1) % size;
        --count;
        return item;
    }

    void write(T element){
        if(count == size){
            throw std::domain_error("buffer is full");
        }
        buffer[tail] = element;
        tail = (tail + 1) % size;
        count = (count + 1) % size;
    }

    void overwrite(T element){
        buffer[tail] = element;
        tail = (tail + 1) % size;
        head = (head + 1) % size;
        count = (count + 1) % size;
    }

    void clear(){
        head = tail = count = 0;
    }
};

}  // namespace circular_buffer

#endif // CIRCULAR_BUFFER_H

I have now got the tests passing, but it was mainly through trial and error updates to my code.

My question, reformulated for clarity, is what causes us to get a blank error message when running our tests and is there a way to fix this?

kind regards,

Jonathan

When I run the tests with the solution from your comment several tests fail:

can_read_an_item_just_written
-------------------------------------------------------------------------------
/vagrant/exercism/cpp/circular-buffer/circular_buffer_test.cpp:15
...............................................................................

circular_buffer_test.cpp:22: FAILED:
  REQUIRE( expected == buffer.read() )
due to unexpected exception with message:
  buffer is empty

I think this “unexpected exception” is what the test runner cannot understand. IIRC the tests create a file in the junit XML format and the result for such an unexpected exception looks different or messes up the file completely.

Proof by example: When I add the line throw std::domain_error("test"); in the hello-world exercise I get the same empty error message.

So yes, the test runner should be fixed so that we get a meaningful error message.


BTW:
Take a close look at the last line in write() and overwrite(). If count + 1 equals size then count gets set to 0.
Also, overwrite() should behave like write() if the circular buffer is not full.