简体   繁体   English

模板 class 专业化使用 class 作为专业化类型

[英]Template class specialization using class as type for the specialization

I'm currently working on a program with C++ 14;我目前正在使用 C++ 14 开发一个程序; basically I was implementing a template class called Database and i wanted to differentiate the constructor of this template class basing it on different specialization of my template.基本上我正在实现一个名为数据库的模板 class,我想根据我模板的不同专业化来区分这个模板 class 的构造函数。 In other words i wanted it to do different things when I specialize my type with a specific class.换句话说,当我使用特定的 class 专门化我的类型时,我希望它做不同的事情。 in my example I wanted to do different things when i pass the class boat to it (boat is define by a constructor with 5 strings).在我的示例中,当我将 class 船传递给它时,我想做不同的事情(船由具有 5 个字符串的构造函数定义)。 ''' '''

#include"boat.h"
   #include<vector>
  #include<iostream>
   #include<string>
   #include<fstream>
   using namespace std;



template <typename T>
class Database{


public:
    Database(const string& file_name);
    ~Database();
    void Add (const string& file_list);
    void Upload (const string& file_list);
     vector <T>  get_vector();
protected:
    vector <T> _list;
    vector <T>_new_element;
    string token,st;
    string delimiter=";";
    size_t pos=0;
    vector <string> temp;
    fstream f;
    int max_id;

};

template <>
class Database<boat>
{
friend class boat;
public:
    Database(const string& file_name){
        f.open(file_name);
        //controllo apertura file
        if (f.is_open()) {
            while ((getline(f, token))) {

                while ((pos = token.find(delimiter)) != (string::npos)) {
                    st = token.substr(0, pos);
                    tempg.push_back(st);
                    token.erase(0, pos + delimiter.length());
                }
                tempg.push_back(token);
                _list.push_back(boat(tempg[0],tempg[1],tempg[2],tempg[3],tempg[4]));
                pos = 0;
                tempg.clear();
            }
        }
    };
    ~Database();
    void Add (const string& file_list);
    void Upload (const string& file_list);
    const vector <boat>  get_vector();
protected:
    vector <boat> _list;
    boat _new_element();
    string token,st;
    string delimiter=";";
    size_t pos=0;
    vector <string> tempg;
    ifstream f;

};'''

So basically I did this method with the constructor to open a file and read the important data of the class boat which were divided by ';'.所以基本上我用构造函数做这个方法打开一个文件并读取class船的重要数据,这些数据被';'分隔。

My question is: how i have to define the syntax for the template constructor specialization, when I pass to the template not a normal type like int , char or other, but when I pass a class.我的问题是:当我传递给模板而不是普通类型(如intchar或其他)时,我必须如何定义模板构造函数专业化的语法,但是当我传递 class 时。 Because it give me the error:因为它给了我错误:

boat was not declared in this scope class Database.船未在此 scope class 数据库中声明。

How do I declare it right?我如何声明它是正确的?

You can do somethimng like this:你可以这样做:

template<>
Database<boat>::Database(const string& file_name)
{
    // ... your stuff here ...
}

But I think that the more correct design is to have template implementation of the constructor and then have stream input and output operartors for the class boat , ie但我认为更正确的设计是有构造函数的模板实现,然后有 stream 输入和 output 操作符 class boat ,即

std::istream& operator>>(std::istream& is, boat& obj)
{
   // ... actual input here ...
   return is;
}

std::ostream& operator<<(std::ostream& os, const boat& obj)
{
   // ... actual output here ...
   return os;
}

and then use them to load/store data in the file.然后使用它们在文件中加载/存储数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM