试图编译.h文件而不理解某些东西

时间:2011-08-18 11:38:38

标签: c++ visual-studio compilation

我正在尝试用Windows上的visual studio编译Opengazer(开源注视跟踪器)代码,而代码最初是为linux编写的,应该用cmake编译。

无论如何,我无法编译少量文件。

代码将无法编译:

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为空

广告代码使用上面的类是:

#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);
};

我得到的错误是:

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''

我理解为什么我会收到错误。 'iter'没有在任何地方定义。无论如何,这不是我的代码,它应该工作。 我试图复制并通过定义部分到函数,但仍然得到相同的错误。 我坚持这个并试图解决它几个小时,但无法理解如何使它工作。

我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:3)

typeof是一个gcc扩展,相当于C ++ 0x decltype,没有实际支持它的VS版本。

您需要使用C ++ 0x和decltype或尝试使用Boost.TypeOf,它有自己的警告。

将宏更改为:

#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)

如果您认为这更清楚,也可以使用BOOST_AUTO。