gpt4 book ai didi

c++ - 错误 : invalid use of incomplete type (Maybe a definition issue)

转载 作者:行者123 更新时间:2023-11-30 02:36:40 27 4
gpt4 key购买 nike

一段时间以来,我一直在为这个问题苦苦挣扎,但似乎无法正确处理所有模板和特化。我想我缺少静态函数 internal_set_BC 的定义,但我不确定。当我编译下面的代码时,出现以下错误:

test.cc:37:49: error: invalid use of incomplete type ‘class RT<2, D>’
static void RT<2, D>::internal_set_BC(D& d, T& t)
^
test.cc:19:7: error: declaration of ‘class RT<2, D>’
class RT: public BC<n, D>
^
test.cc:41:49: error: invalid use of incomplete type ‘class RT<3, D>’
static void RT<3, D>::internal_set_BC(D& d, T& t)
^
test.cc:19:7: error: declaration of ‘class RT<3, D>’
class RT: public BC<n, D>

简化代码如下:

class A {};
class B {};
class C {};
class E {};

template <int n>
class Base
{};

template <int n, typename D>
class BC: public Base<n>
{
public:
virtual void set_BC(D& d, A& a) =0;
virtual void set_BC(D& d, B& b) =0;
};

template <int n, typename D>
class RT: public BC<n, D>
{
public:
void set_BC(D& d, A& a)
{internal_set_BC(d, a);}
void set_BC(D& d, B& b)
{internal_set_BC(d, b);}
private:
template <typename T>
static void internal_set_BC(D& d, T& t);
};

template <typename D, typename T>
static void RT<2, D>::internal_set_BC(D& d, T& t)
{//code
}
template <typename D, typename T>
static void RT<3, D>::internal_set_BC(D& d, T& t)
{//code
}

此外,当我尝试使用此代码实例化 internal_set_BC 时:

template void RT<2, C>::internal_set_BC(C& d, A& t);

我收到这个错误:

test.cc:45:51: error: explicit instantiation of ‘static void RT<n, D>::internal_set_BC(D&, T&) [with T = A; int n = 2; D = C]’ but no definition available [-fpermissive]
template void RT<2, C>::internal_set_BC(C& d, A& t);

这支持了我对上述代码中缺少定义的怀疑。

非常感谢您的帮助。

最佳答案

你不能。你只能为了完全特化而做这样的事情:

template <>
template <typename T>
static void RT<2, int>::internal_set_BC(int& d, T& t)
{//code
}

int 只是示例。您应该编写部分类特化。

template<typename D>
class RT<2, D> : public BC<2, D>
{
// public functions set_BC.
private:
template <typename T>
static void internal_set_BC(D& d, T& t);
};

template <typename D>
template <typename T>
void RT<2, D>::internal_set_BC(D& d, T& t)
{//code
}

你也可以这样写,只专门化internal_set_BC:

template<int n, typename D, typename Impl>
class RT_base : public BC<n, D>
{
public:
void set_BC(D& d, A& a)
{
Impl::internal_set_BC(d, a);
}
void set_BC(D& d, B& b)
{
Impl::internal_set_BC(d, b);
}
};

template <int n, typename D>
class RT: public RT_base<n, D, RT<n, D> >
{
friend class RT_base<n, D, RT<n, D> >;
template <typename T>
static void internal_set_BC(D& d, T& t);
};

template<typename D>
class RT<2, D> : public RT_base<2, D, RT<2, D> >
{
friend class RT_base<2, D, RT<2, D> >;
template <typename T>
static void internal_set_BC(D& d, T& t);
};

template <typename D>
template <typename T>
void RT<2, D>::internal_set_BC(D& d, T& t)
{//code
}

或者更简单,没有CRTP

template<int n, typename D>
class RT_Base : public BC<n, D>
{
protected:
template<typename T>
static void internal_set_BC(D& d, T& t);
};

template<typename D>
class RT_Base<2, D> : public BC<2, D>
{
protected:
template <typename T>
static void internal_set_BC(D& d, T& t);
};

template <typename D>
template <typename T>
void RT_Base<2, D>::internal_set_BC(D& d, T& t)
{//code
}

template<int n, typename D>
class RT : public RT_Base<n, D>
{
public:
void set_BC(D& d, A& a)
{
this->internal_set_BC(d, a);
}
void set_BC(D& d, B& b)
{
this->internal_set_BC(d, b);
}
};

关于c++ - 错误 : invalid use of incomplete type (Maybe a definition issue),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32456853/

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