CANNOT fix error C2660: function does not take 0 arguments.

Pages: 12
Hi, I am a beginner with C++, been programming for a month or so now. I am trying to write a simple program for my CSCI class using arrays and functions, but I cannot get past the error C2660, "function does not take 0 arguments". I've been trying to figure it out for about 2 hours now. The only answer I seem to find is that the arguments in the function declaration must match the function prototype, yet this does not solve my problem. If anyone could help save me from further headaches and tell me what I am doing wrong, it'd be greatly appreciated. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <iostream>
using namespace std;

void displayFunction(int);

int _tmain(int argc, _TCHAR* argv[])
{
	displayFunction();
	return 0;
}

void displayFunction(int)
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

	cout << array_1[10] << endl;
	cout << array_2[5] << endl;
}
closed account (zb0S216C)
In displayFunction()'s prototype, you specify that a signed int is required to invoke the function. However, in main(), you invoke the function without passing anything. Besides, a parameter without an identifier is untouchable and therefore, is useless. You have three options here:

- Omit the parameter altogether
- Specify an identifier for the parameter so that you can access it
- Specify an identifier for the parameter and give it a default argument

The second option requires an argument when the function is invoked. The third option doesn't require an argument when the function is invoked, because the compiler will use the default one (unless you overwrite it).

Edit: Examples would probably help you here.

Option 2:

1
2
3
4
5
6
void MyFunction(int Parameter);

int main()
{
    MyFunction(10); // 10 is required here.
}

Option 3:

1
2
3
4
5
6
void MyFunction(int Parameter = 10); // 10 is the default argument.

int main()
{
    MyFunction();
}

In the latter code, the compiler will use the default argument (10). This means that you don't have to pass a value to MyFunction() when you call it. However, if you do pass a value, it'll override the default argument in favour of the one you specified at the call site.

Wazzak
Last edited on
Thanks very much for the quick response. I understand what you are saying, and it made sense to me to just omit the parameter altogether.

However, the error still occurs even without a parameter.
closed account (zb0S216C)
Did you remove the int from the function definition, too?

Wazzak
Yes. int is not in the function prototype, call, or definition. They all have empty parentheses.
Last edited on
closed account (zb0S216C)
Can you post the code you have so far? There's more than meets the eye here.

Wazzak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <iostream>
using namespace std;

void displayFunction();

int _tmain(int argc, _TCHAR* argv[])
{
	displayFunction();
	return 0;
}

void displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

	cout << array_1[10] << endl;
	cout << array_2[5] << endl;
}
closed account (zb0S216C)
Has "stdafx.h" got any prototypes within it?

The only issue I see here is the std::cout statements in displayFunction()'s definition. Note that the index range of an array is 0 to N - 1 (N is the length of the array). For example, an array of 10 elements has an index range of 0 to 9.

Wazzak
Last edited on
Not sure if stdafx.h has any prototypes.

Could you elaborate on what you mean? What exactly is wrong with the cout statements?
closed account (zb0S216C)
LilKB wrote:
"What exactly is wrong with the cout statements?"

In addition to my proceeding reply, you're attempting to read the 11th element of array_1, which doesn't exist, and you're attempting to access the 6th element of array_2, which, again, doesn't exist.

Wazzak
Last edited on
On lines 15 and 16 you define array_1 as an array of 10 ints and array_2 an array of 5 ints.

Then on lines 18 and 19, you try to output the 11th and 6th elements. Those elements don't exist, so the result is undefined.

EDIT: and as Framework said:
...Note that the index range of an array is 0 to N - 1 (N is the length of the array). For example, an array of 10 elements has an index range of 0 to 9.


EDIT2: NINJA'D :O
Last edited on
I think that you are going tp print out elements of your arrays in function displayFunction. In this case you shall print out each element of arrays in a loop. For example

