c++ - 模板类错误

标签 c++ templates ubuntu g++

您好,我正在尝试为模板构建空函数,以便稍后填写详细信息。这是我的代码:

namespace my {
    template <class T>
    class Sptr {
    private:
        //some kind of pointer
            //one to current obj
        T obj;
        size_t reference_count;
            //one to original obj
    public:
        template <typename U> Sptr(U *);
        Sptr(const Sptr &);
        //template <typename U> Sptr(const Sptr<U> &);
        ~Sptr();
        T* operator->() {return &obj;};
        template <typename U> Sptr<T> &operator=(const Sptr<U> &);
        //overload *,->,=,copy-constructor

        // const-ness should be preserved.
        // Test for null using safe-bool idiom
        // Static casting, returns a smart pointer
    };


    template <typename U> Sptr<U>::Sptr(U* u) {
        //do something
    }

    template <typename T> Sptr<T>::Sptr(const Sptr<T> &copyObj) {
        //do copy constructor stuff
    }

    template <typename T> Sptr<T>::Sptr& operator=(const Sptr<T> &T) {
        return *this;
    }
}

但是编译的时候出现如下错误

Sptr.hpp:30:24: error: prototype for ‘my::Sptr<T>::Sptr(U*)’ does not match any in class ‘my::Sptr<T>’
Sptr.hpp:17:3: error: candidates are: my::Sptr<T>::Sptr(const my::Sptr<T>&)
Sptr.hpp:16:25: error:                 template<class T> template<class U> my::Sptr::Sptr(U*)
Sptr.hpp:38:24: error: ‘my::Sptr<T>::Sptr’ names the constructor, not the type

我该如何解决它们?

最佳答案

template <typename U> Sptr<U>::Sptr(U* u) {
    //do something
}

应该是

template <typename T> 
   template <typename U>
       Sptr<T>::Sptr(U* u) {
        //do something
    }

其他成员函数模板也类似。

关于c++ - 模板类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712669/

相关文章:

templates - CSS3 样式表模板

ubuntu - ubuntu 16 服务器上微软软件包的哈希和不匹配

c++ - G++ 如何编译与 .cpp 位于不同目录中的 .h

c++ - std::map 和 std::set 不返回带提示的 bool

c++ - 使用 NtCreateKey() NTAPI 函数创建新的注册表项 [NtOpenKey() 函数返回 NTSTATUS 错误值 -1073741772]

python - 有没有办法使用 pip 一次安装所有 python 模块?

php - 使用 apt-src 在 ubuntu 上编译 php 时遇到问题

c++ - catch-all-rethrow 与根本没有 try-catch block 有什么不同吗?

c++ - 尽管已声明,但找不到模板化函数

python - 如何从 index.html 中的模板返回 appengine 数据存储对象的键?