C++ typedef 和模板

发布于 2024-12-21 22:56:18 字数 446 浏览 3 评论 0原文

假设有一个小类,

template<class T1>
class c {
    template<class T>
    class Test {
    public:
        typedef std::vector<T> vetor_type;

        vetor_type some_var;
    };

    void f() {
        Test<int>::vetor_type tt; //error
    }
};

我收到一个错误:

预期的“;”表达后。

编辑:我不知道为什么有关类型名的答案被删除,因为它实际上有帮助。 但是有人可以解释一下,如果我在另一个类模板中编写此代码,为什么我必须使用 typename ?

Suppose there is a tiny class

template<class T1>
class c {
    template<class T>
    class Test {
    public:
        typedef std::vector<T> vetor_type;

        vetor_type some_var;
    };

    void f() {
        Test<int>::vetor_type tt; //error
    }
};

I get an error:

Expected ';' after expression.

Edit: I don't know why the answer about the typename was deleted, cause it actually helped.
But could someone explain why do i have to use typename if I'm writing this code inside another class template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酒忠诚 2024-12-28 22:56:18

Test 取决于用于实例化 c 的类型,因此您需要在 的定义中使用 typename >foo()

void f() {
    typename Test<int>::vetor_type tt;
}

Test<T> is dependent on the type used to instantiate c<T1> with so you need to use typename in the definition within foo().

void f() {
    typename Test<int>::vetor_type tt;
}
把时间冻结 2024-12-28 22:56:18

..因为代码对我来说看起来不错。

可能存在拼写错误:也许vetor_type在一个地方,而vector_type在其他地方?

.. as it is that code looks good to me.

There's a possible typo: maybe vetor_type is in one place and vector_type somewhere else ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文