c++ - 尝试在不了解某些内容的情况下编译 .h 文件

标签 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() {};
};

顺便说一句,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' 没有在任何地方定义。无论如何,这不是我的代码,它应该可以工作。 我尝试将定义部分复制并粘贴到函数中,但仍然出现相同的错误。 我被这个问题困扰并试图解决它几个小时,但不明白该怎么做才能让它发挥作用。

如果有任何帮助,我将非常感激。

最佳答案

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。

关于c++ - 尝试在不了解某些内容的情况下编译 .h 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7106808/

相关文章:

c++ - 如何在 C++ 中重载赋值运算符?

windows - 在构建机器上编译 java 代码会出现 major.minor 错误 52

Swift重写静态方法编译报错

c# - 当没有错误时,CSharpCodeProvider 不会返回编译器警告

c++ - thread_local 变量的实例是否保证由访问它们的线程初始化?

c++ - 如何将可变参数模板参数的特征值减少为一个值?

c++ - 从线程抛出异常没有给出预期的结果

c++ - 从现有项目构建 exe 包(可移植)

c# - Visual Studio 2010 调试器(x86 模式)未显示未处理的错误

c++ - 如何找出 C++ header 在 Visual Studio 中编译需要多少时间?