I'm trying to call a member function inside a class but am having no luck. I need to find the length of the array. When I posted this code before there were some questions on the member functions. The reason those functions are private is that I don't want the user to be able to have direct access to them. There error is at line 323. Please, what am I doing wrong.

#include <iostream>
using std::istream;
using std::ostream;
using namespace std;
 
class SString
{
	private:
		char *_string;
		//recursive functions
		int str_length(const char str[]);
		int str_compare(const char *str1, const char *str2);
		void str_reverse(char str[] , int length);
		bool str_replace_first(char str[], const char find[], const char replace[]);

		//iterative functions
		int length_iter(const char str[]);
		int str_compare_iter(char str1[], const char str2[]);
		void str_reverse_iter(char str[], int length);
		bool str_replace_first_iter(char str[], const char find[], const char replace[]);

	public:
		//Constructors
		SString();
		SString(char *array, int size);
		//Deconstructor
		~SString();

		int str_length();
		int str_compare();
		void str_reverse();
		bool str_replace_first();

		int length_iter();
		int str_compare_iter();
		void str_reverse_iter();
		bool str_replace_first_iter();

		//Overloaded Operators
		void operator+(char &rstring);
		friend std::istream &operator >>(std::istream &is, SString &string);
		friend std::ostream &operator <<(std::ostream &os, SString &string);
};		
SString::SString()
{
	_string = new char[100];
	_string[0] = '\0';
}

SString::SString(char *array, int size)
    {
        int i;
	    _string = new char[size+1]; // +1 for the string's null terminator character
	    for(i=0; i < size; i++)
	    {
            _string[i] = array[i];
	    }
        _string[i] = 0; // add string's null terminating character
    }
//Deconstructor
SString::~SString()
{
	delete [] _string;
}
/**
 * This function finds the length of a c-string using recursion
 * @param str[ ] a c-string variable that stores a string from user
 * @pre  in main command = "length", str[0] is not an empty string-'\0'
 * @post if str[0]='\0' then 0 is returned or str_length is called again but at the next index of str[]
 * @return the length of the c-string str[]
 */
int SString::str_length(const char str[])
{
	//base case
	if (str[0] == '\0')
		return 0;
	//recursive statement
	else
		return 1 + str_length(str+1);
}

/**
 * This function finds the length of a c-string using iteration
 * @param str[ ] a c-string variable that stores a string from user
 * @pre in main command = "length_iter", str[ ] is not an empty string-'\0'
 * @post i is < 0
 * @return the length of the c-string str[]
 */
int SString::length_iter(const char str[])
{
	int i=0;
	while(str[i] != '\0')
	{
		i++;
	}
	return i;
}

/**
 * This recursive function compares 2 c-strings to see if they are equal or there is a character mismatch
 * @param *str1 is a pointer to charArray[ ]
 * @param *str2 is a pointer to scharArray2[ ]
 * @pre in main command = "compare", str1[ ] and str2[ ] is not an empty string-'\0'
 * @post str1[] and str2[ ] are equal or *str1 and *str2 point to different characters
 * @return a integer vaule that is the distance between the ASCII character vaules.  If *str1 points to
a smaller character, the vaule is negative.  If *str2 points to the a smaller character the value is positive.
If both *str1 and *str2 do not point to a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned 
 */	
int SString::str_compare(const char *str1, const char *str2)
{
    if (*str1 != '\0' || *str2 != '\0')
    {
        if (*str1 != *str2)
        {
            if (*str1 > *str2)
            {
                return *str1 - *str2;
            }
            else if (*str1 < *str2)
            {
                return *str1 - *str2;
            }
        }
        
    }
    else return 0; //C-strings are equal-base case
    //recursive statement
    return str_compare(str1 + 1, str2 + 1);
}	

/**
 * This iterative function compares 2 c-strings to see if they are equal or there is a character mismatch
 * @param str1[ ] is a c-string variable that stores a string from user
 * @param str2[ ] is a c-string variable that stores a string from user
 * @pre in main command = "compare_iter", str1[ ] and str2[ ]  are not an empty string-'\0'
 * @post str1[] and str2[ ] are equal or str1[i]  and str2[i]  contain different characters
 * @return a integer vaule(diff) that is the distance between the ASCII character vaules.  If str1[i] contains
a smaller character, the vaule(diff) is negative.  If str2[i] contains the a smaller character the value(diff) is positive.
If both str1[i]  and str2[i]  do not contain a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned (diff)
*/
int SString::str_compare_iter(char str1[], const char str2[])
{
	for( ; *str1=*str2; *str1++, str2++)
	{
	     if(*str1 == 0)
            return 0;
	}
   
	return int(*str1-*str2); 
}

/**
 * This recursive function will reverse the c-string str
 * @param *str is a pointer to charArray[ ]
 * @param length is the length of charArray[ ]
 * @pre in main command = "reverse", *str does notpoint to an empty string-'\0'
 * @post the string in charArray[ ]  is reversed
 * @return no return value
 */
