Huge compile problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

Huge compile problems

 
0
  #1
Sep 13th, 2008
I really don't know where to start and to be honest I really don't know what I'm doing. I was given this project and we never went over these topics in class or the previous class. My attempts to wing it are not going so well and quite frankly its embarrassing. The program should be divided in its respective header and implementation files, but I am just trying to get the code to work. I can divide the code up later. I still need to write one more class which is the entire collection of DVD's. The c-string class is a re-definition of c-strings that I have to implement. The DVD class is attributes of a DVD in the collection. If anyone could please at the very least guide me in the right direction I would really appreciate it.

  1. #include <iostream>
  2. using std::istream;
  3. using std::ostream;
  4.  
  5. clas string
  6. {
  7. private:
  8. char _string[];
  9. //recursive functions
  10. int str_length(const char str[]);
  11. int str_compare(const char *str1, const char *str2);
  12. void str_reverse(char str[] , int length);
  13. bool str_replace_first(char str[], const char find[], const char replace[]);
  14.  
  15. //iterative functions
  16. int length_iter(const char str[]);
  17. int str_compare_iter(char str1[], const char str2[]);
  18. void str_reverse_iter(char str[], int length);
  19. bool str_replace_first_iter(char str[], const char find[], const char replace[]);
  20.  
  21. public:
  22. //Constructors
  23. string();
  24. string(char *array, int size);
  25. //Deconstructor
  26. ~string();
  27.  
  28. int str_length(){
  29. return str_length(_string);}
  30. int str_compare(){
  31. return str_compare(_string, _string);}
  32. void str_reverse(){
  33. return str_reverse(_string, str_length(_string));}
  34. bool str_replace_first(){
  35. return str_replace_first(_string, _string, _string);}
  36.  
  37. int length_iter(){
  38. return str_length(_string);}
  39. int str_compare_iter(){
  40. return str_compare(_string, _string);}
  41. void str_reverse_iter(){
  42. return str_reverse(_string, str_length(_string));}
  43. bool str_replace_first_iter(){
  44. return str_replace_first(_string, _string, _string);}
  45. //Overloaded Operators
  46. void operator+(string &rstring);
  47. friend std::istream &operator >>(std::istream &is, string &string);
  48. friend std::ostream &operator <<(std::ostream &os, string &string);
  49.  
  50. string::string()
  51. {
  52. _string = new char[100];
  53. _string[0] = '\0';
  54. }
  55.  
  56. string::string(char *array, int size)
  57. {
  58. _string = new char[size];
  59. for(int i=0; i < size; i++)
  60. {
  61. _string = *array;
  62. }
  63. }
  64. //Deconstructor
  65. string::~string()
  66. {
  67. delete [] _string;
  68. }
  69. /**
  70.  * This function finds the length of a c-string using recursion
  71.  * @param str[ ] a c-string variable that stores a string from user
  72.  * @pre in main command = "length", str[0] is not an empty string-'\0'
  73.  * @post if str[0]='\0' then 0 is returned or str_length is called again but at the next index of str[]
  74.  * @return the length of the c-string str[]
  75.  */
  76. int string::str_length(const char str[])
  77. {
  78. //base case
  79. if (str[0] == '\0')
  80. return 0;
  81. //recursive statement
  82. else
  83. return 1 + str_length(str+1);
  84. }
  85.  
  86. /**
  87.  * This function finds the length of a c-string using iteration
  88.  * @param str[ ] a c-string variable that stores a string from user
  89.  * @pre in main command = "length_iter", str[ ] is not an empty string-'\0'
  90.  * @post i is < 0
  91.  * @return the length of the c-string str[]
  92.  */
  93. int string::length_iter(const char str[])
  94. {
  95. int i=0;
  96. while(str[i] != '\0')
  97. {
  98. i++;
  99. }
  100. return i;
  101. }
  102.  
  103. /**
  104.  * This recursive function compares 2 c-strings to see if they are equal or there is a character mismatch
  105.  * @param *str1 is a pointer to charArray[ ]
  106.  * @param *str2 is a pointer to scharArray2[ ]
  107.  * @pre in main command = "compare", str1[ ] and str2[ ] is not an empty string-'\0'
  108.  * @post str1[] and str2[ ] are equal or *str1 and *str2 point to different characters
  109.  * @return a integer vaule that is the distance between the ASCII character vaules. If *str1 points to
  110. a smaller character, the vaule is negative. If *str2 points to the a smaller character the value is positive.
  111. If both *str1 and *str2 do not point to a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned
  112.  */
  113. int string::str_compare(const char *str1, const char *str2)
  114. {
  115. if (*str1 != '\0' || *str2 != '\0')
  116. {
  117. if (*str1 != *str2)
  118. {
  119. if (*str1 > *str2)
  120. {
  121. return *str1 - *str2;
  122. }
  123. else if (*str1 < *str2)
  124. {
  125. return *str1 - *str2;
  126. }
  127. }
  128.  
  129. }
  130. else return 0; //C-strings are equal-base case
  131. //recursive statement
  132. return str_compare(str1 + 1, str2 + 1);
  133. }
  134.  
  135. /**
  136.  * This iterative function compares 2 c-strings to see if they are equal or there is a character mismatch
  137.  * @param str1[ ] is a c-string variable that stores a string from user
  138.  * @param str2[ ] is a c-string variable that stores a string from user
  139.  * @pre in main command = "compare_iter", str1[ ] and str2[ ] are not an empty string-'\0'
  140.  * @post str1[] and str2[ ] are equal or str1[i] and str2[i] contain different characters
  141.  * @return a integer vaule(diff) that is the distance between the ASCII character vaules. If str1[i] contains
  142. a smaller character, the vaule(diff) is negative. If str2[i] contains the a smaller character the value(diff) is positive.
  143. If both str1[i] and str2[i] do not contain a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned (diff)
  144. */
  145. int string::str_compare_iter(char str1[], const char str2[])
  146. {
  147. for( ; *str1=*str2; *str1++, str2++)
  148. {
  149. if(*str1 == 0)
  150. return 0;
  151. }
  152.  
  153. return int(*str1-*str2);
  154. }
  155.  
  156. /**
  157.  * This recursive function will reverse the c-string str
  158.  * @param *str is a pointer to charArray[ ]
  159.  * @param length is the length of charArray[ ]
  160.  * @pre in main command = "reverse", *str does notpoint to an empty string-'\0'
  161.  * @post the string in charArray[ ] is reversed
  162.  * @return no return value
  163.  */
  164. void string::str_reverse(char *str, int length)
  165. {
  166.  
  167. char temp, temp1;
  168.  
  169. if (length >= 1)
  170. {
  171. // swap first & last chars
  172. temp = str[0];
  173. str[0] = str[length-1];
  174. str[length-1] = temp;
  175. // recursive call--
  176. str_reverse(&str[1], length-2);
  177. }
  178. }
  179.  
  180. /**
  181.  * * This recursive function will reverse the c-string str
  182.  * @param str[ ] str1[ ] is a c-string variable that stores a string from charArray[ ]
  183.  * @param length is the length of charArray[ ]
  184.  * @pre in main command = "reverse_iter", str[ ] does notpoint to an empty string-'\0'
  185.  * @post the string in charArray[ ] is reversed
  186.  * @return no return value
  187.  */
  188. void string::str_reverse_iter(char str[], int length)
  189. {
  190. char temp;
  191. int starti, endi, mid;//starting and ending index
  192. starti = 0;
  193. endi = length - 1;
  194. mid = length / 2;
  195.  
  196. while(starti < mid)
  197. {
  198. temp = str[starti];
  199. str[starti] = str[endi-starti];
  200. str[endi-starti] = temp;
  201. starti++;
  202. }
  203. }
  204.  
  205. /**
  206.  * This recursive function will search a c-string for a character(s) to be replaced with other character(s)
  207.  * @param *str points to charArray[ ] the array to be searched
  208.  * @param *find points to charArray2[ ], contains what to search for
  209.  * @param*replace points to charArray3[ ] what to replace the characters with
  210.  * @param is_partial_match is true if part of the find string is found in the original string
  211.  * @pre in main command = "replace_first", *str[ ] , *find, *replace does nont point to an empty string-'\0'
  212.  * @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
  213.  * @return false condition if *find and *replace were not equal in size thus the array was not searched or
  214.  true condition if *find and *replace were equal in size thus the array was searched and replaced with the contents *replace points to
  215.  */
  216. bool string::str_replace_first(char *str, const char *find, const char *replace)
  217. {
  218. if(length_iter(find) != length_iter(replace))
  219. {
  220. cout << "ERROR: The find and replace strings are of different length." << endl;
  221. cout << "Please change the find and replace strings to the same length." << endl;
  222. return false;
  223. }
  224.  
  225. while(*str != '\0')
  226. {
  227.  
  228. if(*str == *find)
  229. {
  230. *str = *replace;
  231. *replace++;
  232. *find++;
  233. }
  234.  
  235. else str_replace_first(str+1, find, replace);
  236. }
  237. return true;
  238. }
  239.  
  240. /**
  241.  * This iteraive function will search a c-string for a character(s) to be replaced with other character(s)
  242.  * @param str points to charArray[ ] the array to be searched
  243.  * @param find points to charArray2[ ], contains what to search for
  244.  * @param replace points to charArray3[ ] what to replace the characters with
  245.   * @param is_partial_match is true if part of the find string is found in the original string
  246.  * @pre in main command = "replace_first_iter", str , find, replace does nont point to an empty string-'\0'
  247.  * @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
  248.  * @return false condition if find and replace were not equal in size thus the array was not searched or
  249.  true condition if find and replace were equal in size thus the array was searched and replaced with the contents replace points to
  250.  */
  251. bool string::str_replace_first_iter(char str[], const char find[], const char replace[])
  252. {
  253. if(length_iter(find) != length_iter(replace))
  254. {
  255. cout << "ERROR: The find and replace strings are of different length." << endl;
  256. cout << "Please change the find and replace strings to the same length." << endl;
  257. return false;
  258. }
  259. int i, j, k, m, n, delta;
  260. int n1, n2, n3;
  261. char* p, *q;
  262. if (!str || !find)
  263. return 0;
  264. if (!replace)
  265. replace = "";
  266. n1 = str_length(str);
  267. n2 = str_length(find);
  268. n = n1 - n2 + 1;
  269. for (i = 0; i < n; ++i)
  270. {
  271. for (j = 0; j < n2 && find[j] == str[i+j]; ++j)
  272. ;
  273. if (j == n2) // found
  274. {
  275. n3 = str_length(replace);
  276. delta = n3 - n2;
  277. m = n1 - i - n2;
  278. if (delta < 0) /* move left */
  279. {
  280. p = str + (i + n2 + delta);
  281. q = p - delta;
  282. for (k = 0; k <= m; ++k)
  283. p[k] = q[k];
  284. }
  285. else if (delta > 0) /* move right */
  286. {
  287. q = str + n1 - m;
  288. p = q + delta;
  289. for (k = m; k >= 0; --k)
  290. p[k] = q[k];
  291. }
  292. for (k = 0; k < n3; ++k)
  293. str[i+k] = replace[k];
  294. return str + i + n3;
  295. }
  296. }
  297. return 0;
  298. }
  299.  
  300. int string::str_length(){
  301. return str_length(_string);}
  302.  
  303. int string::str_compare(){
  304. return str_compare(_string, _string);}
  305.  
  306. void string::str_reverse(){
  307. return str_reverse(_string, str_length(_string));}
  308.  
  309. bool string::str_replace_first(){
  310. return str_replace_first(_string, _string, _string);}
  311.  
  312. int string::length_iter(){
  313. return str_length(_string);}
  314.  
  315. int string::str_compare_iter(){
  316. return str_compare(_string, _string);}
  317.  
  318. void string::str_reverse_iter(){
  319. return str_reverse(_string, str_length(_string));}
  320.  
  321. bool string::str_replace_first_iter(){
  322. return str_replace_first(_string, _string, _string);}
  323.  
  324. void string::operator+(string &rstring)
  325. {
  326. //string temp;
  327. int len = str_length(_string) + str_length(rstring);
  328. string temp(_string, len);
  329. int i;
  330. for (i = 0; i < str_length(_string); i++)
  331. temp[i] = _string[i];
  332. for (int j = 0; j < str_length(rstring); j++, i++)
  333. temp[i] = rstring[j];
  334. //rstring[len]='\0';
  335. }
  336.  
  337. istream &operator >>(std::istream &is, string &string)
  338. {
  339. for(int i = 0; i < str_length(string); i++)
  340. is >> string[i];
  341. }
  342.  
  343. ostream &operator <<(std::ostream &os, string &string)
  344. {
  345. for(int i = 0; i < str_length(string); i++)
  346. os << string[i];
  347. return os;
  348. }
  349. };
  350.  
  351. namespace string
  352. {
  353. clas DVD : public string
  354. {
  355. private:
  356. string actor, title, descrip;
  357. int year;
  358.  
  359. public:
  360. DVD();
  361. DVD(string actor, string title, string descrip, int year);
  362. friend std::ostream& operator <<(std::ostream &os, DVD &DVD);
  363.  
  364. DVD()
  365. {
  366.  
  367. }
  368.  
  369. DVD(string actor, string title, string descrip, int year):string(actor, str_length(actor)),
  370. string(title, str_length(title)), string(descrip, str_length(descrip)), year =0)
  371. {
  372.  
  373. }
  374.  
  375. friend std::istream& operator >>(std::istream &is, DVD &DVD)
  376. {
  377. is.getline(actor, 100);
  378. is.getline(album, 100);
  379. is >> year;
  380. is.getline(descrip, 100);
  381. do
  382. {
  383. is.getline(descrip, 100);
  384. } while(descrip != 'END');
  385. return is;
  386. }
  387. }
  388. };
  389.  
  390. int main(){
  391. DVD one;
  392. one.actor >> "The Cure";
  393. one.title >> "Boys Don't Cry";
  394. one.year >> 1985;
  395. one.descrip >> "The best.";
  396. return 0;
  397. }

