C++ 使用模板的固定长度字符串
所以我有一个学校作业,要求我使用模板创建一个固定长度的字符串类,作为一个更大项目的一部分,但我在开始使用固定长度字符串时遇到困难,所以我想我会来这里寻求帮助。我没有太多的模板经验,这是造成我的问题。我目前的问题是复制构造函数,它给了我一些我不知道如何处理的错误。下面是我的类定义:C++ 使用模板的固定长度字符串,c++,C++,所以我有一个学校作业,要求我使用模板创建一个固定长度的字符串类,作为一个更大项目的一部分,但我在开始使用固定长度字符串时遇到困难,所以我想我会来这里寻求帮助。我没有太多的模板经验,这是造成我的问题。我目前的问题是复制构造函数,它给了我一些我不知道如何处理的错误。下面是我的类定义: template <int T> class FixedStr { public: FixedStr ();
template <int T>
class FixedStr
{
public:
FixedStr ();
FixedStr (const FixedStr<T> &);
FixedStr (const string &);
~FixedStr ();
FixedStr<T> & Copy (const FixedStr<T> &);
FixedStr<T> & Copy (const string &);
private:
string Data;
};
模板
类固定str
{
公众:
FixedStr();
FixedStr(constfixedstr&);
FixedStr(常量字符串&);
~FixedStr();
FixedStr&复制(const FixedStr&);
FixedStr和Copy(常量字符串&);
私人:
字符串数据;
};
这是给我带来问题的复制构造函数:
template <int T>
FixedStr<T>::FixedStr (const string & Str)
{
if (Str.length() == <T>)
strcpy (FixedStr<T>.Data, Str);
}
模板
FixedStr::FixedStr(常量字符串和Str)
{
如果(Str.length()==)
strcpy(固定Str.Data,Str);
}
有谁能给我一些关于如何处理这件事的建议吗?你看到的是容易出错还是我处理问题的方式不对?谢谢你能给我的任何帮助。未经测试:我想应该是
if (Str.length() == T)
Data = Str;
首先,在访问模板参数时不使用尖括号。其次,对于C++字符串,不使用<代码> StrucP,它们支持通过赋值复制。
请注意,类中不需要自定义析构函数或复制构造函数
字母T
通常用于类型参数。我会用Length
或N
来代替
以下是您的类的修改版本:
#include <string>
template<int N> class FixedStr {
public:
FixedStr(const std::string&);
private:
std::string Data;
};
template<int N> FixedStr<N>::FixedStr(const std::string& Str) {
if (Str.length() == N) Data = Str;
}
int main() {
FixedStr<11> test("Hello World");
}
#包括
模板类FixedStr{
公众:
FixedStr(conststd::string&);
私人:
std::字符串数据;
};
模板FixedStr::FixedStr(常量std::string&Str){
如果(Str.length()=N)Data=Str;
}
int main(){
FixedStr测试(“Hello World”);
}
未经测试:我认为应该
if (Str.length() == T)
Data = Str;
首先,在访问模板参数时不使用尖括号。其次,对于C++字符串,不使用<代码> StrucP,它们支持通过赋值复制。
请注意,类中不需要自定义析构函数或复制构造函数
字母T
通常用于类型参数。我会用Length
或N
来代替
以下是您的类的修改版本:
#include <string>
template<int N> class FixedStr {
public:
FixedStr(const std::string&);
private:
std::string Data;
};
template<int N> FixedStr<N>::FixedStr(const std::string& Str) {
if (Str.length() == N) Data = Str;
}
int main() {
FixedStr<11> test("Hello World");
}
#包括
模板类FixedStr{
公众:
FixedStr(conststd::string&);
私人:
std::字符串数据;
};
模板FixedStr::FixedStr(常量std::string&Str){
如果(Str.length()=N)Data=Str;
}
int main(){
FixedStr测试(“Hello World”);
}
无需将T
放在尖括号中,除非它正在修改类型。所以它应该是Str.length()==T
不能在
字符串上使用strcpy
,必须使用c_str()
方法来获得一个兼容的空终止字符串。但这并不重要,因为您无论如何都不应该使用strcpy
来分配string
对象。只需使用Data=Str
无需将T
放在尖括号中,除非它正在修改类型。所以它应该是Str.length()==T
不能在字符串上使用strcpy
,必须使用c_str()
方法来获得一个兼容的空终止字符串。但这并不重要,因为您无论如何都不应该使用strcpy
来分配string
对象。只需使用Data=Str
它应该是Str.length()==T
。尖括号不用于检索模板参数。换行std::string
缺少固定长度字符串IMO的完整点--您应该避免任何freestore分配,并且std::string
在内部使用freestore分配。@ildjarn:我想这只是为了学习模板的工作原理,不用于编写用于实际生产的固定长度字符串类。它应该是Str.length()==T
。尖括号不用于检索模板参数。换行std::string
缺少固定长度字符串IMO的完整点--您应该避免任何freestore分配,并且std::string
在内部使用freestore分配。@ildjarn:我想这只是为了学习模板的工作原理,不适用于为实际生产使用编写固定长度字符串类。哦,抱歉,我打开了一段时间的页面,没有刷新以查看编辑。哦,抱歉,我打开了一段时间的页面,没有刷新以查看编辑。感谢您的帮助。这解决了我的问题,缩短了我的代码,希望现在我的项目不会再有任何问题。再次感谢。谢谢你的帮助。这解决了我的问题,缩短了我的代码,希望现在我的项目不会再有任何问题。再次感谢。