【问题标题】:Trying to compile a .h file without understanding something试图在不了解某些内容的情况下编译 .h 文件
【发布时间】:2011-08-18 11:38:38
【问题描述】:

我正在尝试在 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 是空的

ad 使用上述类的代码是:

#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' 没有在任何地方定义。无论如何,这不是我的代码,它应该可以工作。 我试图将定义部分复制并传递给函数,但仍然得到相同的错误。 我一直坚持这个问题并试图解决它几个小时,但不明白该怎么做才能让它发挥作用。

如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 你用的是哪个VS版本?它真的支持typeof吗?

标签: c++ visual-studio compilation


【解决方案1】:

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。

【讨论】:

  • 感谢您的回答!我了解 VS 不支持 typeof。我尝试包含 Boost.Typeof,但仍然遇到相同的错误。我不明白它应该如何编译,因为“iter”仍然没有在任何地方定义。
  • @Frank 我添加了应该解决这个问题的代码。不幸的是,我无法对此进行测试。
  • 感谢您的帮助。它确实修复了它,但我现在面临其他错误。我发布了我现在遇到的错误。
  • @Frank 看起来移植这是一项更大的工作。自己尝试一下或联系项目。如果这是一个可以实现的目标,他们会更清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2018-08-13
  • 2020-05-04
  • 1970-01-01
相关资源
最近更新 更多