如何使这个专门的功能发挥作用
事情是这样的。我查看了这个论坛,但没有找到我正在搜索的信息,或者我可能无法针对我的问题重复它。我有一个通用的类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您试图专门化
add()
,但实际上它并不是一个函数模板。您期望它如何发挥作用?您可能的意思是:(类的专门化)
但是只有当您专门化类本身时才允许这样做。如果没有专门化该类,上面的代码将给出编译错误!
编辑:
您可以阅读这些在线教程:
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)
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:
如果您可以控制
MyString
类的代码,则可以提供充当从float
到MyString
的隐式转换的构造函数。示例:这样,您不需要对 add 方法进行任何专门化。但是,隐式转换很棘手,您必须小心不要意外调用它们,并且它们可能会产生歧义。
If you can control the code of the
MyString
class, you can provide constructors that act as implicit conversions fromfloat
toMyString
. An example: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.
编辑:经过再三考虑,我的以下建议基本上等同于
因此过多和/或不能解决问题。请随意忽略:)
我将使用通用方法扩展 Table 类,以添加一些内容,您可以从中构造所需类型的对象:
请注意在赋值之前构造临时 typeGen 对象。
假设 MyString 对象可以从字符串文字构造,即从 const char* 构造,然后您可以按如下方式使用它:
或者如果上述假设是错误的,则以下内容可能应该有效(因为您从字符串实例构造了 MyString 实例):
EDIT: Upon a second thought, my suggestion below is basically equivalent to
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:
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:
or if the above assumption is wrong, the following should probably work (because you constructed a MyString instance from a string instance):