void SString::str_reverse(char *str, int length)
{
	
	char temp, temp1;
    	
    if (length >= 1)
    {
         // swap first & last chars    
         temp = str[0];
         str[0] = str[length-1];
         str[length-1] = temp; 
         // recursive call--
         str_reverse(&str[1], length-2);
    }
}    

/**
 * * This recursive function will reverse the c-string str
 * @param str[ ] str1[ ] is a c-string variable that stores a string from charArray[ ]
 * @param length is the length of charArray[ ]
 * @pre in main command = "reverse_iter", str[ ]  does notpoint to an empty string-'\0'
 * @post the string in charArray[ ]  is reversed
 * @return no return value
 */	
void SString::str_reverse_iter(char str[], int length)
{
	char temp;
	int starti, endi, mid;//starting and ending index
	starti = 0;
	endi = length - 1;
	mid = length / 2;
	
	while(starti < mid)
	{
		temp = str[starti];
		str[starti] = str[endi-starti];
		str[endi-starti] = temp;
		starti++;
	}
}

/**
 * This recursive function will search a c-string for a character(s) to be replaced with other character(s)
 * @param *str points to charArray[ ] the array to be searched
 * @param *find points to charArray2[ ], contains what to search for
 * @param*replace points to charArray3[ ] what to replace the characters with
 * @param is_partial_match  is true if part of the find string is found in the original string
 * @pre  in main command = "replace_first", *str[ ] , *find, *replace does nont point to an empty string-'\0'
 * @post Either the string was found and replaced in the array or *find and *replace were not equal in size thus the array was not searched
 * @return false condition if *find and *replace were not equal in size thus the array was not searched or
 true condition if *find and *replace were equal in size thus the array was searched and replaced with the contents *replace points to
 */
bool SString::str_replace_first(char *str, const char *find, const char *replace)
{
	if(length_iter(find) != length_iter(replace))
	{
		cout << "ERROR: The find and replace strings are of different length." << endl;
		cout << "Please change the find and replace strings to the same length." << endl;
		return false;
	}
	
	while(*str != '\0')
    {        
            
    if(*str == *find)
		{
			*str = *replace;
			*replace++;
			*find++;
        }

	else	str_replace_first(str+1, find, replace);
}
    return true;
}

/**
 * This iteraive function will search a c-string for a character(s) to be replaced with other character(s)
 * @param str points to charArray[ ] the array to be searched
 * @param find points to charArray2[ ], contains what to search for
 * @param replace points to charArray3[ ] what to replace the characters with
  * @param is_partial_match  is true if part of the find string is found in the original string
 * @pre  in main command = "replace_first_iter", str , find, replace does nont point to an empty string-'\0'
 * @post Either the string was found and replaced in the array or find and replace were not equal in size thus the array was not searched
 * @return false condition if find and replace were not equal in size thus the array was not searched or
 true condition if find and replace were equal in size thus the array was searched and replaced with the contents replace points to
 */
bool SString::str_replace_first_iter(char str[], const char find[], const char replace[])
{
	if(length_iter(find) != length_iter(replace))
	{
		cout << "ERROR: The find and replace strings are of different length." << endl;
		cout << "Please change the find and replace strings to the same length." << endl;
		return false;
	}
	int i, j, k, m, n, delta;
    int n1, n2, n3;
    char* p, *q;
    if (!str || !find)
        return 0;
    if (!replace)
        replace = "";
    n1 = str_length(str);
    n2 = str_length(find);
    n = n1 - n2 + 1;
    for (i = 0; i < n; ++i)
    {
        for (j = 0; j < n2 && find[j] == str[i+j]; ++j)
            ;
        if (j == n2) // found
        {
            n3 = str_length(replace);
            delta = n3 - n2;
            m = n1 - i - n2;
            if (delta < 0) /* move left */
            {
                p = str + (i + n2 + delta);
                q = p - delta;
                for (k = 0; k <= m; ++k)
                    p[k] = q[k];
            }
            else if (delta > 0) /* move right */
            {
                q = str + n1 - m;
                p = q + delta;
                for (k = m; k >= 0; --k)
                    p[k] = q[k];
            }
            for (k = 0; k < n3; ++k)
                str[i+k] = replace[k];
            return str + i + n3;
        }
    }
    return 0;
}

int SString::str_length(){
	return str_length(_string);}

int SString::str_compare(){
	return str_compare(_string, _string);}

void SString::str_reverse(){
	return str_reverse(_string, str_length(_string));}

bool SString::str_replace_first(){
	return str_replace_first(_string, _string, _string);}

int SString::length_iter(){
	return length_iter(_string);}

int SString::str_compare_iter(){
	return str_compare_iter(_string, _string);}

void SString::str_reverse_iter(){
	return str_reverse_iter(_string, str_length(_string));}

bool SString::str_replace_first_iter(){
	return str_replace_first_iter(_string, _string, _string);}

