I have now written a very lightweight cl**** in a header file, but cannot for the life of me get the program to compile: whenever I add:
#include "../tpl/fixed_list_tpl.h"
to one of the existing files, I get dozens of cryptic errors, stating that I am missing a semicolon before the first line of code below the #include statements, even though there would never be a semicolon there normally, and there was no semicolon there before. Removing the reference to my new file allows it to compile. I get the impression that the file is somehow not properly included for compilation, but I cannot find a way of adding it: when I try Project>Add Existing Item (in MS VC++) and add fixed_list_tpl.h, it has no effect (and is actually the only header file added in that way: the others seem to work fine without explicitly being added!).
For reference (for the low-level fans), here is my code:
/* A very light 32 or less element list
* using a fixed sized array with a
* head and tail node. Template type.
*
* Author: jamespetts. Released under the terms
* of the Artistic Licence. Intended for use
* with Simutrans, but may be used elsewhere.
*/
#include <typeinfo>
#include "../simtypes.h"
#include "../simdebug.h"
template<cl**** T> cl**** fixed_list_tpl
{
fixed_list_tpl() : size(0), head(0), tail(0) { }
public:
T get_element(uint8 e)
{
if(e > size)
{
return NULL;
}
else
{
uint8 i = add_index(head, e);
return datum[i];
}
}
void clear()
{
size = 0;
head = 0;
tail = 0;
}
void trim_from_head(uint8 trim_to)
{
if(size <= trim_to)
{
return;
}
else
{
head = add_index(head, trim_to);
size -= trim_to;
}
return;
}
void trim_from_tail(uint8 trim_to)
{
if(size <= trim_to)
{
return;
}
else
{
tail = subtract_index(tail, trim_to);
size -= trim_to;
}
return;
}
uint8 get_size()
{
return size;
}
void add_to_head(T datum)
{
uint8 i = subtract_index(head, 1);
data[i] = datum;
head = i;
if(tail == head)
{
tail = subtract_index(head, 1);
}
else
{
size ++;
}
}
void add_to_tail(T datum)
{
uint8 i = add_index(tail, 1);
data[i] = datum;
tail = i;
if(head == tail)
{
head = add_index(tail, 1);
}
else
{
size ++;
}
}
bool add_to_head_no_overwite(T datum)
{
if(size < 32)
{
add_to_head(datum);
return true;
}
else
{
return false;
}
}
bool add_to_tail_no_overwrite(T datum)
{
if(size < 32)
{
add_to_tail(datum);
return true;
}
else
{
return false;
}
}
uint8 get_index_of(T datum)
{
uint8 tmp = 999;
for(i = 0, i < 32, i++)
{
if(data[i] == datum)
{
tmp = i;
break;
}
}
if(tmp > 31)
{
return NULL;
}
else
{
uint8 index = subtract_index(tmp, head);
return data[index];
}
}
private:
T data[32];
uint8 size;
uint8 head;
uint8 tail;
//These methods are used for automating looping arithmetic
uint8 add_index(uint8 base, uint8 addition)
{
uint8 tmp;
if((base + addition) < 32)
{
return base + addition;
}
else
{
tmp = (base + addition) - 32;
}
if(tmp < 32)
{
return tmp;
}
else
{
//There could be a sophisticated system of trimming here,
//but it would take extra work/memory/processing power, and
//is only used internally, so not worth it. This code should
//never be reached.
return 31;
}
}
uint8 subtract_index(uint8 base, uint8 subtraction)
{
uint8 tmp;
if((base - subtraction) < 32)
{
return base - subtraction;
}
else //Will be > 32 if has gone to < 0, as unsigned ints are used: overflow.
{
tmp = (base - subtraction) + 32; //Should re-set the overflow
}
if(tmp < 32)
{
return tmp;
}
else
{
//There could be a sophisticated system of trimming here,
//but it would take extra work/memory/processing power, and
//is only used internally, so not worth it. This code should
//never be reached.
return 31;
}
}
}
I should be very grateful for any ideas on how to make it compile properly with the other code.