如何使这个专门的功能发挥作用

发布于 2024-10-24 01:03:01 字数 1400 浏览 1 评论 0原文

事情是这样的。我查看了这个论坛,但没有找到我正在搜索的信息,或者我可能无法针对我的问题重复它。我有一个通用的类 Table,并且有一个名为 MyString 的类。

template <typename typeGen, int DIM>
class Table {
    public:
        TableauGenerique() : index_(0) { //On initialise courant à 0
        }
        void add(typeGen type);
    private:
        typeGen tableGen_[DIM];
        int index_;    
};

我的问题是添加功能。 我有时必须在 main.cpp 中执行此操作:(效果很好)

 Table <float,6> tabFloat;
    tabFloat.add(1.6564);

,并且在某一时刻,我需要执行此操作,但这不起作用,因为我需要专门化 add 函数来创建 MyString 的对象,以传递它字符串,然后将对象存储在数组(tableGen)中:

TableauGenerique <MyString,4> tabString;

所以我尝试了这个(课后),但没有成功。

template <typename typeGen, int DIM>
void Table<typeGen,DIM>::add(typeGen type){ //Which is the generic one for float or ints
    if(index_ < DIM) {
        tableGen_[courant_] = type;
        index_++; 
    }
}

template <class typeGen, int DIM>
void Table<typeGen,DIM>::add<string>(typeGen type) { //(line 75) Which is the specific or specialized function for myString
    MyString str(type);
    if(index_ < DIM) {
        tableGen_[courant_] = str;
        index_++; 
    }
}

那么,我怎样才能完成这项工作,因为它根本无法编译,说:line75:错误:预期的初始化程序在“<”之前令牌,主要是说不匹配调用 Table::add(const char[6]) 的函数,

我希望一切都足够清楚。如果有不清楚的地方请告诉我。

非常感谢您的帮助!

Here's the deal. I've looked on this forum and I didn't find the information I'm searching for or I'm probably not able to repeat it for my problem. I have a class Table which is generic and I have a class named MyString.

template <typename typeGen, int DIM>
class Table {
    public:
        TableauGenerique() : index_(0) { //On initialise courant à 0
        }
        void add(typeGen type);
    private:
        typeGen tableGen_[DIM];
        int index_;    
};

My problem is with the add function.
I sometimes have to do this in the main.cpp: (which works well)

 Table <float,6> tabFloat;
    tabFloat.add(1.6564);

and at one point, I need to do this which doesn't work because I need to specialize the add function to create an object of MyString, to pass it the string and then store the object in the array (tableGen) :

TableauGenerique <MyString,4> tabString;

So I tried this (after the class), without success.

template <typename typeGen, int DIM>
void Table<typeGen,DIM>::add(typeGen type){ //Which is the generic one for float or ints
    if(index_ < DIM) {
        tableGen_[courant_] = type;
        index_++; 
    }
}

template <class typeGen, int DIM>
void Table<typeGen,DIM>::add<string>(typeGen type) { //(line 75) Which is the specific or specialized function for myString
    MyString str(type);
    if(index_ < DIM) {
        tableGen_[courant_] = str;
        index_++; 
    }
}

So, How can I make this work because it doesn't compile at all, saying: line75 : error: expected initializer before '<' token and in the main it says not matching function to call Table::add(const char[6]),

I hope everything is clear enough. Let me know if somethings is unclear.

Thank you very much for your help !

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

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

发布评论

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

