#include "stdafx.h"
#include<vector>
template<class T, int capacity = 30>
class Stack {
public:
Stack(){
pool.reserve(capacity);
}
void clear(){
pool.clear;
}
void isEmpty(){
return pool.empty();
}
T& topEl(){
return pool.back();
}
T pop(){
T el = pool.back();
pool.pop_back();
return el;
}
void push(const T& el){
pool.push_back(el);
}
private:
vector<T> pool;
};
int _tmain(int argc, _TCHAR* argv[])
{
//Stack<int,30> test;
return 0;
}
Above is the code that I have written, which outlines a very generic vector implementation of stack. However,when I compiled it, I got the following errors from the compiler.
1>------ Rebuild All started: Project: Stack, Configuration: Debug Win32 ------
1> stdafx.cpp
1> Stack.cpp
1>c:\users\lxjhk\documents\visual studio 2013\projects\stack\stack\stack.cpp(31): error C2143: syntax error : missing ';' before '<'
1> c:\users\lxjhk\documents\visual studio 2013\projects\stack\stack\stack.cpp(32) : see reference to class template instantiation 'Stack<T,capacity>' being compiled
1>c:\users\lxjhk\documents\visual studio 2013\projects\stack\stack\stack.cpp(31): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\lxjhk\documents\visual studio 2013\projects\stack\stack\stack.cpp(31): error C2238: unexpected token(s) preceding ';'
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
I really could not understand where went wrong and I have checked the code against my book multiple times but could not find any difference.
The vector<T> template class is defined in the std namespace. You shouldn't use a using namespace statement in a header, so instead you should prefix all STL types with std::
...
private:
std::vector<T> pool;
};
...
As an aside, assuming this isn't a homework exercise, have you seen std::stack<T>? ( http://www.cplusplus.com/reference/stack/stack/ )
void clear(){
pool.clear;
}
should be
void clear(){
pool.clear();
}
And since vector<T> is in namespace std, you should use
std::vector<T> pool;
in your class definition.
Related
Consider the following code snippet:
template<unsigned... IDs>
class MyClass{
public:
static const std::array<unsigned, sizeof...(IDs)> ids { IDs... };
PinIDs() = default;
};
Then use the class as:
MyClass<1,5,7,9> myClass;
The Objective would be to have ids with a size of 4, and contain the values: (1,5,7,9) respectively.
Is this type object possible or would I have to remove the static qualifier? If not how would one write this with the static qualifier. The object needs to be default constructible.
EDIT:
I tried Apple Apple's first solution and with MS Visual Studio 2017 CE on Win7
I got this compiler error:
1>------ Build started: Project: PracticeMath, Configuration: Debug Win32 ------
1>stdafx.cpp
1>PracticeMath.cpp
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2988: unrecognizable template declaration/definition
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2143: syntax error: missing ';' before '<'
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2059: syntax error: '<'
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2039: 'ids': is not a member of '`global namespace''
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2143: syntax error: missing ';' before '{'
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(38): error C2065: 'myId': undeclared identifier
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(39): error C2065: 'myId': undeclared identifier
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(40): error C2065: 'myId': undeclared identifier
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(44): error C2065: 'c': undeclared identifier
1>Done building project "PracticeMath.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
With full original source like this:
#include <iostream>
#include <array>
template<unsigned... IDs>
class PinIDs{
public:
static const std::array<unsigned, sizeof...(IDs)> ids;
PinIDs() = default;
const unsigned& operator[]( unsigned idx ) const {
return ids[idx];
}
};
template<unsigned... IDs>
const std::array<unsigned, sizeof...(IDs)> PinIDs<IDs...>::ids { IDs... };
int main() {
PinIDs<4, 17, 19> myId;
std::cout << myId[0] << " ";
std::cout << myId[1] << " ";
std::cout << myId[2] << " ";
std::cout << "\nPress any key and enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
Thanks to StoryTeller bringing up the fact that when I tried to apply Apple Apple's 1st method I accidently mixed up MyClass as opposed to the actual name of the class in my solution - project. Once I corrected that it does compile, build and run as expected.
you can try this
#include <array>
template<unsigned... IDs>
class MyClass{
public:
static const std::array<unsigned, sizeof...(IDs)> ids;
MyClass() = default;
};
template<unsigned... IDs>
const std::array<unsigned, sizeof...(IDs)> MyClass<IDs...>::ids {IDs...};
int main(){
MyClass<1,5,7,9> myClass;
return myClass.ids[0];
}
or use constexpr/inline (both need c++17)
#include <array>
template<unsigned... IDs>
class MyClass{
public:
//static constexpr std::array<unsigned, sizeof...(IDs)> ids{IDs...};//or this
static inline const std::array<unsigned, sizeof...(IDs)> ids{IDs...};
MyClass() = default;
};
int main(){
MyClass<1,5,7,9> myClass;
return myClass.ids[0];
}
Refer to #apple apple's answer for the basics. I'll just add the C++17 way to do it. Which is quite close to your original attempt. Just add an inline specifier to the variable:
template<unsigned... IDs>
class MyClass{
public:
static inline const std::array<unsigned, sizeof...(IDs)> ids{ { IDs... } };
MyClass() = default;
};
Now the declaration can double as a definition. Oh, and mind the braces. std::array needs to be initialized as an aggregate. So one pair of {} for the std::array, and one for the internal raw array it holds.
why don't you just try it with online compiler, supporting c++17?
template<unsigned... IDs>
class MyClass{
public:
static constexpr std::array<unsigned, sizeof...(IDs)> ids { IDs... };
MyClass() = default;
};
works fine. You don't need static const inline for variables, which are computed at compile time, just use static constexpr
I'm trying to compile a very simple grammar using boost spirit, but now I get this compilation error and after spending like an hour trying to figure out the reason I hope someone from the community immediately sees what the problem is or could be. This is the source code Viper.h:
#pragma once
#include <string>
#include <vector>
#define BOOST_SPIRIT_UNICODE
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/fusion/adapted/boost_tuple.hpp>
namespace nsunic = boost::spirit::unicode;
namespace nsqi = boost::spirit::qi;
namespace viper
{
struct identifier
{
std::wstring name;
};
struct identifier2
{
std::wstring name;
};
struct function
{
boost::variant<identifier, identifier2> func;
};
struct program
{
std::vector<function> functions;
};
}
BOOST_FUSION_ADAPT_STRUCT(
viper::identifier,
(std::wstring, name)
)
BOOST_FUSION_ADAPT_STRUCT(
viper::identifier2,
(std::wstring, name)
)
BOOST_FUSION_ADAPT_STRUCT(
viper::function,
(boost::variant<viper::identifier,viper::identifier2>, func)
)
BOOST_FUSION_ADAPT_STRUCT(
viper::program,
(std::vector<viper::function>, functions)
)
namespace viper
{
template<typename Iterator> struct function_parser : nsqi::grammar<Iterator, program(), nsqi::space_type>
{
function_parser() : function_parser::base_type(program)
{
identifier %=
nsqi::eps
>> (char_('a') > *(nsqi::alnum | nsqi::char_('_')));
identifier2 %=
nsqi::eps
>> (char_('b') > *(nsqi::alnum | nsqi::char_('_')));
function %=
identifier | identifier2;
program %=
nsqi::eps
>> +function;
}
nsqi::rule<Iterator, identifier()> identifier;
nsqi::rule<Iterator, identifier2()> identifier2;
nsqi::rule<Iterator, function(), nsqi::space_type> function;
nsqi::rule<Iterator, program(), nsqi::space_type> program;
};
std::wstring render(const program& f)
{
std::wostringstream s;
const auto functions_count = f.functions.size();
for(auto j=0; j<functions_count; ++j)
{
if(j>0)
{
s << L",";
}
s << f.functions[j].name;
}
return s.str();
}
template<typename Iterator> std::wstring parse(Iterator first, Iterator last)
{
using nsqi::phrase_parse;
program f;
function_parser<Iterator> fp;
auto b = phrase_parse(first, last, fp, nsqi::space, f);
if(b)
{
return render(f);
}
return std::wstring(L"FAIL");
}
}
The output of the compiler is this:
1>------ Build started: Project: Viper.Notepad, Configuration: Debug Win32 ------
1>Build started 24-Jan-16 17:40:29.
1>InitializeBuildStatus:
1> Touching "Debug\Viper.Notepad.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1> Viper.Notepad.cpp
1>c:\users\srzmtl\local_copies\visual studio\visual studio 2010\viper\viper\viper.h(51): warning C4002: too many actual parameters for macro 'BOOST_FUSION_ADAPT_STRUCT_FILLER_0'
1>c:\users\srzmtl\local_copies\visual studio\visual studio 2010\viper\viper\viper.h(51): error C2146: syntax error : missing ',' before identifier 'attribute_type'
1>c:\users\srzmtl\local_copies\visual studio\visual studio 2010\viper\viper\viper.h(51): error C2065: 'attribute_type' : undeclared identifier
1>c:\users\srzmtl\local_copies\visual studio\visual studio 2010\viper\viper\viper.h(51): error C2143: syntax error : missing '>' before ';'
1>c:\users\srzmtl\local_copies\visual studio\visual studio 2010\viper\viper\viper.h(51): error C2208: 'boost::variant' : no members defined using this type
1>c:\users\srzmtl\local_copies\visual studio\visual studio 2010\viper\viper\viper.h(51): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:23.25
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Line 51 is the closing ')' of the 3rd BOOST_FUSION_ADAPT_STRUCT() macro.
The problem is that boost::variant<viper::identifier,viper::identifier2> contains a comma (,) and as such is interpreted as 2 arguments by the preprocessor.
A workaround:
typedef boost::variant<viper::identifier,viper::identifier2> Variant;
BOOST_FUSION_ADAPT_STRUCT(
viper::function,
(Variant, func)
)
Some other workarounds are listed here.
Please have a look at the following code,
Stack.h
template <typename T>
class Stack
{
public:
Stack(int number)
{
maxSize = number;
top = -1;
stackData = new T*[maxSize];
}
~Stack()
{
delete [] stackData;
}
int count()
{
}
bool isEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(top== (maxSize-1))
{
return true;
}
else
{
return false;
}
}
*T pop()
{
if(!isEmpty())
{
return stackData[top--]; // Remove Item From Stack
}
}
*T peek()
{
T *peekData = &stackData[top];
return peekData;
}
void push(T *pushValue)
{
if(!isFull())
{
stackData[++top] = pushValue;
}
}
private:
int maxSize;
T ** stackData;
int top;
};
Main.cpp
#include <iostream>
#include "Stack.h"
#include <iostream>
using namespace std;
int main()
{
int i = 0;
Stack<double> doubleStack(5);
double doubleValue = 1.1;
cout << "pushing elements into the stack" << endl;
while(i<5)
{
doubleStack.push();
}
system("pause");
return 0;
}
When I run this code, I get the following error.
1>------ Build started: Project: CourseWork2, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(74) : see reference to class template instantiation 'Stack<T>' being compiled
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(54): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(60): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(11) : see reference to class template instantiation 'Stack<T>' being compiled
1> with
1> [
1> T=double
1> ]
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(18): error C2660: 'Stack<T>::push' : function does not take 0 arguments
1> with
1> [
1> T=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Intellelisense is not identifying any method except isFull(), count() and isEmpty(). I can't code the rest because of this!
Why is this? Please help!
You put * at wrong place in function syntax:
Update:
*T pop()
*T peek()
To:
T* pop()
T* peek()
You put the asterisk in the wrong spot. It should be:
T *pop() {
//implementation
}
and
T *peek() {
//implementation
}
I'm having some strange issues trying to initialize a static const member variable of a template class. All of my other static variables initialize fine but for some reason it doesn't like this one. I put together some sample code to test and it doesn't have the problem so I really don't know what's going on.
On top of this I'm also having issues defining functions that use typedefs declared inside of the template class with the same issue saying it can't find the type. This problem I have been able to reproduce though in the code below. I know one way to fix it is to define the function inside of the class, but the function is really large and I'm trying to keep it consistent with having all of the huge functions defined outside of the class to make the class definition easier to read. If that's my only option though then I guess I'll have to make an exception...
class tTestType
{
public:
tTestType(int32_t val) : fValue(val) { }
private:
int32_t fValue;
};
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
typedef std::vector<int32_t> tSomeVec;
tSomeVec mTestFunction() const;
static const tTestType kTestStatic;
};
// Should cause the following errors but I can't reproduce them for some reason:
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// error C2988: unrecognizable template declaration/definition
// error C2059: syntax error : 'constant'
template<class T>
const tTestType tTestTemplate<T>::kTestStatic(10);
// Causes the following errors:
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction'
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class T>
tTestTemplate<T>::tSomeVec tTestTemplate<T>::mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
Thanks to a coworker I've figured out a solution to both issues.
For the first issue, the static member variable, I've moved the definition to the CPP file and used a template specialization. The reason this doesn't break in the test code I posted is because the basic type (int, float, etc.) handle the problem, but if you use a more complex type, like a class, then it should cause the error. This solution isn't the best thing in the world to do, I know, but it's the only thing that works that is somewhat clean. If someone has a better solution please let me know:
template<>
const tTestType tTestTemplate<uint32_t>::kTestStatic(10);
For the second issue, the function using a type declared inside the class, I went with the solution I described in the initial post and just moved the function definition inside of the template class so now it looks like this:
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
typedef std::vector<int32_t> tSomeVec;
// Declaring the function inside the class to fix the compiler error.
tSomeVec mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
static const tTestType kTestStatic;
};
I made 2 changes in your code and its compiling fine.
class tTestType
{
public:
tTestType(int32_t val) : fValue(val) { }
private:
int32_t fValue;
};
typedef std::vector<int32_t> tSomeVec;
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
tSomeVec mTestFunction() const;
static const tTestType kTestStatic;
};
// Should cause the following errors but I can't reproduce them for some reason:
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// error C2988: unrecognizable template declaration/definition
// error C2059: syntax error : 'constant'
template<class T>
const tTestType tTestTemplate<T>::kTestStatic(10);
// Causes the following errors:
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction'
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class T>
tSomeVec tTestTemplate<T>::mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
I'm trying to compile Opengazer (Open source gaze tracker) code with visual studio on windows, while the code was originally written for linux and should be compile with cmake.
Anyway, I can't compile few files.
The code won't compile is this:
Containers.h:
#pragma once
#define xforeachactive(iter,container) \
for(typeof(container.begin()) iter = container.begin(); \
iter != container.end(); iter++) \
if ((*iter)->parent == this)
template <class ParentType, class ChildType> class Container;
template <class ParentType, class ChildType>
class Containee {
protected:
void detach() { parent = 0; }
public:
ParentType *parent; /* set to null to request removal */
Containee(): parent(0) {}
virtual ~Containee() {}
};
template <class ParentType, class ChildType>
class Container {
typedef ChildType *ChildPtr;
static bool isFinished(const ChildPtr &object) {
return !(object && object->parent);
}
protected:
std::vector<ChildPtr> objects;
void removeFinished() {
objects.erase(remove_if(objects.begin(), objects.end(), isFinished),
objects.end());
}
public:
void clear() {
xforeachactive(iter, objects)
(*iter)->parent = 0;
removeFinished();
}
static void addchild(ParentType *parent, const ChildPtr &child) {
parent->objects.push_back(child);
child->parent = parent;
parent->removeFinished();
}
virtual ~Container() {
clear();
}
};
template <class ParentPtr, class ChildPtr>
class ProcessContainer: public Container<ParentPtr, ChildPtr> {
public:
virtual void process() {
xforeachactive(iter, this->objects)
(*iter)->process();
this->removeFinished();
}
virtual ~ProcessContainer() {};
};
btw Containers.cpp is empty
ad the code uses the above class is:
#pragma once
class FrameProcessing;
class FrameFunction:
public Containee<FrameProcessing, FrameFunction>
{
const int &frameno;
int startframe;
protected:
int getFrame() { return frameno - startframe; }
public:
FrameFunction(const int &frameno): frameno(frameno), startframe(frameno) {}
virtual void process()=0;
virtual ~FrameFunction();
};
class FrameProcessing:
public ProcessContainer<FrameProcessing,FrameFunction> {};
class MovingTarget: public FrameFunction {
WindowPointer *pointer;
public:
MovingTarget(const int &frameno,
const vector<Point>& points,
WindowPointer *&pointer,
int dwelltime=20);
virtual ~MovingTarget();
virtual void process();
protected:
vector<Point> points;
const int dwelltime;
int getPointNo();
int getPointFrame();
bool active();
};
class CalibrationHandler
{
public:
CalibrationHandler(void);
~CalibrationHandler(void);
};
the error I get is :
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2146: syntax error : missing ';' before identifier 'iter'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2146: syntax error : missing ')' before identifier 'iter'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2059: syntax error : ';'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2059: syntax error : ')'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2143: syntax error : missing ';' before 'if'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2227: left of '->parent' must point to class/struct/union/generic type
type is ''unknown-type''
visual studio 2008\projects\eyemouse\eyemouse\containers.h(59) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(59) : error C2227: left of '->process' must point to class/struct/union/generic type
type is ''unknown-type''
I understand why I'm getting an error.
'iter' is not defined anywhere. anyway, this isnt my code and it should work.
I tried to copy and past the define part to the function, but still get the same error.
I'm stuck with this and trying to solve it for hours, but can't understand what to do to make it work.
I'll really be grateful for any help.
typeof is a gcc extension and equivalent to C++0x decltype there is no VS version that actually supports it.
You would need to use C++0x and decltype or try to use Boost.TypeOf, which comes with its own caveats.
Change the macro to this:
#include <boost/typeof/typeof.hpp>
#define xforeachactive(iter,container) \
for(BOOST_TYPEOF(container.begin()) iter = container.begin(); \
iter != container.end(); iter++) \
if ((*iter)->parent == this)
You could also use BOOST_AUTO if you think this is clearer.