String I/O overloading

So for my class assignment, we were tasked with making a string class and I'm having problems overloading the input and output operators. Whenever I try to read information from a file, I get an std::bad_alloc and segfault and I don't know why or how to fix it. Could you help please?

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #ifndef CS23001_STRING_INTERFACE_HPP
#define CS23001_STRING_INTERFACE_HPP
#include <iostream>
#include <vector>

class String {
private:
	// helper constructors and methods
	String(int);
	String(int, const char *);
	void reset_capacity(int);

	char * str;

	// size includes NULL terminator
	int string_size;

public:

	// constructor: empty string, String('x'), and String("abcd")
	String();
	String(char);
	String(const char *);

	// copy ctor, destructor, constant time swap, and assignment
	String(const String &);
	~String();
	void     swap(String &);
	String & operator=     (String);

	// subscript: accessor/modifier and accessor
char & operator[](int);
char   operator[](int) const;

// max chars that can be stored (not including null terminator)
int capacity()      const;
// number of char in string
int length()      const;

// concatenation
String   operator+ (const String &) const;
String & operator+=(String);

// relational methods
bool operator==(const String &)  const;
bool operator< (const String &)  const;

// i/o
friend std::ostream& operator<<(std::ostream &, const String &);
friend std::istream& operator>>(std::istream &, String &);

String substr(int, int) const;
int find(char, int) const;
int find(const String & , int) const;
std::vector<String> split(char) const;
};

// free functios for concatenation and relational
String operator+       (const char *, const String &);
String operator+       (char, const String &);
bool   operator==      (const char *, const String &);
bool   operator==      (char, const String &);
bool   operator<       (const char *, const String &);
bool   operator<       (char, const String &);
bool   operator<=      (const String &, const String &);
bool   operator!=      (const String &, const String &);
bool   operator>=      (const String &, const String &);
bool   operator>       (const String &, const String &);
int                     toInt(const String &);
int                     Pow(int, int);
#endif 


Input/Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String::String(int size, const char * word) 
{
	int i = 0;
	str = new char[size];
	while (word[i] != '\0')
	{
		str[i] = word[i];
		i++;
	}
	i++;
	string_size = i;
}

std::ostream& operator<<(std::ostream & out, const String & rhs)
{
	out << rhs.str;
	return out;
}

std::istream& operator>>(std::istream &in, String &rhs)
{
	char hold[1024];
	in >> hold;
	rhs.str = hold;
	rhs.string_size = rhs.length() + 1;
	return in;
}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "String.hpp"
#include <fstream>

int main()
{
std::ifstream InputFile;
InputFile.open("./tiny.txt");
if (!InputFile.is_open())
{
	std::cout << "A file was not open";
}
String x;
while(InpiutFile >> x)
{
std::cout << x;
}
}
hold is a local variable so it will no longer exist when the function has ended so rhs.str ends up pointing to some invalid data. You need allocate the array with new the same way as you do in the constructor.
@Peter87 Thank you!!! Fixed it and it works fine now
Topic archived. No new replies allowed.