function does not take 1 arguments?

Hey guys, so for my college class we have a project where we prompt the user to enter a sentence, then we take the first word (a contiguous sequence of letters delimited either by non-letter characters or by the start or the end of the string), and we evaluate it later. The professor gave us a program to test it out, but I keep getting the 3 of the error "C2660: 'getFirstWord': function does not take 1 arguments" for each of the given tests.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string getFirstWord();

void test()
	{
	      // This writes "Hello"
	    cerr << getFirstWord("!!Hello, Fred") << endl;

	      // This writes "greetings 9"
	    string msg = "greetings, mom, how are you?";
	    string result = getFirstWord(msg);
	    cerr << result << " " << result.size() << endl;

	      // This writes "0"
	    string s = getFirstWord(" $@#%!!");
	    cerr << s.size() << endl;
	}

string getFirstWord(string text)
{
	string t;
	for(size_t k = 0; k != text.size()-1; k++)
	{
		if (isalpha(text[k]))
			t += text[k];
		if (isalpha(text[k]) && !isalpha(text[k+1]))
			break;
	}

	return t;

}
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string getFirstWord(); /// declaration doesn't match definition
...
string getFirstWord(string text)
{...
Last edited on
Yeah you're missing the parameter at your declaration

string getFirstWord(); should be string getFirstWord(string);
Your function prototype and definition do not match.

string getFirstWord();


string getFirstWord(string text)
Ok, that makes sense, but when I declare it as
string getFirstWord(string);
I don't get the C2660 errors, but I get two new ones: LNK2019: unresolved external symbol_main referenced in function __tmainCRTStartup and LNK1120: 1 unresolved externals
Last edited on
Where is your main function?
urrrrrr do I put the test into the main function? I didn't think I needed it.

I'm totally clueless, people.
In C++, it is mandatory to have a main() function that returns an int. When the program executes, the control begins from the 1st line of this main() function. You can do so by changing

void test()
to
int main()
Huzzah! Thank you, good people! It's now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string getFirstWord(string);

int main()
	{
	      // This writes "Hello"
	    cerr << getFirstWord("!!Hello, Fred") << endl;

	      // This writes "greetings 9"
	    string msg = "greetings, mom, how are you?";
	    string result = getFirstWord(msg);
	    cerr << result << " " << result.size() << endl;

	      // This writes "0"
	    string s = getFirstWord(" $@#%!!");
	    cerr << s.size() << endl;
	}

string getFirstWord(string text)
{
	string t;
	for(size_t k = 0; k != text.size()-1; k++)
	{
		if (isalpha(text[k]))
			t += text[k];
		if (isalpha(text[k]) && !isalpha(text[k+1]))
			break;
	}

	return t;

}


and it works!
Another thing I'd like to add, you do not have a return statement in your main() despite the fact that it should return an int.

For the main() function specially, the compiler understands that if no return value is given, then it by default gives it a return value of 0 upon exit.
Topic archived. No new replies allowed.