尝试通过运算符+重载在不同的对象中添加2个char数组
trying to add 2 char arrays in different objects via operator+ overloading
我似乎很难让这个加法运算符重载为我的对象工作。我翻遍了网,搞不清楚我做错了什么。基本上,它应该被重载以连接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;
}
您遇到的问题比串联更多,我怀疑其他一切是否"完美"。
-
默认构造函数不会初始化任何内容
即使是String s; std::cout << s;
也不应该工作,String s; std::cin >> s;
也不应该。 -
您没有赋值运算符
{String s1; String s2; s2 = s1;}
很可能会崩溃。 -
您有一个指针
s
和一个数组ch
您将数据存储在s
中,但有些代码使用ch
你需要下定决心
(参数和局部变量的名称不要与成员的名称相同。) -
您同时拥有
strLength
和length
,并随意使用它们。
代码中的未定义行为比任何人列出的都多
你最好的出路可能是扔掉代码,重新开始。
相关文章:
- C++ 将子类的对象添加到父类的向量中
- 如何在qt中将对象添加到现有布局中?--已解决
- 测试驱动开发 c++:如何将对象添加到向量中,将歌曲添加到播放列表并对其进行测试
- 在C++中将对象添加到 2D 矢量
- 如何将对象添加到类中的静态向量
- 重载加法运算符(使用函数向对象添加字符串)
- 将对象添加到数组时EXC_BAD_ACCESS
- 仅为从某种语言编译的对象添加链接库?
- C++将带有重载构造函数的对象添加到另一个对象
- 如何在 c++ 中将对象添加到数组中?
- 尝试向 COM 对象添加另一个接口时出现静态强制转换错误 C2440
- 使用类函数将类对象添加到数组中
- 如何在 c c ++ 混合生成文件中为 c 对象添加附加规则?
- 如何从列表中将对象添加到图表中?
- 将对象添加到LinkedList C 中
- 将动态分配的对象添加到对象指针数组中
- 用于将对象添加到指针数组的函数
- 为什么我的代码没有将每个对象添加到向量中
- 是 std::make_pair 在将对象添加到地图时创建副本
- 当我将对象添加到向量时,为什么typeID名称会更改