Compile errors:
  1. test.cpp:51: error: `SString::SString()' and `SString::SString()' cannot be overloaded
  2.  
  3. test.cpp:57: error: `SString::SString(char*, int)' and `SString::SString(char*, int)' cannot be overloaded
  4. test.cpp:66: error: `SString::~SString()' and `SString::~SString()' cannot be overloaded
  5. test.cpp:77: error: `int SString::str_length(const char*)' and `int SString::str_length(const char*)' cannot be overloaded
  6. test.cpp:94: error: `int SString::length_iter(const char*)' and `int SString::length_iter(const char*)' cannot be overloaded
  7.  
  8. test.cpp:114: error: `int SString::str_compare(const char*, const char*)' and `int SString::str_compare(const char*, const char*)' cannot be overloaded
  9. test.cpp:146: error: `int SString::str_compare_iter(char*, const char*)' and `int SString::str_compare_iter(char*, const char*)' cannot be overloaded
  10.  
  11. test.cpp:165: error: `void SString::str_reverse(char*, int)' and `void SString::str_reverse(char*, int)' cannot be overloaded
  12. test.cpp:189: error: `void SString::str_reverse_iter(char*, int)' and `void SString::str_reverse_iter(char*, int)' cannot be overloaded
  13. test.cpp:217: error: `bool SString::str_replace_first(char*, const char*, const char*)' and `bool SString::str_replace_first(char*, const char*, const char*)' cannot be overloaded
  14. test.cpp:252: error: `bool SString::str_replace_first_iter(char*, const char*, const char*)' and `bool SString::str_replace_first_iter(char*, const char*, const char*)' cannot be overloaded
  15. test.cpp:300: error: `int SString::str_length()' and `int SString::str_length()' cannot be overloaded
  16. test.cpp:303: error: `int SString::str_compare()' and `int SString::str_compare()' cannot be overloaded
  17. test.cpp:306: error: `void SString::str_reverse()' and `void SString::str_reverse()' cannot be overloaded
  18. test.cpp:309: error: `bool SString::str_replace_first()' and `bool SString::str_replace_first()' cannot be overloaded
  19. test.cpp:312: error: `int SString::length_iter()' and `int SString::length_iter()' cannot be overloaded
  20. test.cpp:315: error: `int SString::str_compare_iter()' and `int SString::str_compare_iter()' cannot be overloaded
  21. test.cpp:318: error: `void SString::str_reverse_iter()' and `void SString::str_reverse_iter()' cannot be overloaded
  22. test.cpp:321: error: `bool SString::str_replace_first_iter()' and `bool SString::str_replace_first_iter()' cannot be overloaded
  23.  
  24. test.cpp:325: error: `void SString::operator+(SString&)' and `void SString::operator+(SString&)' cannot be overloaded
  25. test.cpp:338: error: `std::istream& SString::operator>>(std::istream&, SString&)' must take exactly one argument
  26. test.cpp:344: error: `std::ostream& SString::operator<<(std::ostream&, SString&)' must take exactly one argument
  27. test.cpp: In constructor `SString::SString()':
  28. test.cpp:52: error: incompatible types in assignment of `char*' to `char[0u]'
  29. test.cpp: In constructor `SString::SString(char*, int)':
  30. test.cpp:58: error: incompatible types in assignment of `char*' to `char[0u]'
  31. test.cpp:61: error: incompatible types in assignment of `char' to `char[0u]'
  32.  
  33. test.cpp: In member function `bool SString::str_replace_first(char*, const char*, const char*)':
  34. test.cpp:220: error: `cout' undeclared (first use this function)
  35. test.cpp:220: error: (Each undeclared identifier is reported only once for each function it appears in.)
  36. test.cpp:220: error: `endl' undeclared (first use this function)
  37.  
  38. test.cpp: In member function `bool SString::str_replace_first_iter(char*, const char*, const char*)':
  39. test.cpp:255: error: `cout' undeclared (first use this function)
  40. test.cpp:255: error: `endl' undeclared (first use this function)
  41.  
  42. test.cpp: In member function `void SString::operator+(SString&)':
  43. test.cpp:327: error: no matching function for call to `SString::str_length(SString&)'
  44. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  45. test.cpp:28: note: int SString::str_length()
  46. test.cpp:331: error: no match for 'operator[]' in 'temp[i]'
  47. test.cpp:332: error: no matching function for call to `SString::str_length(SString&)'
  48. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  49. test.cpp:28: note: int SString::str_length()
  50. test.cpp:333: error: no match for 'operator[]' in 'temp[i]'
  51. test.cpp:333: error: no match for 'operator[]' in 'rstring[j]'
  52. test.cpp: In member function `std::istream& SString::operator>>(std::istream&, SString&)':
  53. test.cpp:339: error: no matching function for call to `SString::str_length(SString&)'
  54. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  55. test.cpp:28: note: int SString::str_length()
  56. test.cpp:340: error: no match for 'operator[]' in 'string[i]'
  57. test.cpp: In member function `std::ostream& SString::operator<<(std::ostream&, SString&)':
  58. test.cpp:345: error: no matching function for call to `SString::str_length(SString&)'
  59. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  60. test.cpp:28: note: int SString::str_length()
  61. test.cpp:346: error: no match for 'operator[]' in 'string[i]'
  62. test.cpp: At global scope:
  63. test.cpp:365: error: `SString::CD::CD()' and `SString::CD::CD()' cannot be overloaded
  64. test.cpp:369: error: `SString::CD::CD(SString, SString, SString, int)' and `SString::CD::CD(SString, SString, SString, int)' cannot be overloaded
  65. test.cpp:384:22: warning: multi-character character constant
  66. test.cpp: In constructor `SString::CD::CD(SString, SString, SString, int)':
  67. test.cpp:369: error: no matching function for call to `SString::CD::str_length(SString&)'
  68. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  69. test.cpp:28: note: int SString::str_length()
  70. test.cpp:370: error: no matching function for call to `SString::CD::str_length(SString&)'
  71. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  72. test.cpp:28: note: int SString::str_length()
  73. test.cpp:370: error: no matching function for call to `SString::CD::str_length(SString&)'
  74. test.cpp:10: note: candidates are: int SString::str_length(const char*)
  75. test.cpp:28: note: int SString::str_length()
  76. test.cpp:370: error: expected `(' before '=' token
  77. test.cpp:370: error: multiple initializations given for base `SString'
  78. test.cpp:370: error: multiple initializations given for base `SString'
  79.  
  80. test.cpp:370: error: expected `{' before '=' token
  81.  
  82. test.cpp: In function `std::istream& SString::operator>>(std::istream&, SString::CD&)':
  83. test.cpp:356: error: invalid use of non-static data member `SString::CD::artist'
  84. test.cpp:377: error: from this location
  85. test.cpp:378: error: `album' undeclared (first use this function)
  86. test.cpp:357: error: invalid use of non-static data member `SString::CD::year'
  87. test.cpp:379: error: from this location
  88.  
  89. test.cpp:356: error: invalid use of non-static data member `SString::CD::descrip'
  90. test.cpp:380: error: from this location
  91. test.cpp:356: error: invalid use of non-static data member `SString::CD::descrip'
  92. test.cpp:383: error: from this location
  93. test.cpp:356: error: invalid use of non-static data member `SString::CD::descrip'
  94. test.cpp:384: error: from this location
  95. test.cpp: At global scope:
  96. test.cpp:388: error: expected unqualified-id before '}' token
  97. test.cpp:388: error: expected `,' or `;' before '}' token
  98. test.cpp: In function `int main()':
  99. test.cpp:391: error: `CD' undeclared (first use this function)
  100. test.cpp:391: error: expected `;' before "one"
  101. test.cpp:392: error: `one' undeclared (first use this function)
  102.  
  103. Execution terminated
Last edited by Ancient Dragon; Sep 13th, 2008 at 8:53 pm. Reason: add line numbers for easier reading
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,604
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 online now Online
Still Learning

Re: Huge compile problems

 
0
  #2
Sep 13th, 2008
line 5: class is misspelled.

line 8: you must specify a length if you want to use a character array. If you want to use dynaming array allocation then use a pointer -- char* _string;

line 49: you must end the class declaration with };

