【问题标题】:How to templating a class constructor如何模板类构造函数
【发布时间】:2018-10-21 22:02:51
【问题描述】:

我学习了如何使用模板,它可以节省很多时间,但是当我尝试使用带有构造函数的模板时,它会出错,我不知道如何修复错误。

我的项目包含...

main.cpp

#include "Header.h"
#include <iostream>
using namespace std;
int main(){
    c a(1);
    c b(a);
    a.f(2);
    b.f(a);
    return 0;
}

头文件.h

#pragma once
#include <iostream>
using namespace std;
class c {
public:
    template<typename T>
    c(const T&);
    ~c();
    template<typename T>
    void f(const T&);
private:
    uint64_t data;
};
                              //Constructors
template<typename T>
inline c::c(const T& input) {
    data = input;
}
template<>
inline c::c<c>(const c& input) {  //This line produced errors
    data = input.data;
}
                              //Destructor
inline c::~c() {}
                              //Functions
template<typename T>
inline void c::f(const T& input){  //Magic function
    cout << (data += input) << endl;
}
template<>
inline void c::f<c>(const c& input){  //Magic function
    cout << (data += input.data) << endl;
}

我正在使用 Visual Studio 2017,错误是...

C2988 unrecognizable template declaration/definition
C2143 syntax error: missing ';' before '<'
C7525 inline variables require at least '/std:c++17'
C2350 'c::{ctor}' is not a static member
C2513 'c::{ctor}': no variable declared before '='
C2059 syntax error: '<'
C2143 syntax error: missing ';' before '{'
C2447 '{': missing function header (old-style formal list?)

不知何故构造函数不能正常工作,但函数可以。

谁能给我解释一下?

【问题讨论】:

  • 该;在 c 类声明的末尾
  • 你能标出错误所指的行吗?
  • 不确定您对 template&lt;&gt; c::c&lt;c&gt;(const c&amp; input) 的期望。
  • 你已经有正确的构造函数template&lt;typename T&gt; c::c(const T&amp; input)
  • @Jarod42 我知道,但是我想要特定类型的构造函数和函数,例如字符串和自己的类,因为uint64_t = string 会产生错误并且与uint64_t = class 相同,所以我创建了特定类型的构造函数和处理它的函数,但只有构造函数才会产生错误

标签: c++ visual-studio templates visual-studio-2017


【解决方案1】:

我相信,你不能用模板制作构造函数,考虑为构造函数制作整个类的模板。

    template <typename T>
    class
    {
       ...
       c<T>() = default; // for example
       c<T>(const c& input) {...}
       ...
    }

    int main()
    {
        c<int>* a = new c<int>();
    }

【讨论】:

    【解决方案2】:

    看来你想要:

    class c
    {
    public:
        template<typename T>
        c(const T&);
    
        c(const c&) = default;
    
        template<typename T>
        void f(const T&);
    
        void f(const c&);
    private:
        uint64_t data;
    };
    
    template<typename T>
    c::c(const T& input) : data(input) {}
    
    template<typename T>
    void c::f(const T& input) {  //Magic function
        data += input;
        std::cout << data << std::endl;
    }
    
    inline void c::f(const c& input) {  //Magic function
        data += input.data;
        std::cout << data << std::endl;
    }
    

    【讨论】:

    • @user463035818 我不知道...但是@Jarod42,这段代码,data(input)input 自动访问input.data 如果类相同??
    • 复制构造函数是一个特殊的成员,不会是你的模板方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2011-05-08
    • 2013-05-30
    • 2016-03-22
    • 2015-07-03
    相关资源
    最近更新 更多