void SString::operator+(char &rstring)
{
	//string temp();
	int len = length_iter(*_string) + length_iter(*rstring);
    string temp(_string, len);
    int i;
    for (i = 0; i < _string.length_iter(); i++)
         temp[i] = _string[i];
    for (int j = 0; j < rstring.length_iter(); j++, i++)
        rstring[j] = temp[i]; //temp[i] = rstring[j];
    //rstring[len]='\0';
}

istream &operator >>(std::istream &infile, string &string)
{
	for(int i = 0; i < string.length_iter(); i++)
		infile >> string[i];
	return infile;
}

ostream &operator <<(std::ostream &outfile, string &string)
{
	for(int i = 0; i < string.length_iter(); i++)
		outfile << string[i];
	return outfile;
}

namespace SString
{
	class CD : public SString
	{
		private:
			SString artist, title, descrip;
			int year;
		
		public:
			CD();
			CD(SString artist, SString title, SString descrip, int year);
			SString get_artist(){return artist;}
			SString get_title(){return title;}
			SString get_descrip(){return descrip;}
			int get_year(){return year;}
	        void set_artist(char newArtist[]){artist = newArtist;}
	        void set_title(char newTitle[]){title = newTitle;}
	        void set_descrip(char newDescrip[]){descrip = newDescrip;}
            void set_year(int newYear){year = newYear;}
			friend std::istream& operator >>(std::istream &is, CD &CD);
};
	CD::CD()
	{
    
	}

	CD::CD(SString artist, SString title, SString descrip, int year) : artist(&artist[0], artist.length_iter()),
		title(&title[0], title.length_iter()), descrip(&descrip[0], descrip.length_iter()), year(0)
	{
    
    }

    SString get_artist()
	{
		return artist;
	}
    
	SString get_title()
	{
		return title;
	}
	
	SString get_descrip()
	{
		return descrip;
	}
	
	int CD::get_year()
	{
		return year;
	}
	
	void CD::set_artist(char newArtist[])
	{
		artist = newArtist;
	}
	void CD::set_title(char newTitle[])
	{
		title = newTitle;
	}
	
	void CD::set_descrip(char newDescrip[])
	{
		descrip = newDescrip;
	}
    
	void CD::set_year(int newYear)
	{
		year = newYear;
	}

	std::istream& operator >>(std::istream &infile, CD &CD)
	{
		infile.getline(artist, 100);
		infile.getline(album, 100);
		infile >> year;
		infile.getline(descrip, 100);
		do
		{
			infile.getline(descrip, 100);
		} while(descrip != "END");
		return is;
	}
}

int main(int argc, char *argv[])
{
	//Testing for correct number of arguments at command line
	if(argc != 3)
	{
		cout << "ERROR--Not enough arguements!!" << endl;
		cout << "The correct format is: \"./name of program\" \"input file\" \"output file\"" << endl;
		//exit(1);
	}
	
	//opening data file from user at run time
	ifstream infile;
    infile.open(argv[1]);
	
	if(infile.fail())
    {
        cout << "Failed to open input file." << endl;
        exit(1);
    }
	
	ofstream outfile;
    outfile.open(argv[2]);
	
	if(outfile.fail())
    {
        cout << "Failed to open input file." << endl;
        exit(1);
    }
	
	char command[100], charArray[100], charArray2[100], charArray3[100];
	int length, compareValue;

	while(infile >> command)
	{
		if(SString.str_compare(command, "add") == 0)
		{
			infile >> charArray;
			length = length_iter(charArray);
			outfile << length << endl;
		}
		else if(SString.str_compare(command, "get") == 0)
		{
			infile >> charArray;
			length = str_length(charArray);
			outfile << length << endl;
		}
		else if(SString.str_compare(command, "print") == 0)
		{
			infile >> charArray >> charArray2;
			compareValue = str_compare_iter(charArray, charArray2);
			outfile << compareValue << endl;
		}
	}
	outfile.close();
	infile.close();
    return 0;
}

Recommended Answers

All 8 Replies

line 323: According to line 17 length_iter() requires an array of characters. passijng *_string only passes a sincle character, not the entire array. And rstring is not an array of characters, but a reference to a single character.

I think what you want is this:

void SString::operator+(const char* rstring)
{
	//string temp();
	int len = length_iter(_string) + length_iter(rstring);

<snip>
}

Or you might also want this overloaded function

void SString::operator+(SString& rstring)
{
	//string temp();
	int len = length_iter(_string) + length_iter(rstring._string);

<snip>
}

When I try it the first way, I get a compile error that "invalid conversion from `char' to `const char*'" The second way I get "_string is not a type".

you also have to change line 40 to be the correct parameter type.

Ok I got it now. Thank you. One more question if I could. I'm getting "error: no match for 'operator[]' in 'temp'". Can I not compare the char array like I did?

Your program contains millions (maybe even billions) of other errors. What line number are you talking about.

338-341

Never mind. I figured out what was wrong. Thanks!

line 338: >> ostream &operator <<(std::ostream &outfile, string &string)

Shouldn't that second parameter be SString& string instead of string &string ? string is the name of std::string, and you can't use that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.