The constructor with two parameters should be this:
  1. string::string(char *array, int size)
  2. {
  3. int i;
  4. _string = new char[size+1]; // +1 for the string's null terminator character
  5. for(i=0; i < size; i++)
  6. {
  7. _string[i] = array[i];
  8. }
  9. _string[i] = 0; // add string's null terminating character
  10. }

line 294: I don't know what you want to return here, but what you have coded isn't correct.
Last edited by Ancient Dragon; Sep 13th, 2008 at 9:07 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: Huge compile problems

 
0
  #3
Sep 13th, 2008
I did what was suggested, but still have some errors. As for line# 243, that function will not be used, but has to be declared. They just wanted to see if could follow syntax.

  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. return str_length(_string);}
  31. int str_compare(){
  32. return str_compare(_string, _string);}
  33. void str_reverse(){
  34. return str_reverse(_string, str_length(_string));}
  35. bool str_replace_first(){
  36. return str_replace_first(_string, _string, _string);}
  37.  
  38. int length_iter(){
  39. return str_length(_string);}
  40. int str_compare_iter(){
  41. return str_compare(_string, _string);}
  42. void str_reverse_iter(){
  43. return str_reverse(_string, str_length(_string));}
  44. bool str_replace_first_iter(){
  45. return str_replace_first(_string, _string, _string);}
  46. //Overloaded Operators
  47. void operator+(SString &rstring);
  48. friend std::istream &operator >>(std::istream &is, SString &string);
  49. friend std::ostream &operator <<(std::ostream &os, SString &string);
  50. };
  51. SString::SString()
  52. {
  53. _string = new char[100];
  54. _string[0] = '\0';
  55. }
  56.  
  57. SString::SString(char *array, int size)
  58. {
  59. int i;
  60. _string = new char[size+1]; // +1 for the string's null terminator character
  61. for(i=0; i < size; i++)
  62. {
  63. _string[i] = array[i];
  64. }
  65. _string[i] = 0; // add string's null terminating character
  66. }
  67. //Deconstructor
  68. SString::~SString()
  69. {
  70. delete [] _string;
  71. }
  72. /**
  73.  * This function finds the length of a c-string using recursion
  74.  * @param str[ ] a c-string variable that stores a string from user
  75.  * @pre in main command = "length", str[0] is not an empty string-'\0'
  76.  * @post if str[0]='\0' then 0 is returned or str_length is called again but at the next index of str[]
  77.  * @return the length of the c-string str[]
  78.  */
  79. int SString::str_length(const char str[])
  80. {
  81. //base case
  82. if (str[0] == '\0')
  83. return 0;
  84. //recursive statement
  85. else
  86. return 1 + str_length(str+1);
  87. }
  88.  
  89. /**
  90.  * This function finds the length of a c-string using iteration
  91.  * @param str[ ] a c-string variable that stores a string from user
  92.  * @pre in main command = "length_iter", str[ ] is not an empty string-'\0'
  93.  * @post i is < 0
  94.  * @return the length of the c-string str[]
  95.  */
  96. int SString::length_iter(const char str[])
  97. {
  98. int i=0;
  99. while(str[i] != '\0')
  100. {
  101. i++;
  102. }
  103. return i;
  104. }
  105.  
  106. /**
  107.  * This recursive function compares 2 c-strings to see if they are equal or there is a character mismatch
  108.  * @param *str1 is a pointer to charArray[ ]
  109.  * @param *str2 is a pointer to scharArray2[ ]
  110.  * @pre in main command = "compare", str1[ ] and str2[ ] is not an empty string-'\0'
  111.  * @post str1[] and str2[ ] are equal or *str1 and *str2 point to different characters
  112.  * @return a integer vaule that is the distance between the ASCII character vaules. If *str1 points to
  113. a smaller character, the vaule is negative. If *str2 points to the a smaller character the value is positive.
  114. If both *str1 and *str2 do not point to a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned
  115.  */
  116. int SString::str_compare(const char *str1, const char *str2)
  117. {
  118. if (*str1 != '\0' || *str2 != '\0')
  119. {
  120. if (*str1 != *str2)
  121. {
  122. if (*str1 > *str2)
  123. {
  124. return *str1 - *str2;
  125. }
  126. else if (*str1 < *str2)
  127. {
  128. return *str1 - *str2;
  129. }
  130. }
  131.  
  132. }
  133. else return 0; //C-strings are equal-base case
  134. //recursive statement
  135. return str_compare(str1 + 1, str2 + 1);
  136. }
  137.  
  138. /**
  139.  * This iterative function compares 2 c-strings to see if they are equal or there is a character mismatch
  140.  * @param str1[ ] is a c-string variable that stores a string from user
  141.  * @param str2[ ] is a c-string variable that stores a string from user
  142.  * @pre in main command = "compare_iter", str1[ ] and str2[ ] are not an empty string-'\0'
  143.  * @post str1[] and str2[ ] are equal or str1[i] and str2[i] contain different characters
  144.  * @return a integer vaule(diff) that is the distance between the ASCII character vaules. If str1[i] contains
  145. a smaller character, the vaule(diff) is negative. If str2[i] contains the a smaller character the value(diff) is positive.
  146. If both str1[i] and str2[i] do not contain a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned (diff)
  147. */
  148. int SString::str_compare_iter(char str1[], const char str2[])
  149. {
  150. for( ; *str1=*str2; *str1++, str2++)
  151. {
  152. if(*str1 == 0)
  153. return 0;
  154. }
  155.  
  156. return int(*str1-*str2);
  157. }
  158.  
  159. /**
  160.  * This recursive function will reverse the c-string str
  161.  * @param *str is a pointer to charArray[ ]
  162.  * @param length is the length of charArray[ ]
  163.  * @pre in main command = "reverse", *str does notpoint to an empty string-'\0'
  164.  * @post the string in charArray[ ] is reversed
  165.  * @return no return value
  166.  */
  167. void SString::str_reverse(char *str, int length)
  168. {
  169.  
  170. char temp, temp1;
  171.  
  172. if (length >= 1)
  173. {
  174. // swap first & last chars
  175. temp = str[0];
  176. str[0] = str[length-1];
  177. str[length-1] = temp;
  178. // recursive call--
  179. str_reverse(&str[1], length-2);
  180. }
  181. }
  182.  
  183. /**
  184.  * * This recursive function will reverse the c-string str
  185.  * @param str[ ] str1[ ] is a c-string variable that stores a string from charArray[ ]
  186.  * @param length is the length of charArray[ ]
  187.  * @pre in main command = "reverse_iter", str[ ] does notpoint to an empty string-'\0'
  188.  * @post the string in charArray[ ] is reversed
  189.  * @return no return value
  190.  */
  191. void SString::str_reverse_iter(char str[], int length)
  192. {
  193. char temp;
  194. int starti, endi, mid;//starting and ending index
  195. starti = 0;
  196. endi = length - 1;
  197. mid = length / 2;
  198.  
  199. while(starti < mid)
  200. {
  201. temp = str[starti];
  202. str[starti] = str[endi-starti];
  203. str[endi-starti] = temp;
  204. starti++;
  205. }
  206. }
  207.  
  208. /**
  209.  * This recursive function will search a c-string for a character(s) to be replaced with other character(s)
  210.  * @param *str points to charArray[ ] the array to be searched
  211.  * @param *find points to charArray2[ ], contains what to search for
  212.  * @param*replace points to charArray3[ ] what to replace the characters with
  213.  * @param is_partial_match is true if part of the find string is found in the original string
  214.  * @pre in main command = "replace_first", *str[ ] , *find, *replace does nont point to an empty string-'\0'
  215.  * @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
  216.  * @return false condition if *find and *replace were not equal in size thus the array was not searched or
  217.  true condition if *find and *replace were equal in size thus the array was searched and replaced with the contents *replace points to
  218.  */
  219. bool SString::str_replace_first(char *str, const char *find, const char *replace)
  220. {
  221. if(length_iter(find) != length_iter(replace))
  222. {
  223. cout << "ERROR: The find and replace strings are of different length." << endl;
  224. cout << "Please change the find and replace strings to the same length." << endl;
  225. return false;
  226. }
  227.  
  228. while(*str != '\0')
  229. {
  230.  
  231. if(*str == *find)
  232. {
  233. *str = *replace;
  234. *replace++;
  235. *find++;
  236. }
  237.  
  238. else str_replace_first(str+1, find, replace);
  239. }
  240. return true;
  241. }
  242.  
  243. /**
  244.  * This iteraive function will search a c-string for a character(s) to be replaced with other character(s)
  245.  * @param str points to charArray[ ] the array to be searched
  246.  * @param find points to charArray2[ ], contains what to search for
  247.  * @param replace points to charArray3[ ] what to replace the characters with
  248.   * @param is_partial_match is true if part of the find string is found in the original string
  249.  * @pre in main command = "replace_first_iter", str , find, replace does nont point to an empty string-'\0'
  250.  * @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
  251.  * @return false condition if find and replace were not equal in size thus the array was not searched or
  252.  true condition if find and replace were equal in size thus the array was searched and replaced with the contents replace points to
  253.  */
  254. bool SString::str_replace_first_iter(char str[], const char find[], const char replace[])
  255. {
  256. if(length_iter(find) != length_iter(replace))
  257. {
  258. cout << "ERROR: The find and replace strings are of different length." << endl;
  259. cout << "Please change the find and replace strings to the same length." << endl;
  260. return false;
  261. }
  262. int i, j, k, m, n, delta;
  263. int n1, n2, n3;
  264. char* p, *q;
  265. if (!str || !find)
  266. return 0;
  267. if (!replace)
  268. replace = "";
  269. n1 = str_length(str);
  270. n2 = str_length(find);
  271. n = n1 - n2 + 1;
  272. for (i = 0; i < n; ++i)
  273. {
  274. for (j = 0; j < n2 && find[j] == str[i+j]; ++j)
  275. ;
  276. if (j == n2) // found
  277. {
  278. n3 = str_length(replace);
  279. delta = n3 - n2;
  280. m = n1 - i - n2;
  281. if (delta < 0) /* move left */
  282. {
  283. p = str + (i + n2 + delta);
  284. q = p - delta;
  285. for (k = 0; k <= m; ++k)
  286. p[k] = q[k];
  287. }
  288. else if (delta > 0) /* move right */
  289. {
  290. q = str + n1 - m;
  291. p = q + delta;
  292. for (k = m; k >= 0; --k)
  293. p[k] = q[k];
  294. }
  295. for (k = 0; k < n3; ++k)
  296. str[i+k] = replace[k];
  297. return str + i + n3;
  298. }
  299. }
  300. return 0;
  301. }
  302.  
  303. int SString::str_length(){
  304. return str_length(_string);}
  305.  
  306. int SString::str_compare(){
  307. return str_compare(_string, _string);}
  308.  
  309. void SString::str_reverse(){
  310. return str_reverse(_string, str_length(_string));}
  311.  
  312. bool SString::str_replace_first(){
  313. return str_replace_first(_string, _string, _string);}
  314.  
  315. int SString::length_iter(){
  316. return str_length(_string);}
  317.  
  318. int SString::str_compare_iter(){
  319. return str_compare(_string, _string);}
  320.  
  321. void SString::str_reverse_iter(){
  322. return str_reverse(_string, str_length(_string));}
  323.  
  324. bool SString::str_replace_first_iter(){
  325. return str_replace_first(_string, _string, _string);}
  326.  
  327. void SString::operator+(SString &rstring)
  328. {
  329. //SString temp;
  330. int len = str_length(_string) + str_length(rstring);
  331. SString temp(_string, len);
  332. int i;
  333. for (i = 0; i < str_length(_string); i++)
  334. temp[i] = _string[i];
  335. for (int j = 0; j < str_length(rstring); j++, i++)
  336. temp[i] = rstring[j];
  337. //rstring[len]='\0';
  338. }
  339.  
  340. istream &operator >>(std::istream &is, SString &string)
  341. {
  342. for(int i = 0; i < str_length(string); i++)
  343. is >> string[i];
  344. }
  345.  
  346. ostream &operator <<(std::ostream &os, SString &string)
  347. {
  348. for(int i = 0; i < str_length(string); i++)
  349. os << string[i];
  350. return os;
  351. }
  352.  
  353. namespace SString
  354. {
  355. class CD : public SString
  356. {
  357. private:
  358. SString artist, title, descrip;
  359. int year;
  360.  
  361. public:
  362. CD();
  363. CD(SString artist, SString title, SString descrip, int year);
  364. friend std::ostream& operator <<(std::ostream &os, CD &cd);
  365. };
  366. CD()
  367. {
  368.  
  369. }
  370.  
  371. CD(SString artist, SString title, SString descrip, int year):SString(artist, str_length(artist)),
  372. SString(title, str_length(title)), SString(descrip, str_length(descrip)), year =0)
  373. {
  374.  
  375. }
  376.  
  377. friend std::istream& operator >>(std::istream &is, CD &cd)
  378. {
  379. is.getline(artist, 100);
  380. is.getline(album, 100);
  381. is >> year;
  382. is.getline(descrip, 100);
  383. do
  384. {
  385. is.getline(descrip, 100);
  386. } while(descrip != 'END');
  387. return is;
  388. }
  389. }
  390.  
  391.  
  392. int main(){
  393. CD one;
  394. one.artist >> "The Cure";
  395. one.title >> "Boys Don't Cry";
  396. one.year >> 1985;
  397. one.descrip >> "The best.";
  398. return 0;
  399. }

