gpt4 book ai didi

c++ - 在不理解的情况下尝试编译 .h 文件

转载 作者:搜寻专家 更新时间:2023-10-31 01:56:27 26 4
gpt4 key购买 nike

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

无论如何,我无法编译几个文件。

无法编译的代码是这样的:

容器.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com