BOOST_FOREACH:使用这个STL容器有什么错误?

时间:2023-01-14 14:05:43

Does anyone know why the following generates an error on VC9?

有谁知道为什么以下在VC9上生成错误?

class Elem;
class ElemVec : public vector<Elem>
{
    public:
        void foo();
};

void ElemVec::foo()
{
    BOOST_FOREACH(Elem& elem, *this)
    {
        // Do something with elem
    }
    return;
}

The error I get is:

我得到的错误是:

error C2355: 'this' : can only be referenced inside non-static member functions

The only (hack) solution I have right now which compiles without error is:

我现在唯一能够无误编译的(黑客)解决方案是:

void ElemVec::foo()
{
    ElemVec* This = this;
    BOOST_FOREACH(Elem& elem, *This)
    {
        // Do something with elem
    }
    return;
}

4 个解决方案

#1


Which compiler/Boost version are you using? I can compile the following without any problem (VS2005/Boost 1.38):

您使用的是哪个编译器/ Boost版本?我可以编译以下没有任何问题(VS2005 / Boost 1.38):

#include <boost/foreach.hpp>
using namespace std;
struct xxx : std::vector<int>
{
    void test()
    {
        BOOST_FOREACH(int x, *this)
        {
        }
    }
}; 

int main(void) {
    xxx x;
    x.test();
    return 0;
}

Search the Boost bugbase if you want more details.

如果您想了解更多详细信息,请搜索Boost bugbase。

#2


You shouldn't inherit from STL containers. These are not polymorphic classes and it's the reason BOOST_FORACH can't handle your derived class.

您不应该继承STL容器。这些不是多态类,这是BOOST_FORACH无法处理派生类的原因。

Try to use aggregation instead.

尝试使用聚合代替。

#3


I had never seen that error. I guess it comes from the implementation of BOOST_FOREACH macro.

我从未见过那个错误。我猜它来自BOOST_FOREACH宏的实现。

May i ask why you're creating a class based on vector<...> and not having a vector member variable ?

请问为什么你要创建一个基于vector <...>而没有vector成员变量的类?

EDIT Following this thread, i found out that this actually is a visual studio bug. The solution you have found seems to be the simplest.

编辑在这个帖子之后,我发现这实际上是一个视觉工作室的错误。您找到的解决方案似乎是最简单的。

#4


Hmm, all compiled succesfully on my msvc (2005) compiller.

嗯,所有编译都在我的msvc(2005)compiller上成功编译。

Maybe you have some error, but fixed or avoided it when was created your example.

也许你有一些错误,但在创建你的例子时修复或避免它。

#1


Which compiler/Boost version are you using? I can compile the following without any problem (VS2005/Boost 1.38):

您使用的是哪个编译器/ Boost版本?我可以编译以下没有任何问题(VS2005 / Boost 1.38):

#include <boost/foreach.hpp>
using namespace std;
struct xxx : std::vector<int>
{
    void test()
    {
        BOOST_FOREACH(int x, *this)
        {
        }
    }
}; 

int main(void) {
    xxx x;
    x.test();
    return 0;
}

Search the Boost bugbase if you want more details.

如果您想了解更多详细信息,请搜索Boost bugbase。

#2


You shouldn't inherit from STL containers. These are not polymorphic classes and it's the reason BOOST_FORACH can't handle your derived class.

您不应该继承STL容器。这些不是多态类,这是BOOST_FORACH无法处理派生类的原因。

Try to use aggregation instead.

尝试使用聚合代替。

#3


I had never seen that error. I guess it comes from the implementation of BOOST_FOREACH macro.

我从未见过那个错误。我猜它来自BOOST_FOREACH宏的实现。

May i ask why you're creating a class based on vector<...> and not having a vector member variable ?

请问为什么你要创建一个基于vector <...>而没有vector成员变量的类?

EDIT Following this thread, i found out that this actually is a visual studio bug. The solution you have found seems to be the simplest.

编辑在这个帖子之后,我发现这实际上是一个视觉工作室的错误。您找到的解决方案似乎是最简单的。

#4


Hmm, all compiled succesfully on my msvc (2005) compiller.

嗯,所有编译都在我的msvc(2005)compiller上成功编译。

Maybe you have some error, but fixed or avoided it when was created your example.

也许你有一些错误,但在创建你的例子时修复或避免它。