help with overloading relational operators

#ifndef PFASTRING_HPP
#define PFASTRING_HPP

#include <string>
#include <vector>

class PFAString {
public:
// Default constructor
PFAString();
// Constructor that sets capacity
PFAString(int capValue);
// Constructor that sets capacity and initializes all elements to a specific string
PFAString(int capValue,PFAStringObject);
// Copy Constructor
PFAString(const PFAString& PFAStringObject);
// Destructor
~PFAString();

int get_capacity()const;
int get_size() const;
void push_back(); // Places a string at the back of the array
void pop_back(); // Destroys the string at the back of the array
void resize(); // Grows or shrinks array accordingly
void empty_array(); // Sets the size to zero

PFAString& operator [](int index); // Needs to work as l-value
PFAString& operator = (const PFAString& rSide) ; // MUST work as l-value

//int length() const { return front – end; }
//char& operator[](int i) { return front[i]; } // will work as left_value
//const char& operator[](int i) const { return front[i]; } //// will work as left_value page 366.

friend bool operator == (const PFAString& lSide,const PFAString& rSide);
friend bool operator < (const PFAString& lSide,const PFAString& rSide);
friend bool operator > (const PFAString& lSide,const PFAString& rSide);
friend bool operator <= (const PFAString& lSide,const PFAString& rSide);
friend bool operator >= (const PFAString& lSide,const PFAString& rSide);

private:
std::string *arr; // Dynamic array for string .
int capacity; // Capacity and size are two different things
int size;
Last edited on
[code][/code#include <string> // for the use of the string class
#include <vector> // For the use of the vector class.
#include <iostream> // std::cout, std::boolalpha
#include <algorithm> // std::lexicographical_compare
#include <cctype> // std::tolower


#include "PFAString.hpp"

using namespace std;


// Default constructor
PFAString::PFAString()
{

}
// Constructor that sets capacity
PFAString::PFAString(int capValue)
{
arr = new string[capacity];
}
// Constructor that sets capacity and initializes all elements to a specific string
PFAString::PFAString(int capValue, PFAStringObject)
{
arr = new string[capacity];
capacity = 0;
size = 0;
//vector < PFAString *> arr;
//std::vector< std::string> myString;// Initializing empty vector of string
}
// Copy Constructor
PFAString::PFAString(const PFAString& PFAStringObject)
{
capacity = arr.get_capacity();
size = arr.get_size();
arr = new string[capacity];
for ( int i = 0; i < size; i++)
arr[i] = myString.arr[i];
}
// Destructor
PFAString::~PFAString()
{
delete [] arr ;
}
// Accessor member functions:
int PFAString::get_capacity() const
{
return capacity;
}
int PFAString::get_size() const
{
return size;
}
// Places a string at the back of the array
void PFAString::push_back()
{
vector < PFAString *> arr;
for (int i= 0; i < arr.get_size();i++)
arr.push_back();
}
// Destroys the string at the back of the array
void PFAString::pop_back()
{
vector < PFAString *> arr;
for (int i= 0; i <= arr.get_size();i++)
arr.pop_back();
}
// Grows or shrinks array accordingly
void PFAString::resize()
{
vector < PFAString *> arr;
for (int i= 0; i < arr.get_size();i++)
arr.resize();
}
// Sets the size to zero
void PFAString::empty_array()
{
size = 0 ;
}
// Needs to work as l-value
PFAString& PFAString::operator[] (int index)
{
if (index >= size)
{
cout << "Illegal index in PFAString.\n";
exit(0);
}
return arr[index];
}
// MUST work as l-value
PFAString& PFAString:: operator = (const PFAString& rSide)
{
//if the right side is the same as the left side :
if (this == &rSide)
{
return *this;
}
else
{
capacity = rSide.get_capacity();
size = rSide.get_size() ;
delete [] arr ;
arr = new string[capacity];
for (int i = 0; i < size; i++)
{
arr = rSide.arr[i];
return *this;
}
}
}
// Overloading the relational operators using string::compare():
bool operator == (const PFAString& lSide,const PFAString& rSide)
{

if (!(lSide<rSide) && !(rSide<lSide))
{
return true;
}else
{
return false;
}
// need to iterate the array and make the comparason

//return strcmp(lSide, rSide) == 0;
}

bool operator < (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) < 0;
}
bool operator > (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) > 0;
}
bool operator <= (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) <= 0;
}
bool operator >= (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) >= 0;
}

//bool mycomp (char c1, char c2)
//{ return std::tolower(c1)<std::tolower(c2); }




Please put your code in code blocks. Like this:
[code_] - At the beginning of your code
[/code_] - At the end of your code

P.S. Don't include the underscores.
Last edited on
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
#ifndef PFASTRING_HPP
#define PFASTRING_HPP

