使用 c++ 模板
Using templates for c++
我尝试在以下代码上使用模板,但我对主函数的第一行有问题!
{
#include <iostream>
using namespace std;
template <class T>
struct node
{
T inf;
node<T> * next;
};
template <class S>
class String
{
private :
int Slength;
node<S> * SS;
public :
void get_String()
{
SS = new node<S>;
SS -> next = NULL;
node<S> * now = SS;
char input;
while(input = getchar())
{
if((input =='n') || (input == ' ') || (input == 't'))
break;
now -> inf = input;
now -> next = new node<S>;
now = now -> next;
now -> next = NULL;
++Slength;
}
}
void show()
{
node<S> * now = SS;
while(now -> next != NULL)
{
cout << now -> inf ;
now = now -> next;
}
cout << endl;
}
};
int main()
{
String a;
a.get_String();
a.show();
cout << char(0) << " " << int(' ') << " " <<endl;
system("pause");
return 0;
}
我通过添加解决了问题
<char>
然后!有没有其他好的和有效的方法。您知道模板的任何可靠且可读的参考吗?
由于String
是一个模板类,它应该是:
String<char> a;
相关文章:
- .cpp和.h文件中的模板专用化声明
- C++模板来检查友元函数的存在
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 如何在c++中为模板函数实例创建快捷方式
- 使用C++中的模板和运算符重载执行矩阵运算
- 有人能分解一下这个c++模板的语法吗
- 如何在c++17中制作一个模板包装器/装饰器
- 模板化建造师专业化
- 调用专用模板时出错"no matching function for call to [...]"
- 模板元程序查找相似的连续类型名称
- 如何在C++20中创建模板别名的推导指南
- 没有名称的C++模板参数
- 具有重复类型的C++可变模板
- 如何将enable-if与模板参数和参数包一起使用
- 没有用于初始化C++中的变量模板的匹配构造函数