1
2
3
4
5
6
7
8
9
10
11
void displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

 
	for ( int i = 0; i < 10; i++ ) std::cout << array_1[i] << ' ';
	std::cout << std::endl;
	for ( int i = 0; i < 5; i++ ) std::cout << array_2[i] << ' ';
	std::cout << std::endl;
}

Also yoi can use standard algorithms for this purpose as, for example, std::copy or std::for_each and lambda expressions. In this case you shall include headers <algorithm> and <iterator>. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
void displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

 
	std::copy( std::begin( array_1 ), std::end( array_1 ),
		std::ostream_iterator<int>( std::cout, " " ) );
	std::cout << std::endl;
	std::copy( std::begin( array_2 ), std::end( array_2 ),
		std::ostream_iterator<int>( std::cout, " " ) );
	std::cout << std::endl;
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
void displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

 
	std::for_each( std::begin( array_1 ), std::end( array_1 ),
		       []( int x ) { std::cout << x << ' '; } );
	std::cout << std::endl;
	std::for_each( std::begin( array_2 ), std::end( array_2 ),
		       []( int x ) { std::cout << x << ' '; } );
	std::cout << std::endl;
}


If your MS VC++ compiler would suppot the for based on range you also could use it to perform this task.
Last edited on
closed account (zb0S216C)
You're having a laugh; that's just OTT. Do you really expect a beginner to understand that? I don't think so.

@LilKB: Ignore Vlad's code for now since it's far beyond your level.

Wazzak
Last edited on
I think the more he will see coding the better.

By the way what does mean OTT?
Last edited on
closed account (zb0S216C)
vlad from moscow wrote:
"I think the more he will see coding the better."

Not in this case; the code is far beyond the capabilities of any beginner. Any beginner that reads that will have no clue as to what it does. The STL is an unforgiving place.

vlad from moscow wrote:
"By the way what does mean OTT?"

Over the Top, which basically means "excessive".

Wazzak
OK, I get what you are saying. I changed my cout statements into for loops, and I think I eliminated the problem you were referencing.. But the same error still occurs. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace std;

void displayFunction();

int _tmain(int argc, _TCHAR* argv[])
{
	displayFunction();
	return 0;
}

void displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

	for ( int x = 0; x < 10; x++ ) 
		cout << array_1[x] << ' ' << endl;
	for ( int x = 0; x < 5; x++ ) 
		cout << array_2[x] << ' ' << endl;
}
try changing line 9 to ::displayFunction();

EDIT: and if that doesn't work, try putting your function in a namespace

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
#include "stdafx.h"
#include <iostream>
using namespace std;

namespace space
{
        void displayFunction();
}

int _tmain(int argc, _TCHAR* argv[])
{
	space::displayFunction();
	return 0;
}

void space::displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

	for ( int x = 0; x < 10; x++ ) 
		cout << array_1[x] << ' ' << endl;
	for ( int x = 0; x < 5; x++ ) 
		cout << array_2[x] << ' ' << endl;
}
Last edited on
The code is valid. Only it would be better to place cout << endl; outside the both loops.
If the compiler issue an error then look header "stdafx.h". Did you change it?
Starting to get frustrated here... I tried changing line 9 as you said atropos, also tried putting the function in a namespace. I even tried removing the "stdafx.h" header, but none of it works. I'm still getting error C2660.
This code is valid. You should not use a user defined namespace.

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
#include "stdafx.h"
#include <iostream>
using namespace std;

namespace space
{
        void displayFunction();
}

int _tmain(int argc, _TCHAR* argv[])
{
	space::displayFunction();
	return 0;
}

void space::displayFunction()
{
	int array_1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
	int array_2[5] = { 50, 40, 30, 20, 10};

	for ( int x = 0; x < 10; x++ ) 
		cout << array_1[x] << ' 'l;
	cout << endl; 
	for ( int x = 0; x < 5; x++ ) 
		cout << array_2[x] << ' ';
	cout << endl; 
}


I can guess that maybe you changed stdafx.h.

In any case show the full error message for the code above.
Last edited on
Pages: 12