#include <string>
#include <vector>

class PFAString {
public:
    // Default constructor
    PFAString();
    // Constructor that sets capacity
    PFAString(int capValue);
    // Constructor that sets capacity and initializes all elements to a specific string
    PFAString(int capValue,std::string e);
    // Copy Constructor
    PFAString(const PFAString& PFAStringObject);
    // Destructor
    ~PFAString();
    
	int get_capacity()const;
    int get_size() const;
	void push_back(std::string e);  // Places a string at the back of the array
    void pop_back();   // Destroys the string at the back of the array
    void resize(size_t newSize);     // Grows or shrinks array accordingly
    void empty_array();        // Sets the size to zero
    bool full () const {return capacity == size;}// return true if the array is full but otherwise. 
    
    PFAString& operator [](int index);    // Needs to work as l-value
    //const PFAString& operator[](int index) const { return front[index]; } //// will work as r_value page 366.
    PFAString& operator = (const PFAString& rSide) ;    // MUST work as l-value
    
    //int length() const { return front – end; }
    //char& operator[](int i) { return front[i]; } // will work as left_value
    //const char& operator[](int i) const { return front[i]; } //// will work as left_value page 366.
    
    friend bool operator == (const PFAString& lSide,const PFAString& rSide);
    friend bool operator < (const PFAString& lSide,const PFAString& rSide);
    friend bool operator > (const PFAString& lSide,const PFAString& rSide);
    friend bool operator <= (const PFAString& lSide,const PFAString& rSide);
    friend bool operator >= (const PFAString& lSide,const PFAString& rSide);
    
private:
    std::string *arr;   // Dynamic array for string .
    int capacity;       // Capacity and size are two different things
    int size;			//size of the array.
    int index;  		// position of element in the array
};

#endif 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
 
#include <string>    // for the use of the string class
#include <vector>    // For the use of the vector class.
#include <iostream>     // std::cout, std::boolalpha
#include <algorithm>    // std::lexicographical_compare
#include <cctype>       // std::tolower


#include "PFAString.hpp"

