私はMSVC10を使用しています。
C
クラスにネストされているクラスがあり、クラスはクラスにネストB
されていA
ます。 B
タイプがのメンバー変数をC
持ちA
、vector
のがB
sです。そのようです:
class A
{
class B
{
string foo_;
class C
{
string bar_;
} c_;
};
vector<B> b_;
};
中には、ラムダA
で使用するメンバー関数があり、を反復処理します。for_each
vector<B>
B
そのラムダでは、とC
(別々に)への参照を取得しようとします:
void A::Run()
{
for_each(b_.begin(), b_.end(), [](std::vector<B>::value_type& that)
{
const B& b = that;
cout << b.foo_;
const B::C& c = b.c_; // 'B' : is not a class or namespace name
// const A::B::C& c = b.c_; <-- THIS COMPILES
cout << c.bar_;
});
}
コード:コンパイラが問題なく受け入れてもconst B::C& c = b.c_;
、「'B':はクラス名または名前空間名ではありません」というコンパイラエラーが発生します。const B& b = that;
この構文は言語で許可されていますか?
これを次のように変更すると const A::B::C& c = b.c_;
、コンパイラはそれを受け入れます。
これがあなたが遊ぶための完全な例です:
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void foo() {}
class A
{
public:
void Run();
struct B
{
std::string foo_;
struct C
{
std::string bar_;
} c_;
};
std::vector<B> b_;
};
void A::Run()
{
for_each(b_.begin(), b_.end(), [](std::vector<B>::value_type& that)
{
const B& b = that;
cout << b.foo_;
const B::C& c = b.c_; // 'B' : is not a class or namespace name
// const A::B::C& c = b.c_; <-- THIS COMPILES
cout << c.bar_;
});
}
int main()
{
A a;
a.Run();
}