Errors:
  1.  
  2. test.cpp: In member function `void SString::operator+(SString&)':
  3. test.cpp:330: error: no matching function for call to `SString::str_length(SString&)'
  4. test.cpp:80: note: candidates are: int SString::str_length(const char*)
  5. test.cpp:29: note: int SString::str_length()
  6.  
  7. test.cpp:334: error: no match for 'operator[]' in 'temp[i]'
  8. test.cpp:335: error: no matching function for call to `SString::str_length(SString&)'
  9. test.cpp:80: note: candidates are: int SString::str_length(const char*)
  10. test.cpp:29: note: int SString::str_length()
  11. test.cpp:336: error: no match for 'operator[]' in 'temp[i]'
  12.  
  13. test.cpp:336: error: no match for 'operator[]' in 'rstring[j]'
  14. test.cpp: In function `std::istream& operator>>(std::istream&, SString&)':
  15. test.cpp:342: error: `str_length' undeclared (first use this function)
  16. test.cpp:342: error: (Each undeclared identifier is reported only once for each function it appears in.)
  17. test.cpp:343: error: no match for 'operator[]' in 'string[i]'
  18. test.cpp: In function `std::ostream& operator<<(std::ostream&, SString&)':
  19. test.cpp:348: error: `str_length' undeclared (first use this function)
  20. test.cpp:349: error: no match for 'operator[]' in 'string[i]'
  21.  
  22. test.cpp: At global scope:
  23. test.cpp:366: error: expected unqualified-id before ')' token
  24. test.cpp:367: error: expected `,' or `;' before '{' token
  25. test.cpp:371: error: expected `)' before "artist"
  26. test.cpp:371: error: expected `,' or `;' before "artist"
  27. test.cpp:378: error: can't initialize friend function `operator>>'
  28. test.cpp:378: error: friend declaration not in class definition
  29.  
  30. test.cpp: In function `std::istream& SString::operator>>(std::istream&, SString::CD&)':
  31. test.cpp:379: error: `artist' undeclared (first use this function)
  32. test.cpp:380: error: `album' undeclared (first use this function)
  33. test.cpp:381: error: `year' undeclared (first use this function)
  34. test.cpp:382: error: `descrip' undeclared (first use this function)
  35. test.cpp:386:22: warning: multi-character character constant
  36. test.cpp: In function `int main()':
  37. test.cpp:393: error: `CD' undeclared (first use this function)
  38. test.cpp:393: error: expected `;' before "one"
  39. test.cpp:394: error: `one' undeclared (first use this function)
  40.  
  41. Execution terminated
  42.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,604
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 online now Online
Still Learning

Re: Huge compile problems

 
0
  #4
Sep 13th, 2008
That first error is telling you that SString class does not have a constructor that takes a SString object by reference. You will probably have to either add one, or change line 330 like this: int len = str_length(_string) + str_length(rstring._string);
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: Huge compile problems

 
0
  #5
Sep 13th, 2008
Well, I wanted to say thank for your help, but I'm throwing in the towel. This program requires a skill set that unfortunately I do not possess. I could complain that the project gets into topics that were never covered in this or my previous class, but that doesn't get the job done. Its a weed out class anyway, so they succeeded.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,604
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 online now Online
Still Learning

Re: Huge compile problems

 
0
  #6
Sep 13th, 2008
Was there a prerequisit for that class? Have you taken those presequisite classes? Have you read the chapter(s) in your textbook? Maybe you need to start with a project a lot simpler so that you can learn the basics as you go along.
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: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Huge compile problems

 
1
  #7
Sep 14th, 2008
Originally Posted by nizbit View Post
Well, I wanted to say thank for your help, but I'm throwing in the towel. This program requires a skill set that unfortunately I do not possess. I could complain that the project gets into topics that were never covered in this or my previous class, but that doesn't get the job done. Its a weed out class anyway, so they succeeded.
Oh don't give up now; you look like you're really close. I'd say there's plenty more weedy students in your class than you.

A lot of the compiler errors are duplicate function definition: it means that you've defined the guts of the same function more than once. Also, the declarations (prototypes) go inside the class, and the definitions (the workings) generally go after.

So, as I see it, the class declaration should be
  1. class SString
  2. {
  3. private:
  4. char *_string;
  5. //recursive functions
  6. int str_length(const char str[]);
  7. int str_compare(const char *str1, const char *str2);
  8. void str_reverse(char str[] , int length);
  9. bool str_replace_first(char str[], const char find[], const char replace[]);
  10.  
  11. //iterative functions
  12. int length_iter(const char str[]);
  13. int str_compare_iter(char str1[], const char str2[]);
  14. void str_reverse_iter(char str[], int length);
  15. bool str_replace_first_iter(char str[], const char find[], const char replace[]);
  16.  
  17. public:
  18. //Constructors
  19. SString();
  20. SString(char *array, int size);
  21. //Deconstructor
  22. ~SString();
  23.  
  24. int str_length();
  25. int str_compare();
  26. void str_reverse();
  27. bool str_replace_first();
  28.  
  29. int length_iter();
  30. int str_compare_iter();
  31. void str_reverse_iter();
  32. bool str_replace_first_iter();
  33.  
  34. //Overloaded Operators
  35. void operator+(SString &rstring);
  36. friend std::istream &operator >>(std::istream &is, SString &string);
  37. friend std::ostream &operator <<(std::ostream &os, SString &string);
  38. };

Try compiling again, there will be less errors. Note that the errors that are reported back to you _try_ to explain what's wrong - and gives you the line number, which should help point you in the direction of what's going on.
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: Huge compile problems

 
0
  #8
Sep 14th, 2008
This class is Programming II. So Programming I was the pre-req. In Prog I the last topic was more like an intro to classes. The chapter we just finished, was almost a review. The project is around Chapter 8. I tried to follow it but it seemed that I was missing a few chapters before that.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,604
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 online now Online
Still Learning

Re: Huge compile problems

 
0
  #9
Sep 14th, 2008
Suggest you re-study the first 8 chapters in your book. Many instructors don't teach directly from the book -- they expect you to read it.
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: Huge compile problems

 
0
  #10
Sep 14th, 2008
Didn't see your post dougy83. I'll will give that a shot.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC