| | |
Help with String Class
![]() |
•
•
Join Date: Sep 2009
Posts: 15
Reputation:
Solved Threads: 0
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
StringDefinitions.cpp
RunStrings.cpp
stringdefinition.cpp(7) : error C2448: 'stringClass::wordCount' : function-style initializer appears to be a function definition
When trying to compile this code:
StringHeader.h
C++ Syntax (Toggle Plain Text)
#ifndef H_StringHeader #define H_StringHeader #include <iostream> #include <string> #include <cstring> #include <algorithm> using namespace std; const int strLength = 51; char strArray[strLength]; int charAmount; string line; int total; class stringClass { public: int wordCount(string); }; #endif
StringDefinitions.cpp
C++ Syntax (Toggle Plain Text)
#include "StringHeader.H" int stringClass::wordCount(line) { cout << "\nThe String you entered in is: " << strArray << endl; charAmount = strlen(strArray); cout << "\nThere are " << charAmount << " characters in your String" << endl; line = strArray; int n = count(line.begin(), line.end(), ' '); cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl; (n+1) = total; return total; }
RunStrings.cpp
C++ Syntax (Toggle Plain Text)
#include "StringHeader.H" int main() { stringClass Obj; cout << "Enter a String" << endl; cin.getline(strArray, strLength); line = strArray; Obj.wordCount(line); cin.ignore(); return 0; }
Missing typename of argument in method definition,
C++ Syntax (Toggle Plain Text)
int stringClass::wordCount(string line){ ..... //(n+1) = total; // <---------------Remove this .... }
•
•
Join Date: Sep 2009
Posts: 15
Reputation:
Solved Threads: 0
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
int stringClass::wordCount(line) <--- Is the error here?
StringDefinitions.cpp
C++ Syntax (Toggle Plain Text)
void stringClass::wordCount(line) { cout << "\nThe String you entered in is: " << strArray << endl; charAmount = strlen(strArray); cout << "\nThere are " << charAmount << " characters in your String" << endl; line = strArray; int n = count(line.begin(), line.end(), ' '); cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl; }
Last edited by Wolf CCMLG; Oct 4th, 2009 at 4:40 am.
int stringClass::wordCount(
string line) C++ Syntax (Toggle Plain Text)
int stringClass::wordCount(string line){ cout << "\nThe String you entered in is: " << strArray << endl; charAmount = strlen(strArray); cout << "\nThere are " << charAmount << " characters in your String" << endl; line = strArray; int n = count(line.begin(), line.end(), ' '); cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl; return n; }
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2009
Posts: 15
Reputation:
Solved Threads: 0
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
StringDefinitions.CPP
RunStrings.CPP
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
C++ Syntax (Toggle Plain Text)
#ifndef H_StringHeader #define H_StringHeader #include <iostream> #include <string> #include <cstring> #include <algorithm> using namespace std; const int strLength = 51; char strArray[strLength]; class stringClass { public: int charAmount; int total; int wordCount(string); }; #endif
C++ Syntax (Toggle Plain Text)
#include "StringHeader.H" int stringClass::wordCount(string line) { cout<<"\nThe String you entered in is: "<<line<<endl; charAmount = strlen(strArray); cout<<"\nThere are "<<charAmount<<" characters in your String"<< endl; int n = count(line.begin(), line.end(), ' '); cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl; return n; }
C++ Syntax (Toggle Plain Text)
#include "StringHeader.H" string line; int main() { stringClass Obj; cout << "Enter a String" << endl; cin.getline(strArray, strLength); line = strArray; Obj.wordCount(line); cin.ignore(); return 0; }
•
•
Join Date: Sep 2009
Posts: 15
Reputation:
Solved Threads: 0
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.
•
•
•
•
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
StringDefinitions.CPPC++ Syntax (Toggle Plain Text)
#ifndef H_StringHeader #define H_StringHeader #include <iostream> #include <string> #include <cstring> #include <algorithm> using namespace std; const int strLength = 51; char strArray[strLength]; class stringClass { public: int charAmount; int wordCount(string); }; #endif
RunStrings.CPPC++ Syntax (Toggle Plain Text)
#include "StringHeader.H" int stringClass::wordCount(string line) { cout<<"\nThe String you entered in is: "<<line<<endl; charAmount = strlen(strArray); cout<<"\nThere are "<<charAmount<<" characters in your String"<< endl; int n = count(line.begin(), line.end(), ' '); cout<<"\nThere are "<< n + 1 <<" words in your String"<<endl; return n; }
C++ Syntax (Toggle Plain Text)
#include "StringHeader.H" string line; int main() { stringClass Obj; cout << "Enter a String" << endl; cin.getline(strArray, strLength); line = strArray; Obj.wordCount(line); cin.ignore(); return 0; }
![]() |
Similar Threads
- program depending on String class (Java)
- String class (C++)
- C++ help (string class) (C++)
- get token from string using string class (C++)
- error in user defined string class (C++)
- String Class Of Java (Java)
Other Threads in the C++ Forum
- Previous Thread: how to compare each element in a queue without modifying it
- Next Thread: passing char array as string
Views: 398 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error exception file files filestream forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework iamthwee input int lazy link linked-list linker list loop looping loops math matrix member memory newbie number object objects opengl output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference search sort spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual win32 window windows winsock wxwidgets






