English 中文(简体)
用户定义的字符串类超载 {} 运算符
原标题:overloading >> operator for user defined string class

Question

问题是,我正试图使用插入操作员获取用户输入, 并初始化值 thecharps , 将大小分配给需要输入长度的字符i, 我如何获得? 以及插入操作员中的初始化 。

主要问题是插入操作员的问题。

当我运行程序时 它会显示分割断层断层

plz 帮助帮助

class string1
{    
  private:
    int len;
    char *thechars;

    //friend ostream& operator<<(ostream&,string1&);##
    //friend istream& operator>>(istream&,string1&);##

  public:
    //string1() :len(0),thechars(NULL){}
    string1()
    {
      thechars = new char[1];
      thechars[0] =   ;
      len=0;
      // cout << "	Default string constructor
";
      // ConstructorCount++;
    }
};

// this is the insertion operator i use
istream& operator>>(istream& in, string1& tpr)
{
  in >> tpr.thechars;
  //tpr.thechars[i+1]=  ;
  return in;
}

//this one is the extraction operator
ostream& operator<<(ostream& out,string1& prt)
{
  for(int i=0;i<prt.len;i++)
    out<<prt.thechars[i];

  return out;
}

// main function##
string1 str;
cout << "enter first string" << endl;
cin >> str;
cout << str << endl;
问题回答

如果 in 是文件输入流, 您可以做以下操作 :

in.seekg(0, ios::end);
length = in.tellg();
in.seekg(0, ios::beg);

另一个选项是按字符读取输入流字符, 每次耗尽时翻倍于 thechars 的大小。 首先, 引入一个变量以存储缓冲区当前分配的大小 -- -- allecSize 。 在此之后更新构建器和 operator< 如下。

构造器 :

string1()
{
    allocSize = 1; // initially allocated size
    thechars = new char[allocSize];
    thechars[0] =   ;
    len=0;
}

输入运算符 :

istream& operator>>(istream& in, string1& tpr)
{
    char inp;
    while (in.get(inp)) {
        // end-of-input delimiter (change according to your needs)
        if (inp ==    )
            break;
        // if buffer is exhausted, reallocate it twice as large
        if (tpr.len == tpr.allocSize - 1) {
            tpr.allocSize *= 2;
            char *newchars = new char[tpr.allocSize];
            strcpy(newchars, tpr.thechars);
            delete[] tpr.thechars;
            tpr.thechars = newchars;
        }
        // store input char
        tpr.thechars[tpr.len++] = inp;
        tpr.thechars[tpr.len] =   ;
    }
}

但最好的选择是使用 std::string 作为 charps 的一种类型。 您真的需要全部手动处理内存吗?

而不是在 中给 in a col_char%/code> 给它一个普通字符串。 然后您可以自己提取数据 。

istream& operator>>(istream& in, string1& tpr)
{
  string temp;
  in >> temp;
  tpr.len = temp.length + 1;
  tpr.thechars = new char[tpr.len];
  tpr.thechars[temp.length] =   ;
  strcpy(tpr.thechars, &temp[0], tpr.len);
  return in;
}

您撰写了

 in>> tpr.thechars; // where thechars=""; 

You allocated only one byte, but i guess you are input string with more bytes. I think error here.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?