using namespace std;
 

    // Default constructor
    PFAString::PFAString()
    {
  		capacity = 0;
  		size = 0; 
  		index = 0;	
  		arr = new string[capacity];
    }
    
    // Constructor that sets capacity
    PFAString::PFAString(int capValue)
    {
    	capacity = 0;
  		size = 0;
  		index = 0;
    	if(capValue < 0)
    	{
    		cout << "Forbidden:negative value as capacity.Resetting back to 0"<< endl;
    		capValue = 0;
    	}
    	capacity = capValue;
    	arr = new string[capacity]; 	
    }
    
    // Constructor that sets capacity and initializes all elements to a specific string
    PFAString::PFAString(int capValue,std:: string e)
    {
    	std::string *arr;
    	capacity = 	capValue;
    	//size = e.get_size();
    	arr = new string[capacity];
    	for ( int i = 0; i < size; i++)
    	arr[i] = e.arr[i];
    } 
     
    // Copy Constructor
    PFAString::PFAString(const PFAString& PFAStringObject)
    {
    	std::string *arr;
    	capacity = 	PFAStringObject.get_capacity();
    	size = PFAStringObject.get_size();
    	arr = new string[capacity];
    	for ( int i = 0; i < size; i++)
    	arr[i] = PFAStringObject.arr[i];
    }
    
    // Destructor
    PFAString::~PFAString()
    {
    	delete [] arr ;	
    }
    
    // Accessor member functions:
    int PFAString::get_capacity() const
    {
    	return capacity;
    }
    
    int PFAString::get_size() const
    {
    	return size;
    }
    
    // Places a string at the back of the array
    void PFAString::push_back(string e)  
    {
    	std::string *arrNew;
    	if ( index == size)
    		{
    			cout<< "Doubling to :"<< size * 2<< endl;
    			size = size * 2;
    			arrNew = new string [size];
    			for ( int i =0; i < index; i++)
    			arrNew[i] = arr[i];
    			delete arr[];
    			arr = arrNew;
    		}
    			arr[index ++] = e ;	
    }
    // Destroys the string at the back of the array
    void PFAString::pop_back()   
    {
    	std::string  *arrNew;
    	if( index < size / 4)
    		{
    			cout << " Reducing to :" << size / 2 << endl;
    			size = size / 2;
    			arrNew = new string [size];
    			for (int i = 0; i < index; i++)
    			arrNew[i] = arr[i];
    			delete arr[];
    			arr = arrNew;	
    		}
    	index --;		
    }
    // Grows or shrinks array accordingly
     void PFAString::resize(size_t newSize)     
    {
    	//std::string *arrNew;
    	arrNew = new string [size ];
    	if ( index == size - 1)
    	{
    		std::string *arr = (std::string *) realloc(arr,newSize);// maybe I should use std::copy() to copy the memory
    		/* alternative solution with using loop.
    			std::string *arr = new string [size];
    			void resizing()
    			{
    				std::string *arrNew = new string [size + 1];
    				for( int = 0 ; i< size ; i++)
    				{
    					arrNew[i] = arr[i];
    					size++;
    					arr = arrNew;
    					delete[] arrNew;	   
    				}
    			}	
    		*/
    	}
    		
    }
     // Sets the size to zero
    void PFAString::empty_array()        
    {
    	size = 0 ;
    }
    // Needs to work as l-value
    PFAString& PFAString::operator[] (int index)
    {
    	std::string  *arrNew; // pointer for new array.
    	if (index >= size)
    	{
        	cout << "Illegal index in PFAString.\n";
        	cout<<" reallocating a bigger array"<< endl;
        	arrNew = new string [size + 10];
        	for(int i = 0 ; i < index ; i++)
        		arrNew[i] = arr[i];       // copying old value
        		for(int j = index ; j < index + 10 ;j++)  // initializing the remainder...
        				arrNew[i] = 0;
        				size = index + 10 ;   // increasing size.
        				delete [] arr ; 		//	deleting the old array.
        				arr = arrNew ;			// reassigning the new array.	
    	}
    	if ( index > size)
    	size = index + 1;			// increment size by 1.
    	return * (arr.size +index );		//returning a reference to the element
    }
    // MUST work as l-value   
    PFAString& PFAString:: operator = (const PFAString& rSide)
    {
    	//if the right side is the same as the left side :
    	if (this == &rSide)
    	{
    		return *this;
    	}
    	else
    	{
    		capacity = rSide.get_capacity();
    		size = rSide.get_size() ;
    		delete [] arr ;
    		arr = new string[capacity];
    		for (int i = 0; i < size; i++)
    			{
    				arr = rSide.arr[i];
    				return *this;
    			}
    	}
    } 
     // Overloading the relational operators  using string::compare():
    bool operator == (const PFAString& lSide,const PFAString& rSide)
    {
    	std::string *arr;
    	arr = new string[size];
    	
				if (!(lSide.get_size() < rSide.get_size()) && !(rSide.get_size() < lSide.get_size()))
				{	
					for (int i = 0; i < arr.get_size(); i++)
					{
    					if(lSide.arr[i]== rSide.arr[i])
    					{
    						cout<< " lside  is equal to the rSide"<< endl;
    					}
    				}return true;
    						
    		}else 
    			
    		{
    			return false;
    		}
    }
    
    bool operator < (const PFAString& lSide,const PFAString& rSide)
    {
    	std::string *arr;
    	arr = new string[size];
    	
				if ((lSide.get_size() < rSide.get_size()) && (rSide.get_size() > lSide.get_size()))
				{	
					for (int i = 0; i < arr.get_size(); i++)
					{
    					if(lSide.arr[i] < rSide.arr[i])
    					{
    						cout<< " lside  is lower than the rSide"<< endl;
    					}
    				}
    				return true;				
    			}else 	
    		{
    			return false;
    		}
    }
    bool operator > (const PFAString& lSide,const PFAString& rSide)
    {
    	std::string *arr;
    	arr = new string[size];
    	
				if ((lSide.size > rSide.size) && (rSide.size < lSide.size))
				{	
					for (int i = 0; i < size; i++)
					{
    					if(lSide.arr[i] > rSide.arr[i])
    					{
    						cout<< " lside  is greater than the rSide"<< endl;
    					}
    				}
    				return true;				
    			}else 	
    		{
    			return false;
    		}
    }
    bool operator <= (const PFAString& lSide,const PFAString& rSide)
    {
    	std::string *arr;
    	arr = new string[size];
    	
				if ((lSide.size < rSide.size) && (rSide.size > lSide.size))
				{	
					for (int i = 0; i < size; i++)
					{
    					if((lSide.arr[i] < rSide.arr[i]) ||(lSide.arr[i]== rSide.arr[i]))
    					{
    						cout<< " lside  is lower or equal than the rSide"<< endl;
    					}
    				}
    				return true;				
    			}else 	
    		{
    			return false;
    		}
    }
    bool operator >= (const PFAString& lSide,const PFAString& rSide)
    {
    	std::string *arr;
    	arr = new string[size];
    	
				if ((lSide.get_size() > rSide.get_size()) && (rSide.get_size() < lSide.get_size()))
				{	
					for (int i = 0; i < arr.get_size(); i++)
					{
    					if((lSide.arr[i] > rSide.arr[i]) ||(lSide.arr[i]== rSide.arr[i]))
    					{
    						cout<< " lside  is graeter or equal than the rSide"<< endl;
    					}
    				}
    				return true;				
    			}else 	
    		{
    			return false;
    		}
    }
