尝试通过运算符+重载在不同的对象中添加2个char数组

trying to add 2 char arrays in different objects via operator+ overloading

本文关键字:对象 添加 2个 数组 char 运算符 重载      更新时间:2023-10-16

我似乎很难让这个加法运算符重载为我的对象工作。我翻遍了网,搞不清楚我做错了什么。基本上,它应该被重载以连接2个字符数组。我尝试的每件事都会导致崩溃或字符的随机混乱。

我是操作员重载的新手,所以可能我用错了。它被称为:

String myString1, myString2; 
std:: cout << "nYour strings added are: " << myString1 + myString2;

在头文件中。。。

 String operator+(String);

这是相关代码:

String String::operator+(String rhs){
String result;
int i = length;
for(int x = 0; x < length; x++)
{
        result.ch[x] = ch[x];
        std::cout << result.ch[x];
}
std:: cout << " ";
for (int j = 0; rhs.ch[j] != ''; ++i, ++j)
{
         result.ch[i] = rhs.ch[j];
         std::cout <<result.ch[i];
}
return result;
}

如果需要,我会提供完整的源代码。不过,其他一切都很完美。

.hpp:

#include<iostream>
#include<string>
using std::istream;
using std::ostream;
const int default_size = 256;
class String
{
  public:
         // constructors
         String();
         String(const char ch, const int default_size);
         String(const String& rhs, int cap = default_size); // copy constructor
         // accessor functions
         void getLength(String str);
         void outPut(String str); 
         void findChar(String str);
         // destructor
         ~String (); // destructor
         // mutator functions
         void getInput(String str); 
         // friend I/O function overloads
         friend std::istream& operator>>(std::istream&, const String& str);
         friend std::ostream& operator<<(std::ostream&, const String& str);
         // operator overloads
         String operator+(String str);
  private:
          int strLength, length;
          int capacity;
          char ch[default_size];
          char *s; };

和.cpp

// String.cpp
#include "String.hpp"
#include<iostream>
// default constructor
String::String() 
{
}
// char constructor
String::String(const char ch, const int default_size)
{
capacity = default_size;
s = new char[capacity];
s[0] = ch;
s[1] = '';
strLength = 1;
}
//copy constructor
String::String(const String& rhs, const int cap)
{
// Check if string capacity is shorter than length
if (cap <= rhs.strLength)
    capacity = rhs.strLength + 1;
    else
        capacity = cap;
    s = new char[capacity];
// Copy chars to string
for (length = 0; rhs.s[length] != ''; ++length)
{
    s[length] = rhs.s[length];
}
s[length] = '';
}
// destructor
String::~String()
{
strLength = 0;
delete [] s;
}
// overloaded io operatiors
// Outputs string with << operator
ostream& operator<<(ostream& out, const String& str)
{
out << str.s;
return out;
}
// Inputs string with >> operator
istream& operator>>(istream& in, const String& str)
{
in >> str.s;
return in;
}
// gets users input
void String::getInput(String str)
{
 std:: cout << "Please enter a string.n";
 std:: cin.getline(ch, default_size);
}
// getLength
void String:: getLength(String str)
{
  size_t strLength = strlen(str.ch);
  length = strLength;
  std:: cout << "The length is " << strLength << ".n";
}
// output
void String:: outPut(String str)
{
 std:: cout << "nYou entered.n";
 for(int x = 0; x < str.length; x++)
 {
         std:: cout << ch[x];
 }
         std:: cout << "n";
 }
// user enters a character, this provides the location(s)
void String:: findChar(String str)
{
char * charSearch;
char s;
std:: cout << "nPlease enter a character to search for. n";
std:: cin >> s;
std:: cout << "n";
charSearch = strchr(ch, s);
while(charSearch != '')
{
                 std:: cout << "Found at: " << charSearch-ch+1 << "n" ;
                 charSearch = strchr(charSearch+1, s);
}
std::cin.ignore();
}
// OPERATOR OVERLOADS---------------------------------------------------
// Addition operator 
// adds the right hand object to the lefthand object
String String::operator+(String rhs)
{
String result; 
int i = length;
for(int x = 0; x < length; x++)
{
        result.ch[x] = ch[x];
        std::cout << result.ch[x];
}
std:: cout << " ";
for (int j = 0; rhs.ch[j] != ''; ++i, ++j)
{
         result.ch[i] = rhs.ch[j];
         std::cout <<result.ch[i];
}
std::cout << "nHere is result.ch: ";
for(int z = 0; result.ch[z] != ''; z++)
{
        std::cout << result.ch[z];
}
return result;
}

您遇到的问题比串联更多,我怀疑其他一切是否"完美"。

  1. 默认构造函数不会初始化任何内容
    即使是String s; std::cout << s;也不应该工作,String s; std::cin >> s;也不应该。

  2. 您没有赋值运算符
    {String s1; String s2; s2 = s1;}很可能会崩溃。

  3. 您有一个指针s和一个数组ch
    您将数据存储在s中,但有些代码使用ch
    你需要下定决心
    (参数和局部变量的名称不要与成员的名称相同。)

  4. 您同时拥有strLengthlength,并随意使用它们。

代码中的未定义行为比任何人列出的都多
你最好的出路可能是扔掉代码,重新开始。