Help with String Class

Reply

Join Date: Sep 2009
Posts: 15
Reputation: Wolf CCMLG is an unknown quantity at this point 
Solved Threads: 0
Wolf CCMLG Wolf CCMLG is offline Offline
Newbie Poster

Help with String Class

 
0
  #1
Oct 4th, 2009
Can someone help me please, I am getting this error:

stringdefinition.cpp(7) : error C2448: 'stringClass::wordCount' : function-style initializer appears to be a function definition

When trying to compile this code:


StringHeader.h
  1. #ifndef H_StringHeader
  2. #define H_StringHeader
  3. #include <iostream>
  4. #include <string>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. const int strLength = 51;
  10. char strArray[strLength];
  11. int charAmount;
  12. string line;
  13. int total;
  14.  
  15. class stringClass
  16. {
  17. public:
  18. int wordCount(string);
  19. };
  20. #endif

StringDefinitions.cpp
  1. #include "StringHeader.H"
  2.  
  3. int stringClass::wordCount(line)
  4. {
  5. cout << "\nThe String you entered in is: " << strArray << endl;
  6. charAmount = strlen(strArray);
  7. cout << "\nThere are " << charAmount << " characters in your String" << endl;
  8. line = strArray;
  9. int n = count(line.begin(), line.end(), ' ');
  10. cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl;
  11. (n+1) = total;
  12.  
  13. return total;
  14. }