Thank you very much for helping out.I have to turn this assignment by tomorrow.

So your help will be greatly appreciated.
Thanks.
I'm sorry, but what exactly was/is your question?
I have problem overloading the operator [] and the relational operators in my .hpp file.Any help will be appreciated.
Here the compiler error that I get.And I do not understand why it complaining for the poster array arr.


PFAString.cpp:47:17: error: no member named 'arr' in
      'std::__1::basic_string<char>'
        arr[i] = e.arr[i];
                 ~ ^
PFAString.cpp:89:19: error: expected expression
                        delete arr[];
                                   ^
PFAString.cpp:105:19: error: expected expression
                        delete arr[];
                                   ^
PFAString.cpp:114:6: error: use of undeclared identifier 'arrNew'
        arrNew = new string [size ];
        ^
PFAString.cpp:152:20: error: use of undeclared identifier 'i'
                                        arrNew[i] = 0;
                                               ^
PFAString.cpp:159:19: error: member reference type 'std::string *' (aka
      'basic_string<char, char_traits<char>, allocator<char> > *') is a pointer;
      did you mean to use '->'?
        return * (arr.size +index );            //returning a reference ...
                  ~~~^
                     ->
PFAString.cpp:159:20: error: reference to non-static member function must be
      called; did you mean to call it with no arguments?
        return * (arr.size +index );            //returning a reference ...
                  ~~~~^~~~
                          ()
PFAString.cpp:159:13: error: indirection requires pointer operand
      ('unsigned long' invalid)
        return * (arr.size +index );            //returning a reference ...
               ^ ~~~~~~~~~~~~~~~~~~
PFAString.cpp:177:15: error: assigning to 'std::string *' (aka
      'basic_string<char, char_traits<char>, allocator<char> > *') from
      incompatible type 'std::string' (aka 'basic_string<char,
      char_traits<char>, allocator<char> >'); take the address with &
                                arr = rSide.arr[i];
                                      ^~~~~~~~~~~~
                                      &
PFAString.cpp:186:23: error: use of undeclared identifier 'size'
        arr = new string[size];
                         ^
PFAString.cpp:190:29: error: member reference type 'std::string *' (aka
      'basic_string<char, char_traits<char>, allocator<char> > *') is a pointer;
      did you mean to use '->'?
                                        for (int i = 0; i < arr.get_size(); i++)
                                                            ~~~^
                                                               ->
PFAString.cpp:190:30: error: no member named 'get_size' in
      'std::__1::basic_string<char>'
                                        for (int i = 0; i < arr.get_size(); i++)
                                                            ~~~ ^
PFAString.cpp:208:23: error: use of undeclared identifier 'size'
        arr = new string[size];
                         ^
PFAString.cpp:212:29: error: member reference type 'std::string *' (aka
      'basic_string<char, char_traits<char>, allocator<char> > *') is a pointer;
      did you mean to use '->'?
                                        for (int i = 0; i < arr.get_size(); i++)
                                                            ~~~^
                                                               ->
PFAString.cpp:212:30: error: no member named 'get_size' in
      'std::__1::basic_string<char>'
                                        for (int i = 0; i < arr.get_size(); i++)
                                                            ~~~ ^
PFAString.cpp:228:23: error: use of undeclared identifier 'size'
        arr = new string[size];
                         ^
PFAString.cpp:232:26: error: use of undeclared identifier 'size'
                                        for (int i = 0; i < size; i++)
                                                            ^
PFAString.cpp:248:23: error: use of undeclared identifier 'size'
        arr = new string[size];
                         ^
PFAString.cpp:252:26: error: use of undeclared identifier 'size'
                                        for (int i = 0; i < size; i++)
                                                            ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
P$ 
Looking specifically at line 43 of the header file, if you are doing dynamic array handling for your string, you want to use char* arr; The std::string class already does the dynamic handling for you. If you are trying to write your own string class that mimics what std::string does, then use char* arr; If you are just writing a wrapper class around the std::string class, just declare std::string arr and don't do dynamic allocation.

When you figure that out...line 41 of the src file declares a local variable arr that hides the member variable. In line 47, you can access the individual characters of a std::string with e[i].

If you want to delete an array of things, use delete[] arr; If you end up with a std::string data member, you don't need to delete.

I don't have time to go over all of the errors here. You need to get the char* vs. std::string thing figured out first.

Last edited on
Ok thanks you very much.I will try it and see how it works.
Topic archived. No new replies allowed.