C++ 试图通过运算符+在不同对象中添加2个字符数组;超载
我似乎很难让这个加法操作符重载为我的对象工作。我搜遍了网,不太明白我做错了什么。基本上,它应该重载以连接2个字符数组。我尝试的每件事都会导致崩溃或字符的随机混乱 我对操作符重载还不熟悉,所以可能我用错了。 其名称为:C++ 试图通过运算符+在不同对象中添加2个字符数组;超载,c++,arrays,operator-overloading,C++,Arrays,Operator Overloading,我似乎很难让这个加法操作符重载为我的对象工作。我搜遍了网,不太明白我做错了什么。基本上,它应该重载以连接2个字符数组。我尝试的每件事都会导致崩溃或字符的随机混乱 我对操作符重载还不熟悉,所以可能我用错了。 其名称为: String myString1, myString2; std:: cout << "\nYour strings added are: " << myString1 + myString2; 以下是相关代码: String String::opera
String myString1, myString2;
std:: cout << "\nYour strings added are: " << myString1 + myString2;
以下是相关代码:
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;
}
字符串::运算符+(字符串rhs){
字符串结果;
int i=长度;
对于(int x=0;x std::cout您的问题比串联问题更多,我怀疑其他所有问题是否“完美工作”
默认构造函数不会初始化任何内容。
甚至String s;std::你能有一个自定义的复制构造函数吗?1.result.ch
是否足够大以得到结果?2.你不是零终止结果。3.你从未将result.length
设置为任何合理的值。4.如果result.ch
是动态分配的,你可能会有更多的问题。谢谢,我还是新手。ch[]默认值为256,至于其他3个…假设我最好查看一下。
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;
}
#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; };
// 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;
}