c++ - 我的成员模板函数声明有什么问题?

标签 c++

我正在尝试在模板之外声明一个成员函数 - GetValue。

我收到错误: main.cpp|16|错误:“GenericType Test::GetValue()”的重新定义| |错误:'GenericType Test::GetValue()' 先前在此处声明|

#include <iostream>

template <class GenericType>
class Test {
public:
        GenericType x;
        Test(){        }

        Test(int y) : x(  y){ }

        GenericType GetValue(){}
};


template <class GenericType>
GenericType Test<GenericType>::GetValue(){
    return x;

}

int main()
{

    Test<int> y(5);
    std::cout << y.GetValue();
    return 0;
}

最佳答案

改变成员函数定义

GenericType GetValue(){}

成员函数声明

GenericType GetValue();

关于c++ - 我的成员模板函数声明有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18875726/

相关文章:

c++ - gdb 中的递归 sizeof?

c++ - 减少 c++ 链接时间的技巧

c++ - 从 xcode c++ 程序终端调用 OSX 包

c++ - 在没有 Visual Studio 的情况下构建 Visual Studio 6 项目

c++ - 在C++中,为什么虚函数使类类型不符合聚合条件?

c++ - 需要帮助开始使用 Boost.Python

c++ - 如何使用指针调整字符串拆分算法,使其改用迭代器?

c++ - x64 转换后指针截断

c++ - 如何想出一个高缓存未命中率的例子?

c++ - 重复符号链接(symbolic link)器错误(C++ 帮助)