Calling a member function inside a class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Calling a member function inside a class

 
0
  #1
Sep 15th, 2008
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.

  1. #include <iostream>
  2. using std::istream;
  3. using std::ostream;
  4. using namespace std;
  5.  
  6. class SString
  7. {
  8. private:
  9. char *_string;
  10. //recursive functions
  11. int str_length(const char str[]);
  12. int str_compare(const char *str1, const char *str2);
  13. void str_reverse(char str[] , int length);
  14. bool str_replace_first(char str[], const char find[], const char replace[]);
  15.  
  16. //iterative functions
  17. int length_iter(const char str[]);
  18. int str_compare_iter(char str1[], const char str2[]);
  19. void str_reverse_iter(char str[], int length);
  20. bool str_replace_first_iter(char str[], const char find[], const char replace[]);
  21.  
  22. public:
  23. //Constructors
  24. SString();
  25. SString(char *array, int size);
  26. //Deconstructor
  27. ~SString();
  28.  
  29. int str_length();
  30. int str_compare();
  31. void str_reverse();
  32. bool str_replace_first();
  33.  
  34. int length_iter();
  35. int str_compare_iter();
  36. void str_reverse_iter();
  37. bool str_replace_first_iter();
  38.  
  39. //Overloaded Operators
  40. void operator+(char &rstring);
  41. friend std::istream &operator >>(std::istream &is, SString &string);
  42. friend std::ostream &operator <<(std::ostream &os, SString &string);
  43. };
  44. SString::SString()
  45. {
  46. _string = new char[100];
  47. _string[0] = '\0';
  48. }
  49.  
  50. SString::SString(char *array, int size)
  51. {
  52. int i;
  53. _string = new char[size+1]; // +1 for the string's null terminator character
  54. for(i=0; i < size; i++)
  55. {
  56. _string[i] = array[i];
  57. }
  58. _string[i] = 0; // add string's null terminating character
  59. }
  60. //Deconstructor
  61. SString::~SString()
  62. {
  63. delete [] _string;
  64. }
  65. /**
  66.  * This function finds the length of a c-string using recursion
  67.  * @param str[ ] a c-string variable that stores a string from user
  68.  * @pre in main command = "length", str[0] is not an empty string-'\0'
  69.  * @post if str[0]='\0' then 0 is returned or str_length is called again but at the next index of str[]
  70.  * @return the length of the c-string str[]
  71.  */
  72. int SString::str_length(const char str[])
  73. {
  74. //base case
  75. if (str[0] == '\0')
  76. return 0;
  77. //recursive statement
  78. else
  79. return 1 + str_length(str+1);
  80. }
  81.  
  82. /**
  83.  * This function finds the length of a c-string using iteration
  84.  * @param str[ ] a c-string variable that stores a string from user
  85.  * @pre in main command = "length_iter", str[ ] is not an empty string-'\0'
  86.  * @post i is < 0
  87.  * @return the length of the c-string str[]
  88.  */
  89. int SString::length_iter(const char str[])
  90. {
  91. int i=0;
  92. while(str[i] != '\0')
  93. {
  94. i++;
  95. }
  96. return i;
  97. }
  98.  
  99. /**
  100.  * This recursive function compares 2 c-strings to see if they are equal or there is a character mismatch
  101.  * @param *str1 is a pointer to charArray[ ]
  102.  * @param *str2 is a pointer to scharArray2[ ]
  103.  * @pre in main command = "compare", str1[ ] and str2[ ] is not an empty string-'\0'
  104.  * @post str1[] and str2[ ] are equal or *str1 and *str2 point to different characters
  105.  * @return a integer vaule that is the distance between the ASCII character vaules. If *str1 points to
  106. a smaller character, the vaule is negative. If *str2 points to the a smaller character the value is positive.
  107. If both *str1 and *str2 do not point to a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned
  108.  */
  109. int SString::str_compare(const char *str1, const char *str2)
  110. {
  111. if (*str1 != '\0' || *str2 != '\0')
  112. {
  113. if (*str1 != *str2)
  114. {
  115. if (*str1 > *str2)
  116. {
  117. return *str1 - *str2;
  118. }
  119. else if (*str1 < *str2)
  120. {
  121. return *str1 - *str2;
  122. }
  123. }
  124.  
  125. }
  126. else return 0; //C-strings are equal-base case
  127. //recursive statement
  128. return str_compare(str1 + 1, str2 + 1);
  129. }
  130.  
  131. /**
  132.  * This iterative function compares 2 c-strings to see if they are equal or there is a character mismatch
  133.  * @param str1[ ] is a c-string variable that stores a string from user
  134.  * @param str2[ ] is a c-string variable that stores a string from user
  135.  * @pre in main command = "compare_iter", str1[ ] and str2[ ] are not an empty string-'\0'
  136.  * @post str1[] and str2[ ] are equal or str1[i] and str2[i] contain different characters
  137.  * @return a integer vaule(diff) that is the distance between the ASCII character vaules. If str1[i] contains
  138. a smaller character, the vaule(diff) is negative. If str2[i] contains the a smaller character the value(diff) is positive.
  139. If both str1[i] and str2[i] do not contain a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned (diff)
  140. */
  141. int SString::str_compare_iter(char str1[], const char str2[])
  142. {
  143. for( ; *str1=*str2; *str1++, str2++)
  144. {
  145. if(*str1 == 0)
  146. return 0;
  147. }
  148.  
  149. return int(*str1-*str2);
  150. }
  151.  
  152. /**
  153.  * This recursive function will reverse the c-string str
  154.  * @param *str is a pointer to charArray[ ]
  155.  * @param length is the length of charArray[ ]
  156.  * @pre in main command = "reverse", *str does notpoint to an empty string-'\0'
  157.  * @post the string in charArray[ ] is reversed
  158.  * @return no return value
  159.  */
  160. void SString::str_reverse(char *str, int length)
  161. {
  162.  
  163. char temp, temp1;
  164.  
  165. if (length >= 1)
  166. {
  167. // swap first & last chars
  168. temp = str[0];
  169. str[0] = str[length-1];
  170. str[length-1] = temp;
  171. // recursive call--
  172. str_reverse(&str[1], length-2);
  173. }
  174. }
  175.  
  176. /**
  177.  * * This recursive function will reverse the c-string str
  178.  * @param str[ ] str1[ ] is a c-string variable that stores a string from charArray[ ]
  179.  * @param length is the length of charArray[ ]
  180.  * @pre in main command = "reverse_iter", str[ ] does notpoint to an empty string-'\0'
  181.  * @post the string in charArray[ ] is reversed
  182.  * @return no return value
  183.  */
  184. void SString::str_reverse_iter(char str[], int length)
  185. {
  186. char temp;
  187. int starti, endi, mid;//starting and ending index
  188. starti = 0;
  189. endi = length - 1;
  190. mid = length / 2;
  191.  
  192. while(starti < mid)
  193. {
  194. temp = str[starti];
  195. str[starti] = str[endi-starti];
  196. str[endi-starti] = temp;
  197. starti++;
  198. }
  199. }
  200.  
  201. /**
  202.  * This recursive function will search a c-string for a character(s) to be replaced with other character(s)
  203.  * @param *str points to charArray[ ] the array to be searched
  204.  * @param *find points to charArray2[ ], contains what to search for
  205.  * @param*replace points to charArray3[ ] what to replace the characters with
  206.  * @param is_partial_match is true if part of the find string is found in the original string
  207.  * @pre in main command = "replace_first", *str[ ] , *find, *replace does nont point to an empty string-'\0'
  208.  * @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
  209.  * @return false condition if *find and *replace were not equal in size thus the array was not searched or
  210.  true condition if *find and *replace were equal in size thus the array was searched and replaced with the contents *replace points to
  211.  */
  212. bool SString::str_replace_first(char *str, const char *find, const char *replace)
  213. {
  214. if(length_iter(find) != length_iter(replace))
  215. {
  216. cout << "ERROR: The find and replace strings are of different length." << endl;
  217. cout << "Please change the find and replace strings to the same length." << endl;
  218. return false;
  219. }
  220.  
  221. while(*str != '\0')
  222. {
  223.  
  224. if(*str == *find)
  225. {
  226. *str = *replace;
  227. *replace++;
  228. *find++;
  229. }
  230.  
  231. else str_replace_first(str+1, find, replace);
  232. }
  233. return true;
  234. }
  235.  
  236. /**
  237.  * This iteraive function will search a c-string for a character(s) to be replaced with other character(s)
  238.  * @param str points to charArray[ ] the array to be searched
  239.  * @param find points to charArray2[ ], contains what to search for
  240.  * @param replace points to charArray3[ ] what to replace the characters with
  241.   * @param is_partial_match is true if part of the find string is found in the original string
  242.  * @pre in main command = "replace_first_iter", str , find, replace does nont point to an empty string-'\0'
  243.  * @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
  244.  * @return false condition if find and replace were not equal in size thus the array was not searched or
  245.  true condition if find and replace were equal in size thus the array was searched and replaced with the contents replace points to
  246.  */
  247. bool SString::str_replace_first_iter(char str[], const char find[], const char replace[])
  248. {
  249. if(length_iter(find) != length_iter(replace))
  250. {
  251. cout << "ERROR: The find and replace strings are of different length." << endl;
  252. cout << "Please change the find and replace strings to the same length." << endl;
  253. return false;
  254. }
  255. int i, j, k, m, n, delta;
  256. int n1, n2, n3;
  257. char* p, *q;
  258. if (!str || !find)
  259. return 0;
  260. if (!replace)
  261. replace = "";
  262. n1 = str_length(str);
  263. n2 = str_length(find);
  264. n = n1 - n2 + 1;
  265. for (i = 0; i < n; ++i)
  266. {
  267. for (j = 0; j < n2 && find[j] == str[i+j]; ++j)
  268. ;
  269. if (j == n2) // found
  270. {
  271. n3 = str_length(replace);
  272. delta = n3 - n2;
  273. m = n1 - i - n2;
  274. if (delta < 0) /* move left */
  275. {
  276. p = str + (i + n2 + delta);
  277. q = p - delta;
  278. for (k = 0; k <= m; ++k)
  279. p[k] = q[k];
  280. }
  281. else if (delta > 0) /* move right */
  282. {
  283. q = str + n1 - m;
  284. p = q + delta;
  285. for (k = m; k >= 0; --k)
  286. p[k] = q[k];
  287. }
  288. for (k = 0; k < n3; ++k)
  289. str[i+k] = replace[k];
  290. return str + i + n3;
  291. }
  292. }
  293. return 0;
  294. }
  295.  
  296. int SString::str_length(){
  297. return str_length(_string);}
  298.  
  299. int SString::str_compare(){
  300. return str_compare(_string, _string);}
  301.  
  302. void SString::str_reverse(){
  303. return str_reverse(_string, str_length(_string));}
  304.  
  305. bool SString::str_replace_first(){
  306. return str_replace_first(_string, _string, _string);}
  307.  
  308. int SString::length_iter(){
  309. return length_iter(_string);}
  310.  
  311. int SString::str_compare_iter(){
  312. return str_compare_iter(_string, _string);}
  313.  
  314. void SString::str_reverse_iter(){
  315. return str_reverse_iter(_string, str_length(_string));}
  316.  
  317. bool SString::str_replace_first_iter(){
  318. return str_replace_first_iter(_string, _string, _string);}
  319.  
  320. void SString::operator+(char &rstring)
  321. {
  322. //string temp();
  323. int len = length_iter(*_string) + length_iter(*rstring);
  324. string temp(_string, len);
  325. int i;
  326. for (i = 0; i < _string.length_iter(); i++)
  327. temp[i] = _string[i];
  328. for (int j = 0; j < rstring.length_iter(); j++, i++)
  329. rstring[j] = temp[i]; //temp[i] = rstring[j];
  330. //rstring[len]='\0';
  331. }
  332.  
  333. istream &operator >>(std::istream &infile, string &string)
  334. {
  335. for(int i = 0; i < string.length_iter(); i++)
  336. infile >> string[i];
  337. return infile;
  338. }
  339.  
  340. ostream &operator <<(std::ostream &outfile, string &string)
  341. {
  342. for(int i = 0; i < string.length_iter(); i++)
  343. outfile << string[i];
  344. return outfile;
  345. }
  346.  
  347. namespace SString
  348. {
  349. class CD : public SString
  350. {
  351. private:
  352. SString artist, title, descrip;
  353. int year;
  354.  
  355. public:
  356. CD();
  357. CD(SString artist, SString title, SString descrip, int year);
  358. SString get_artist(){return artist;}
  359. SString get_title(){return title;}
  360. SString get_descrip(){return descrip;}
  361. int get_year(){return year;}
  362. void set_artist(char newArtist[]){artist = newArtist;}
  363. void set_title(char newTitle[]){title = newTitle;}
  364. void set_descrip(char newDescrip[]){descrip = newDescrip;}
  365. void set_year(int newYear){year = newYear;}
  366. friend std::istream& operator >>(std::istream &is, CD &CD);
  367. };
  368. CD::CD()
  369. {
  370.  
  371. }
  372.  
  373. CD::CD(SString artist, SString title, SString descrip, int year) : artist(&artist[0], artist.length_iter()),
  374. title(&title[0], title.length_iter()), descrip(&descrip[0], descrip.length_iter()), year(0)
  375. {
  376.  
  377. }
  378.  
  379. SString get_artist()
  380. {
  381. return artist;
  382. }
  383.  
  384. SString get_title()
  385. {
  386. return title;
  387. }
  388.  
  389. SString get_descrip()
  390. {
  391. return descrip;
  392. }
  393.  
  394. int CD::get_year()
  395. {
  396. return year;
  397. }
  398.  
  399. void CD::set_artist(char newArtist[])
  400. {
  401. artist = newArtist;
  402. }
  403. void CD::set_title(char newTitle[])
  404. {
  405. title = newTitle;
  406. }
  407.  
  408. void CD::set_descrip(char newDescrip[])
  409. {
  410. descrip = newDescrip;
  411. }
  412.  
  413. void CD::set_year(int newYear)
  414. {
  415. year = newYear;
  416. }
  417.  
  418. std::istream& operator >>(std::istream &infile, CD &CD)
  419. {
  420. infile.getline(artist, 100);
  421. infile.getline(album, 100);
  422. infile >> year;
  423. infile.getline(descrip, 100);
  424. do
  425. {
  426. infile.getline(descrip, 100);
  427. } while(descrip != "END");
  428. return is;
  429. }
  430. }
  431.  
  432. int main(int argc, char *argv[])
  433. {
  434. //Testing for correct number of arguments at command line
  435. if(argc != 3)
  436. {
  437. cout << "ERROR--Not enough arguements!!" << endl;
  438. cout << "The correct format is: \"./name of program\" \"input file\" \"output file\"" << endl;
  439. //exit(1);
  440. }
  441.  
  442. //opening data file from user at run time
  443. ifstream infile;
  444. infile.open(argv[1]);
  445.  
  446. if(infile.fail())
  447. {
  448. cout << "Failed to open input file." << endl;
  449. exit(1);
  450. }
  451.  
  452. ofstream outfile;
  453. outfile.open(argv[2]);
  454.  
  455. if(outfile.fail())
  456. {
  457. cout << "Failed to open input file." << endl;
  458. exit(1);
  459. }
  460.  
  461. char command[100], charArray[100], charArray2[100], charArray3[100];
  462. int length, compareValue;
  463.  
  464. while(infile >> command)
  465. {
  466. if(SString.str_compare(command, "add") == 0)
  467. {
  468. infile >> charArray;
  469. length = length_iter(charArray);
  470. outfile << length << endl;
  471. }
  472. else if(SString.str_compare(command, "get") == 0)
  473. {
  474. infile >> charArray;
  475. length = str_length(charArray);
  476. outfile << length << endl;
  477. }
  478. else if(SString.str_compare(command, "print") == 0)
  479. {
  480. infile >> charArray >> charArray2;
  481. compareValue = str_compare_iter(charArray, charArray2);
  482. outfile << compareValue << endl;
  483. }
  484. }
  485. outfile.close();
  486. infile.close();
  487. return 0;
  488. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1490
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Calling a member function inside a class

 
0
  #2
Sep 15th, 2008
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>
}
Last edited by Ancient Dragon; Sep 15th, 2008 at 10:17 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Re: Calling a member function inside a class

 
0
  #3
Sep 15th, 2008
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".
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1490
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Calling a member function inside a class

 
0
  #4
Sep 15th, 2008
you also have to change line 40 to be the correct parameter type.
Last edited by Ancient Dragon; Sep 15th, 2008 at 11:04 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Re: Calling a member function inside a class

 
0
  #5
Sep 15th, 2008
Ok I got it now. Thank you. One more question if I could. I'm getting "error: no match for 'operator[]' in 'temp[i]'". Can I not compare the char array like I did?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1490
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Calling a member function inside a class

 
0
  #6
Sep 16th, 2008
Your program contains millions (maybe even billions) of other errors. What line number are you talking about.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Re: Calling a member function inside a class

 
0
  #7
Sep 16th, 2008
338-341
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 78
Reputation: nizbit is an unknown quantity at this point 
Solved Threads: 0
nizbit nizbit is offline Offline
Junior Poster in Training

Re: Calling a member function inside a class

 
0
  #8
Sep 16th, 2008
Never mind. I figured out what was wrong. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1490
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Calling a member function inside a class

 
0
  #9
Sep 16th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC