Python のネストされた定義を C++ に変換したいと考えています。これは、ネストされた構造体を使用して行っています。
#include<iostream>
static void ffunc(int x, int y)
{
static int x1 = x;
static int y1 = y;
struct g
{
static void gfunc(int z)
{
static int z1 = z;
std::cout << x1 << y1 << z << std::endl;
struct h
{
static void hfunc(int k)
{
std::cout<< x1 << y1 << z1 << k << std::endl;
}
};
h::hfunc(4);
}
};
g::gfunc(3);
}
int main()
{
ffunc(1, 2);
}
このコードは正常に動作します。問題は、ネストされた関数が任意の型パラメーターを使用できるように、テンプレートを作成することです。ただし、テンプレートを使用しようとすると:
#include<iostream>
template <typename t1>
static void ffunc(t1 x, t1 y)
{
static t1 x1 = x;
static t1 y1 = y;
struct g
{
static void gfunc(int z)
{
static int z1 = z;
std::cout << x1 << y1 << z << std::endl;
struct h
{
static void hfunc(int k)
{
std::cout<< x1 << y1 << z1 << k << std::endl;
}
};
h::hfunc(4);
}
};
g::gfunc(3);
}
int main()
{
ffunc(1, 2);
}
エラーが発生します:
/tmp/cceIMovo.o: In function `void ffunc<int>(int, int)::g::gfunc(int)':
nested.cc:(.text+0x17d): undefined reference to `y1'
nested.cc:(.text+0x183): undefined reference to `x1'
/tmp/cceIMovo.o: In function `void ffunc<int>(int, int)::g::gfunc(int)::h::hfunc(int)':
nested.cc:(.text+0x1e5): undefined reference to `z1'
nested.cc:(.text+0x1ec): undefined reference to `y1'
nested.cc:(.text+0x1f2): undefined reference to `x1'
私がやりたいことがC ++で可能かどうか誰か知っていますか? ありがとう!