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

#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

#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

#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,

int stringClass::wordCount(string line){
   .....
  //(n+1) = total;   // <---------------Remove this
  ....
}

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

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;
}

int stringClass::wordCount( string line)

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;
}

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.

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 :)

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. :confused:
Here is my updated code:

StringHeader.H

#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

StringDefinitions.CPP

#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;
}

RunStrings.CPP

#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;
}

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. :confused:
Here is my updated code:

StringHeader.H

#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

StringDefinitions.CPP

#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;
}

RunStrings.CPP

#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;
}

I am going to take everything out of the class and see if that helps.

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.