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.
[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
#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.
friendbooloperator == (const PFAString& lSide,const PFAString& rSide);
friendbooloperator < (const PFAString& lSide,const PFAString& rSide);
friendbooloperator > (const PFAString& lSide,const PFAString& rSide);
friendbooloperator <= (const PFAString& lSide,const PFAString& rSide);
friendbooloperator >= (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
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.