如何为非模板类​​定义模板方法?

发布于 2024-10-12 06:59:50 字数 1058 浏览 2 评论 0原文

我的编译器对我实现模板方法的方式不满意。它为这些实现提供了大量错误消息,例如“未定义类型T”。

这是我的第一个方法,它是在类块之外实现的:

class VectorConvertor
{
    public:
        // ...
        template <class T>
        static void ReverseVectorElements(std::vector<T> & Vector);
        // ...
};

template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
    std::vector<T>::size_type size = Vector.size();
    T swap;
    for (std::vector<T>::size_type i=0; i<size/2; i++)
    {
        swap = Vector.at(i);
        Vector.at(i) = Vector.at(size-1-i);
        Vector.at(size-1-i) = swap;
    }
}

另一个是这个;这次该方法是在类内部实现的:

class ElementaryMath
{
    public:
        // ...
        template <class T> static char sign(T num)
        {
            return (num >= static_cast<T>(0)) ? static_cast<char>(+1) : static_cast<char>(-1);
        }
        // ...
}

我的代码有什么问题吗,或者这只是编译器本身的问题?

集成开发环境编译器:Visual Studio 2010

My compiler is not happy with the way I implement my template methods. It gives tons of error messages like "undefined type T" for these implementations.

This is my first method, it is implemented outside of the class block:

class VectorConvertor
{
    public:
        // ...
        template <class T>
        static void ReverseVectorElements(std::vector<T> & Vector);
        // ...
};

template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
    std::vector<T>::size_type size = Vector.size();
    T swap;
    for (std::vector<T>::size_type i=0; i<size/2; i++)
    {
        swap = Vector.at(i);
        Vector.at(i) = Vector.at(size-1-i);
        Vector.at(size-1-i) = swap;
    }
}

Another one is this; this time the method is implemented inside the class:

class ElementaryMath
{
    public:
        // ...
        template <class T> static char sign(T num)
        {
            return (num >= static_cast<T>(0)) ? static_cast<char>(+1) : static_cast<char>(-1);
        }
        // ...
}

Is there anything wrong with my code, or is this just a problem with the compiler itself?

IDE & Compiler: Visual Studio 2010

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

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

发布评论

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

评论(5

我的奇迹 2024-10-19 06:59:50

你的代码对我来说看起来没问题。但有一件事一直萦绕在我的脑海里。你能检查一下函数“sign”之前是否定义过吗?只需将鼠标悬停在其上即可。 C 运行时库使用“#define”关键字实现其某些函数,因此您之后无法定义具有相同名称的函数。

Your code looks OK to me. But there is one thing that got in my mind. Can you please check if the function "sign" is defined before? Simply hover your mouse on it. C runtime library implements some of its functions using the "#define" keyword and because of that you can't define a function with that same name afterwards.

我不是你的备胎 2024-10-19 06:59:50

您缺少一些 typename 和分号,但除此之外,代码看起来还不错。恕我直言,如果它仍然不起作用,那么是时候提交错误了。

顺便说一句,交换代码最好使用 std::swap 来完成。

You're missing some typenames and semicolons, but otherwise, the code seems OK. IMHO it's time to file a bug if it still doesn't work.

BTW, the swapping code would be better done with std::swap.

泪是无色的血 2024-10-19 06:59:50

在此输入名称:

template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
    typename std::vector<T>::size_type size = Vector.size();
    T swap;
    for (typename std::vector<T>::size_type i=0; i<size/2; i++)
    {
        swap = Vector.at(i);
        Vector.at(i) = Vector.at(size-1-i);
        Vector.at(size-1-i) = swap;
    }
}

typename here:

template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
    typename std::vector<T>::size_type size = Vector.size();
    T swap;
    for (typename std::vector<T>::size_type i=0; i<size/2; i++)
    {
        swap = Vector.at(i);
        Vector.at(i) = Vector.at(size-1-i);
        Vector.at(size-1-i) = swap;
    }
}
谁的年少不轻狂 2024-10-19 06:59:50
class VectorConvertor
{
    public:
    // ...
    template <typename T>
    static void ReverseVectorElements(std::vector<T> & Vector);
};

template <typename T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
    std::vector<T>::size_type size = Vector.size();
    T swap;
    for (std::vector<T>::size_type i=0; i<size/2; i++)
    {
        swap = Vector.at(i);
        Vector.at(i) = Vector.at(size-1-i);
        Vector.at(size-1-i) = swap;
    }
}

int main()
{
    std::vector <int> i(10,0);

    VectorConvertor obj;   // Since your class isn't a template, template parameter
                           // isn't required for a class template instantiation.
                           // However, if your class was a template class, template-
                           // parameter must have been required for a class template
                           // instantiation.

    obj.ReverseVectorElements(i);  // Equal to obj.ReverseVectorElements<int>(i);
                                   // Based on type of i, compiler can instantiate a 
                                   // template function by default.
    getchar();
    return 0;
}

希望这有帮助!

class VectorConvertor
{
    public:
    // ...
    template <typename T>
    static void ReverseVectorElements(std::vector<T> & Vector);
};

template <typename T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
    std::vector<T>::size_type size = Vector.size();
    T swap;
    for (std::vector<T>::size_type i=0; i<size/2; i++)
    {
        swap = Vector.at(i);
        Vector.at(i) = Vector.at(size-1-i);
        Vector.at(size-1-i) = swap;
    }
}

int main()
{
    std::vector <int> i(10,0);

    VectorConvertor obj;   // Since your class isn't a template, template parameter
                           // isn't required for a class template instantiation.
                           // However, if your class was a template class, template-
                           // parameter must have been required for a class template
                           // instantiation.

    obj.ReverseVectorElements(i);  // Equal to obj.ReverseVectorElements<int>(i);
                                   // Based on type of i, compiler can instantiate a 
                                   // template function by default.
    getchar();
    return 0;
}

Hope this helps !

拿命拼未来 2024-10-19 06:59:50

您的代码在 VS2005 上编译时没有错误(除了 ElementaryMath 定义末尾缺少分号),因此您可能会看到编译器错误。

VS2010 SP1 已推出测试版 这里。可能有帮助,但显然它是测试版......

Your code compiles without error on VS2005 (apart from a missing semicolon at the end of the definition of ElementaryMath) so you may be looking at a compiler bug.

VS2010 SP1 is available in beta here. Might help, but obviously its beta...

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