【发布时间】:2015-05-23 11:13:48
【问题描述】:
TestString::TestString(int num)
这是一个转换构造函数,应该将整数转换为字符串。
例如,如果传入 123,则 TestString 对象应存储将由 c-string "123" 表示的字符串数据。
class TestString //header file
{
public:
TestString (int num);
//etc.
private:
int size;
char* str;
};
TestString::TestString (int num) //.cpp file
{
char c = static_cast<char>(num);
str = new char[size]; //missing size variable
int i = 0;
for (i; cstr[i] != '\0'; i++) //missing cstr array
str[i] = cstr[i];
str[i] = cstr[i]; //to tack on null character
}
如您所知,我在定义中缺少大小变量和 cstr 字符串。我不知道我是否将这一切都搞错了,或者只是无法理解我被要求进行什么样的设置......
非常感谢任何指针或建议。
仅允许使用库:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
【问题讨论】:
-
您是否有理由继续使用
c函数而不是使用c++std::string。还是关于 MS 的CString? -
我个人更喜欢字符串,但 cstring 是要求之一。