评论(3

惟欲睡 2024-10-31 01:03:01
template <class typeGen, int DIM>
void Table<typeGen,DIM>::add<string>(typeGen type)

您试图专门化 add(),但实际上它并不是一个函数模板。您期望它如何发挥作用?


您可能的意思是:(类的专门化)

template <int DIM>
void Table<string,DIM>::add(string type)

但是只有当您专门化类本身时才允许这样做。如果没有专门化该类,上面的代码将给出编译错误!

编辑:

您可以阅读这些在线教程:

template <class typeGen, int DIM>
void Table<typeGen,DIM>::add<string>(typeGen type)

You're trying to specialize add() when in fact it is not a function template to begin with. How do you expect it to work?


You probably meant: (specialization of the class)

template <int DIM>
void Table<string,DIM>::add(string type)

But then this is allowed only if you specialize the class itself. Without specializing the class, the above code would give compilation error!

EDIT:

You can read these online tutorials:

芯好空 2024-10-31 01:03:01

如果您可以控制 MyString 类的代码,则可以提供充当从 floatMyString 的隐式转换的构造函数。示例:

#include <string>
#include <sstream>
#include <iostream>

class MyString {
public:
  MyString(float number) {
    std::stringstream buffer;
    buffer << number;
    value = buffer.str();
  }

  void print() {
    std::cout << value << std::endl;
  }
private:
  std::string value;
};

template <class T>
class Foo {
public:
  void DoStuff(T item) {
    item.print();
  }
};

int main() {
  Foo<MyString> foo;

  foo.DoStuff(1.342); // implicitly converts float to MyString

  return 0;
}

这样,您不需要对 add 方法进行任何专门化。但是,隐式转换很棘手,您必须小心不要意外调用它们,并且它们可能会产生歧义。

If you can control the code of the MyString class, you can provide constructors that act as implicit conversions from float to MyString. An example:

#include <string>
#include <sstream>
#include <iostream>

class MyString {
public:
  MyString(float number) {
    std::stringstream buffer;
    buffer << number;
    value = buffer.str();
  }

  void print() {
    std::cout << value << std::endl;
  }
private:
  std::string value;
};

template <class T>
class Foo {
public:
  void DoStuff(T item) {
    item.print();
  }
};

int main() {
  Foo<MyString> foo;

  foo.DoStuff(1.342); // implicitly converts float to MyString

  return 0;
}

This way, you do not need any specialization of the add method. However, implicit conversions are tricky, and you have be careful not to invoke them accidentally, and they may create ambiguities.

无法回应 2024-10-31 01:03:01

编辑:经过再三考虑,我的以下建议基本上等同于

Table<MyString,4> tabString;
tabString.add(MyString("whatever"));

因此过多和/或不能解决问题。请随意忽略:)


我将使用通用方法扩展 Table 类,以添加一些内容,您可以从中构造所需类型的对象:

template <typename typeGen, int DIM>
class Table {
    public:
        Table() : index_(0) {}
        void add(typeGen type);
        // The additional method
        template<typename T> void add(const T& src);
    private:
        typeGen tableGen_[DIM];
        int index_;
};

template<typename typeGen, int DIM>
template<typename T>
void Table<typeGen,DIM>::add(const T& src) {
    if(index_ < DIM) {
        tableGen_[courant_] = typeGen(src);
        index_++;
    }
}

请注意在赋值之前构造临时 typeGen 对象。
假设 MyString 对象可以从字符串文字构造,即从 const char* 构造,然后您可以按如下方式使用它:

Table<MyString,4> tabString;
tabString.add("whatever");

或者如果上述假设是错误的,则以下内容可能应该有效(因为您从字符串实例构造了 MyString 实例):

tabString.add(string("whatever"));

EDIT: Upon a second thought, my suggestion below is basically equivalent to

Table<MyString,4> tabString;
tabString.add(MyString("whatever"));

and therefore excessive and/or does not solve the problem. Feel free to ignore :)


I would extend the class Table with a generic method to add something from which you can construct an object of the desired type:

template <typename typeGen, int DIM>
class Table {
    public:
        Table() : index_(0) {}
        void add(typeGen type);
        // The additional method
        template<typename T> void add(const T& src);
    private:
        typeGen tableGen_[DIM];
        int index_;
};

template<typename typeGen, int DIM>
template<typename T>
void Table<typeGen,DIM>::add(const T& src) {
    if(index_ < DIM) {
        tableGen_[courant_] = typeGen(src);
        index_++;
    }
}

Note construction of a temporary typeGen object before the assignment.
Assuming that MyString object can be constructed from a string literal, i.e. from const char*, you can then use it as following:

Table<MyString,4> tabString;
tabString.add("whatever");

or if the above assumption is wrong, the following should probably work (because you constructed a MyString instance from a string instance):

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