OK a bad case of code blindness.
I'm just not see why attempts at compiling the following code with -
results in -
The code compiled and worked fine until I pulled it into separate header and implementation files.
I'm just not see why attempts at compiling the following code with -
Code:
clang++ -stdlib=libc++ -std=c++11 circular_queue.cpp
results in -
Code:
circular_queue.cpp:30:6: error: expected a class or namespace
bool circular_queue_t::dequeue(uint8_t& element)
^
circular_queue.cpp:91:6: error: expected a class or namespace
void circular_queue_t::report(std::ostream& os) const
^
2 errors generated.
The code compiled and worked fine until I pulled it into separate header and implementation files.
Code:
/* =====================================================================================================================
* File - circular_queue.h
* ---------------------------------------------------------------------------------------------------------------------
* Implements a circular queue of 2-bit values.
*
* <http://en.wikipedia.org/wiki/Circular_buffer>
*/
#ifndef circular_queue_H
#define circular_queue_H
/* =====================================================================================================================
* ---------------------------------------------------------------------------------------------------------------------
*/
#include <cstdint>
#ifndef NDEBUG
#include <iostream>
#include <iomanip>
#endif
/* =====================================================================================================================
* ---------------------------------------------------------------------------------------------------------------------
*/
template <const int CAPACITY>
class circular_queue_t
{
const int _capacity;
int _count;
int _head;
uint8_t _array[CAPACITY >> 2];
public:
circular_queue_t()
: _capacity(CAPACITY), _count(0), _head(0)
{}
bool dequeue(uint8_t& element);
bool enqueue(uint8_t const element);
bool is_empty() const { return (_count == 0); }
bool is_full() const { return (_count == _capacity); }
int count() const { return _count; }
#ifndef NDEBUG
void report(std::ostream& os) const;
#endif
};
#endif
Code:
/* =====================================================================================================================
* File - circular_queue.cpp
* ---------------------------------------------------------------------------------------------------------------------
* Implements a circular queue of 2-bit values.
*
* <http://en.wikipedia.org/wiki/Circular_buffer>
*
* clang++ -stdlib=libc++ -std=c++11 circular_queue.cpp -o circular_queue.o
*/
/* =====================================================================================================================
* ---------------------------------------------------------------------------------------------------------------------
*/
//#define NDEBUG
#include "circular_queue.h"
/* =====================================================================================================================
* ---------------------------------------------------------------------------------------------------------------------
*/
bool circular_queue_t::dequeue(uint8_t& element)
{
if ( ! is_empty() )
{
int index = (_head >> 2);
int dindex = (_head & 3);
element = ((_array[index] >> ((3 - dindex) << 1)) & 3);
_head = ((_head + 1) % _capacity);
_count--;
return true;
}
return false;
}
/* =====================================================================================================================
* ---------------------------------------------------------------------------------------------------------------------
*/
bool circular_queue_t::enqueue(uint8_t const element)
{
static uint8_t mask[] = { 0b00111111, 0b11001111, 0b11110011, 0b11111100 };
if ( ! is_full() )
{
int tail = ((_head + _count) % _capacity);
int index = (tail >> 2);
int dindex = (tail & 3);
_array[index] = (_array[index] & mask[dindex]) | ((element & 3) << ((3 - dindex) << 1));
_count++;
return true;
}
return false;
}
#ifndef NDEBUG
class StStreamFormatFlags
{
std::ostream& m_ostream;
std::ios_base::fmtflags m_flags;
public:
StStreamFormatFlags(std::ostream& str) : m_ostream(str), m_flags(str.flags())
{}
~StStreamFormatFlags()
{
m_ostream.flags(m_flags);
}
};
void circular_queue_t::report(std::ostream& os) const
{
using std::setfill;
using std::setw;
StStreamFormatFlags ffSave(os);
int count = _count;
int head = _head;
while ( count-- )
{
int index = (head >> 2);
int dindex = (head & 3);
int element = ((_array[index] >> ((3 - dindex) << 1)) & 3);
os << setw(4) << setfill(' ') << element;
head = ((head + 1) % _capacity);
}
}
#endif