我似乎很难让这个加法运算符重载为我的对象工作。我已经在网上搜索过,并且无法弄清楚我做错了什么。基本上它应该重载以连接2个char数组。我尝试的所有内容都会导致崩溃或随机混乱的角色。
我是操作员重载的新手,所以也许我错误地使用了它。 它叫:
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] != '\0'; ++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] = '\0';
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] != '\0'; ++length)
{
s[length] = rhs.s[length];
}
s[length] = '\0';
}
// 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 != '\0')
{
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] != '\0'; ++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] != '\0'; z++)
{
std::cout << result.ch[z];
}
return result;
}
答案 0 :(得分:0)
你遇到的问题多于连接问题,我怀疑其他所有事情都能很好地完成#34;。
您的默认构造函数不会初始化任何内容
即使String s; std::cout << s;
也不起作用,String s; std::cin >> s;
也不应该。{/ p>
您没有转让操作员
{String s1; String s2; s2 = s1;}
很可能会崩溃。
您既有指针s
又有数组ch
您将数据存储在s
中,但有些代码使用ch
代替
你需要下定决心。
(并且不要像对成员那样使用与参数和局部变量相同的名称。)
您同时拥有strLength
和length
,并且随机使用它们。
您的代码中有更多未定义的行为,而不是任何人可能不愿意列出 你最好的道路可能是抛弃代码并重新开始。