RunStrings.cpp

  1. #include "StringHeader.H"
  2.  
  3. int main()
  4. {
  5. stringClass Obj;
  6. cout << "Enter a String" << endl;
  7. cin.getline(strArray, strLength);
  8. line = strArray;
  9. Obj.wordCount(line);
  10.  
  11. cin.ignore();
  12. return 0;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3,955
Reputation: adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future 
Solved Threads: 707
Moderator
adatapost adatapost is offline Offline
Senior Poster

Re: Help with String Class

 
1
  #2
Oct 4th, 2009
Missing typename of argument in method definition,
  1. int stringClass::wordCount(string line){
  2. .....
  3. //(n+1) = total; // <---------------Remove this
  4. ....
  5. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 15
Reputation: Wolf CCMLG is an unknown quantity at this point 
Solved Threads: 0
Wolf CCMLG Wolf CCMLG is offline Offline
Newbie Poster

Re: Help with String Class

 
0
  #3
Oct 4th, 2009
I'm sorry, I still don't understand which part of the code is missing. It tells me line 7 in the compiler error, but line 7 only contains a {.

int stringClass::wordCount(line) <--- Is the error here?

StringDefinitions.cpp
  1. void stringClass::wordCount(line)
  2. {
  3. cout << "\nThe String you entered in is: " << strArray << endl;
  4. charAmount = strlen(strArray);
  5. cout << "\nThere are " << charAmount << " characters in your String" << endl;
  6. line = strArray;
  7. int n = count(line.begin(), line.end(), ' ');
  8. cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl;
  9. }
Last edited by Wolf CCMLG; Oct 4th, 2009 at 4:40 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3,955
Reputation: adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future 
Solved Threads: 707
Moderator
adatapost adatapost is offline Offline
Senior Poster

Re: Help with String Class

 
0
  #4
Oct 4th, 2009
int stringClass::wordCount( string line)

  1. int stringClass::wordCount(string line){
  2.  
  3. cout << "\nThe String you entered in is: " << strArray << endl;
  4. charAmount = strlen(strArray);
  5. cout << "\nThere are " << charAmount << " characters in your String"
  6. << endl;
  7. line = strArray;
  8. int n = count(line.begin(), line.end(), ' ');
  9. cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl;
  10.  
  11. return n;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: venkat arun has a little shameless behaviour in the past 
Solved Threads: 0
venkat arun venkat arun is offline Offline
Newbie Poster

Re: Help with String Class

 
0
  #5
Oct 4th, 2009
While defining a function there needs to be a type with the variable name. So you need to put it as string line instead of just line.

PS: are you trying to make a string class? If yes then some of the global variables should be inside the class, so that multiple objects can be handled effectively.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: venkat arun has a little shameless behaviour in the past 
Solved Threads: 0
venkat arun venkat arun is offline Offline
Newbie Poster

Re: Help with String Class

 
0
  #6
Oct 4th, 2009
Sorry, I just read the second post of yours, it is giving an error there because sometimes compilers aren't able to pinpoint the exact spot of the error, so it is advisable to look at the surrounding code when a compiler gives an error.

Tip: I have noticed that some compilers give an unreasonably large no of errors, this is usually because of an unbalanced parentheses (unequal no. of closing and opening brackets). But of course it can be genuine also
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 15
Reputation: Wolf CCMLG is an unknown quantity at this point 
Solved Threads: 0
Wolf CCMLG Wolf CCMLG is offline Offline
Newbie Poster

Re: Help with String Class

 
0
  #7
Oct 4th, 2009
Thanks for the help everyone, but I am getting a new error now. I tried to put more variables into the class. I left the char * array and const int out. The error is:
StringDefinition.obj : error LNK2005: "char * strArray" (?strArray@@3PADA) already defined in RunStrings.obj
It looks like I may be defining my char * array twice, but I don't know where I am doing this.
Here is my updated code:

StringHeader.H
  1. #ifndef H_StringHeader
  2. #define H_StringHeader
  3. #include <iostream>
  4. #include <string>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. const int strLength = 51;
  10. char strArray[strLength];
  11. class stringClass
  12. {
  13. public:
  14. int charAmount;
  15. int total;
  16. int wordCount(string);
  17. };
  18. #endif
StringDefinitions.CPP
  1. #include "StringHeader.H"
  2.  
  3. int stringClass::wordCount(string line)
  4. {
  5. cout<<"\nThe String you entered in is: "<<line<<endl;
  6. charAmount = strlen(strArray);
  7. cout<<"\nThere are "<<charAmount<<" characters in your String"<< endl;
  8. int n = count(line.begin(), line.end(), ' ');
  9. cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl;
  10. return n;
  11. }
RunStrings.CPP
  1. #include "StringHeader.H"
  2.  
  3. string line;
  4. int main()
  5. {
  6. stringClass Obj;
  7. cout << "Enter a String" << endl;
  8. cin.getline(strArray, strLength);
  9. line = strArray;
  10. Obj.wordCount(line);
  11.  
  12. cin.ignore();
  13. return 0;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 15
Reputation: Wolf CCMLG is an unknown quantity at this point 
Solved Threads: 0
Wolf CCMLG Wolf CCMLG is offline Offline
Newbie Poster

Re: Help with String Class

 
0
  #8
Oct 4th, 2009
Should I not declare the char * array in the header file? I can't seem to find where I am defining it twice. Any help would be greatly appreciated.



Originally Posted by Wolf CCMLG View Post
Thanks for the help everyone, but I am getting a new error now. I tried to put more variables into the class. I left the char * array and const int out. The error is:
StringDefinition.obj : error LNK2005: "char * strArray" (?strArray@@3PADA) already defined in RunStrings.obj
It looks like I may be defining my char * array twice, but I don't know where I am doing this.
Here is my updated code:

StringHeader.H
  1. #ifndef H_StringHeader
  2. #define H_StringHeader
  3. #include <iostream>
  4. #include <string>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. const int strLength = 51;
  10. char strArray[strLength];
  11. class stringClass
  12. {
  13. public:
  14. int charAmount;
  15. int wordCount(string);
  16. };
  17. #endif
StringDefinitions.CPP
  1. #include "StringHeader.H"
  2.  
  3. int stringClass::wordCount(string line)
  4. {
  5. cout<<"\nThe String you entered in is: "<<line<<endl;
  6. charAmount = strlen(strArray);
  7. cout<<"\nThere are "<<charAmount<<" characters in your String"<< endl;
  8. int n = count(line.begin(), line.end(), ' ');
  9. cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl;
  10. return n;
  11. }
RunStrings.CPP
  1. #include "StringHeader.H"
  2.  
  3. string line;
  4. int main()
  5. {
  6. stringClass Obj;
  7. cout << "Enter a String" << endl;
  8. cin.getline(strArray, strLength);
  9. line = strArray;
  10. Obj.wordCount(line);
  11.  
  12. cin.ignore();
  13. return 0;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 15
Reputation: Wolf CCMLG is an unknown quantity at this point 
Solved Threads: 0
Wolf CCMLG Wolf CCMLG is offline Offline
Newbie Poster

Re: Help with String Class

 
0
  #9
Oct 4th, 2009
I am going to take everything out of the class and see if that helps.
Reply With Quote Quick reply to this message  
Reply

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




Views: 398 | Replies: 8
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC