"Argument of type "double *" is incompatible with parameter of type "const int *""

I'm not entirely sure what I did wrong. I know that I apparently shouldn't declare the "dp" pointer as a double but I'm not sure what else to do with it.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

void arrSelectSort(int*[], int);
void showArray(const int[], int);
void showArrPtr(int *[], int);

int main()
{
	int number;

	cout << "Enter the number of donations: ";
	cin >> number;

	double *dp;

	dp = new double[number];

	cout << "Enter your donation values: ";
	cin >> *dp;

	int **parr;

	parr = new int*[number];

	delete[] dp;

	cout << "The donations, sorted in ascending order are: \n";
	showArrPtr(parr, number);

	cout << "The donations, in their original order are: \n";
	showArray(dp, number);
	return 0;
	

}

void arrSelectSort(int *arr[], int size)
{
	int StartScan, minIndex;
	int *minElem;

	for (StartScan = 0; StartScan < (size - 1); StartScan++)
	{
		minIndex = StartScan;
		minElem = arr[StartScan];
		for (int index = StartScan + 1; index < size; index++)
		{
			if (*(arr[index]) < *minElem)
			{
				minElem = arr[index];
				minIndex = index;
			}
		}
		arr[minIndex] = arr[StartScan];
		arr[StartScan] = minElem;
	}
}

void showArrPtr(int *arr[], int size)
{
	for (int count = 0; count < size; count++)
		cout << *(arr[count]) << " ";
	cout << endl;
}
- On lines 6 and 60, you have showArrPtr take an array of integer pointers as the first parameter.

- On line 15, you have 'dp' be a double pointer.

- On line 32, you try to call showArrPtr with dp as the first parameter.


showArrPtr needs a array of integer pointers. You are giving it a double pointer. Those types don't match. So you get an error.


Either change dp to be an array of integer pointers, or change showArrPtr to take a double pointer.



Either change dp to be an array of integer pointers, or change showArrPtr to take a double pointer


How do I do either of these?

EDIT: I think I might have fixed it but now I have two new errors:

"1 unresolved externals"
and
""Error 1 error LNK2019: unresolved external symbol "void __cdecl showArray(double * const,int)" (?showArray@@YAXQANH@Z) referenced in function _main""
Last edited on
"unresolved external error" means you are trying to call a function that has no body.

Likely, you changed the prototype of showArray correctly (on line 6), but forgot to change the actual function definition on line 60 to match it.
I actually changed the one in line 5 from "const int" to "double"...I guess I shouldn't have done that but I don't know what else to do.
Last edited on
The concept is very simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void func(int foo)  // <- func takes an 'int' parameter.  Notice that this is dictated by
   // the 'int' before foo.
{
   // ...
}

int main()
{
    int myvar;  // <- myvar is an 'int'
    func( myvar );  // <- giving an 'int' to a function which expects an 'int'.  OK


    double* ptr;  // <- ptr is a 'pointer to double'
    func( ptr );  // <- giving a 'pointer to double' to a function which expects an 'int'.  ERROR
}


Ask yourself what data you want to work with. Do you want a 'pointer to double'? If yes, then have your function accept that as a parameter.
Last edited on
I'm even more confused now. The assignment was take a code used in the text book (one that I also couldn't get to work in Visual Studio even though I typed it out word for word) and replace two parts so that the program lists the numbers in ascending order, ect. The first part was to be replaced by a dynamic 1 dimensional array. To do that, I'm had to:

- Declare a pointer, ‘dp’
- Use this pointer to create a dynamic array of values. You will need to use the ‘number’ to determine the size of the new array.

I thought I did those steps correctly but "dp" seems to be the problem. I had to replace the second part with a dynamic 1 dimensional array of pointers. To do that, I had to:

- Declare another pointer, ‘parr’. However, this pointer needs to be a pointer to a pointer.
- Use this pointer to create a dynamic array of pointers

I haven't run into many problems with the "parr" thing so I think I did that right. It's basically just "dp" that's giving me problems.

- Declare a pointer, ‘dp’
- Use this pointer to create a dynamic array of values. You will need to use the ‘number’ to determine the size of the new array.

I thought I did those steps correctly



You did:

1
2
3
	double *dp;  // <- dp is a pointer.  CORRECT

	dp = new double[number];  // <- using 'number' to create a dynamic array, and have dp point to it.  CORRECT 



but "dp" seems to be the problem.


dp is fine. The problem is you have mismatching types.

Since my previous post did not clarify it... let me try to explain by backing up and going over some basics:


Every variable has to have a "type". This type indicates what kind of information the variable can contain.

For example:

- int can contain an integer
- char can contain a single character
- double can contain a floating-point numerical value
- string can contain a string of text
etc
etc


Pointers are exactly the same. They are also variables -- but instead of containing a character or an integer, they contain an address. This address typically refers, or "points to" another variable.

For example:

- int* is a pointer (because it has the * on it). So it will contain an address. And whatever variable that is located at that address must be an int, so it would contain an integer.


C++ is a [mostly] strict typed language, so understanding different types is very important. You cannot treat a string as if it were an int, because they are two different types. Likewise, you cannot assign strings to ints, nor ints to strings. The types must match (or otherwise be compatible).



Now when you create a function, you can choose which parameters that function accepts. Coming back to my previous example:

1
2
3
void func(int foo)
{
}


This function takes a single parameter, and that parameter must be an integer because we defined it to be an int.

So now when we call this function, we must give it an integer:

1
2
3
4
5
6
7
8
int main()
{
    int x = 0;
    func(x);  // OK.  x is an int, so this works -- the types match

    string y = "lskjdfs";
    func(y);  // ERROR.  y is a string, and func() can only accept an int! types don't match!
}


See now? When you call a function, you have to give it a type that matches what the function expects.



You are not doing this in your original code:

1
2
3
4
5
void showArray(const int[], int); // <- showArray takes an array of ints for the first param

//...

showArray(dp, number); // <- but 'dp' is an array of DOUBLES, not of ints. 


These are incompatible types, and therefore the compiler is complaining at you.

What you need to do is make the types match. So either change showArray to take an array of doubles -- or change dp to be an array of ints.
So to change showArray to take an array of doubles, would I do this:

 
void showArray(const double[], double);
Last edited on
Yes and no.

1
2
3
4
5
6
void showArray(const double[], double);
//                     ^          ^
//                     |          |
//                     |        This is the 2nd param telling you the size of the array
//                     |
//                   This is the first param, the array itself 


So yes, you now have an array of doubles which is what you want.

But does it make sense for the size of the array to be a double? How can you have 4.3 elements in the array? Shouldn't that be an integer?
So turn the second double into an int? It says everything's done correctly when I compile it but I get the unresolved externals thing when I try to run the program
I get the unresolved externals thing when I try to run the program



I wrote:
"unresolved external error" means you are trying to call a function that has no body.

Likely, you changed the prototype of showArray correctly (on line 6), but forgot to change the actual function definition on line 60 to match it.



Make sure your prototype and function body match.
I've tried about every possible combination that I can think of on line 5 but I'm still getting the same error.
Last edited on
Line 5 probably isn't the problem.

The function exists in 2 places: One where you prototype it (near the start of the file)

And again when you actually give it a body.


You probably are changing just the prototype at the top, and not the body at the bottom. The two have to match.
I thought I was making them match

prototype
void showArray(double [], int);

body?
showArray(dp, number);
Last edited on
// prototype:
 
void showArray( double whatever[], int size ); // <- note 'whatever' and 'size' names are optional here 


// calling the function:
 
showArray( dp, size );


// the function body:
1
2
3
4
void showArray( double whatever[], int size )
{
    // ... code here
}
Well I finally got the program to run. But once it starts loading the numbers in ascending order, I get a message saying console has stopped responding
I'm pretty much out of time now. Hopefully my teacher wont take too many points off for the program not working....I mean the code at least looks right :/

Thanks for your help though. Sorry if I was a little slow. Computer Science isn't my major and I barely know the first thing about it (...it's far from it actually. Why I have to take this class is beyond me)
Last edited on
Topic archived. No new replies allowed.