2011-08-18 44 views
2

我想在windows上編譯Opengazer(Open source gaze tracker)代碼與visual studio,而代碼最初是爲linux編寫的,應該用cmake編譯。試圖編譯一個.h文件而不理解某些東西

無論如何,我無法編譯幾個文件。

的代碼將無法編譯是這樣的:

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」沒有在任何地方定義。無論如何,這不是我的代碼,它應該工作。 我試圖複製和過去定義部分的功能,但仍然得到相同的錯誤。 我堅持這一點,並試圖解決它幾個小時,但不明白做什麼使它工作。

我真的很感激任何幫助。

+0

你使用哪個VS版本?它實際上是否支持'typeof'? – pmr

回答

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,如果你認爲這是更清晰。

+0

謝謝你的答案! 我明白VS不支持typeof。我試圖包括Boost.Typeof,但我仍然得到相同的錯誤。 我不明白它應該如何編譯,因爲'iter'仍然沒有被定義在任何地方。 – Frank

+0

@Frank我添加了應該修復這個問題的代碼。不幸的是我無法測試這個。 – pmr

+0

感謝您的幫助。它確實解決了它,但我現在正面臨其他錯誤。我發佈了我現在得到的錯誤。 – Frank

相關問題