No matching function for call to function error [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 19 days ago.
Improve this question
When I am trying to call my function, I get the error:
expected expression []. What do I put inside the []? I put count for each array but then another error saying Undefined symbol: modifiedSortGPA(double, int, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, int)
void modifiedSortGPA(double, int, string, int); //function prototype
...
// my arrays
int netID[NUM_ELMTS]; // Original netID array
string major[NUM_ELMTS]; // Original major array
double GPA[NUM_ELMTS]; // Original GPA array
double sortedGPAbyGPA[NUM_ELMTS]; // GPA array, sorted by GPA
int sortedNetIDbyGPA[NUM_ELMTS];
string sortedMajorbyGPA[NUM_ELMTS];
for (int i = 0; i <= count; i++) //populating my arrays
{
GPA[i] = sortedGPAbyGPA[i];
netID[i] = sortedNetIDbyGPA[i];
major[i] = sortedMajorbyGPA[i];
}
modifiedSortGPA(sortedGPAbyGPA[], sortedNetIDbyGPA[], sortedMajorbyGPA[], count); //function call in main
...
void modifiedSortGPA(double array1[], int array2[], string array3[], int size) //function
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array1[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array1[index] < minValue)
{
minValue = array1[index];
minIndex = index;
}
}
array1[minIndex] = array1[startScan];
array1[startScan] = minValue;
}
}
I tried to make sure that there were no typos in my prototype, my function call, and my function definition. I am expecting my function to print the sorted arrays.

I tried to make sure that there were no typos in my prototype, my function call, and my function definition.
And yet, you did not succeed in that, because your function's declaration DOES NOT match your function's definition. The values you are passing into the function DO NOT match the declaration, hence the error.
In your declaration, the 1st parameter takes a single double, the 2nd parameter takes a single int, and the 3rd parameter takes a single string.
But, in your definition, the 1st parameter takes a double[] array, the 2nd parameter takes an int[] array, and the 3rd parameter takes a string[] array.
You need to fix the declaration to match the definition, eg:
void modifiedSortGPA(double[], int[], string[], int); //function prototype
On a side note: arrays are 0-indexed, meaning the 1st index is 0 and the last index is 1 less than the element count. But your for loop that is populating the arrays with data is going past the end of the arrays by 1 element, which is undefined behavior . It is trying to access elements at count as the last index, but the last index is count-1 instead. You need to fix that, by use < instead of <= in the loop condition, eg:
for (int i = 0; i < count; i++) //populating my arrays

Related

Variable not initialized with a constant expression

I'm a novice programmer who is learning C++.
Below are the relevant parts of my code. VSCode throws an error 'size' not initialized with a constant expression, and I'm unclear about the underlying error(s) in which I'm making.
int min, max;
min = max = 0;
for(int i=0;i<a.size();i++){
if(a[i]/int(pow(10,exp))%10>max) max = a[i];
if(a[i]/int(pow(10,exp))%10<min) min = a[i];
}
return std::array<int,2> {min,max};
}
std::vector<int> radixSort(std::vector<int> a){
int xp,maxval;
xp = maxval = 0;
int xpv = pow(10, xp);
for(int i=0;i<a.size();i++){
if(a[i]>maxval) maxval = a[i];
}
while(maxval/pow(10/xp) != 0){
std::array<int, 2> rg = findRange(a,xp);
int min = rg[0];
int size = rg[1]-rg[0]+1; //error at rg[1]: attempt to access storage one position past the end of an array of 1 elements
std::array<int,size> count;// Variable size cannot be used as constant
...
I'd like to clarify a few details:
I've returned an array size 2 with 2 elements {max,min} from my function findRange. So why is it that the compiler only interprets it as one? Changing the function return type to std::vector doesn't seem to fix the problem.
I realized that array sizes need to be fixed at compile time, but when I put const or constexpr infront of a size variable it doesn't fix the problem.
Feel free to point out any other mistakes in my code. Thanks!
EDIT: Changed int i to int i=0 in the first for loop
it is just because of array of size 2 with 2 elements try it with inheritance concept

Calling a C++ function with an array of arrays [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've got a library function I need to call and I'm having trouble with c++ basics for some reason.
The declaration of the function is:
void doSomething(int** values, int length, int width);
It's taking an array of integer arrays. This is fine, I'm having trouble sending getting the data into it.
My data is just 2 integers, say 4 & 5.
In c# I think the syntax would be somethings like: [ [4,5] ]
An array containing an array which contains the two values.
How on earth do you declare this basic structure in C++?
I've tried:
int vals[1][2] = { {4,5} };
doSomething(vals, 1,2);
But the compiler comes back with:
error: no matching function for call to ‘myclass::doSomething(int [1][2], int, int)’
doSomething(vals, 1, 2);
^
src/mysclass.cpp:74:6: note: candidate: void myclass::doSomething(int**, int, int)
This must be simple. There must be a simple way to declare this data to call the function with these values. I'd prefer stack based, if possible.
Parameter int **values denotes a pointer to one pointer (or several consecutive pointers) to one int (or several consecutive ints). The "several consecutive..."-case can be used to represent a "two-dimentional array". Note that values points to an array of pointer values, not to an array of ints. This is different from a data structure like int myArr[10][20], where myArr points to / is an array of arrays of integers.
A simple way to call it is to generate a 1D-array, let an int-pointer point to this array, and pass the address of this pointer:
void test (int** value, int length, int width) {
for (int l=0; l<length; l++) {
for (int w=0; w<width; w++) {
cout << "[" << l << "," << w << "]:" << value[l][w] << endl;
}
}
}
int main() {
int myArr[] = { 4,5 };
int* ptrToMyArr = myArr;
test (&ptrToMyArr,1,sizeof(myArr)/sizeof(int));
}
You could also do something like this:
#include <iostream>
void doSomething(int** value, int length, int width)
{
for (int i = 0; i < length; ++i)
for (int j= 0; j < width; ++j)
std::cout << value[i][j] << std::endl;
}
int main()
{
// array of arrays of int
int arr[2][2] = { { 1,2 },{ 3,4 } };
// convert to array of pointers to int
int *vals[2] = { arr[0], arr[1] };
doSomething(vals, 2, 2);
return 0;
}
https://ideone.com/PgpzK0

Trouble using pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have made an array filled with randomized ints. Im trying to find out the biggest & smalest elements in it by using pointers in two different functions.
But one of my problem is that when I compare to find the lowest element the first element in my array changes to the found min-value.
// Compare to find min value
min = array;
for (int i = 0; i < size; i++)
{
if (*min > *(min + i))
*min = *(min + i);
}
// show array using pointer
ptr = resetArray; // Fill int *ptr with original array
for (int i = 0; i < size; i++)
{
std::cout << *(ptr + i) << std::endl;
}
After this comparment is done it moves the *min value to the begining of the array replacing the first element in it.
Sorry for my bad English!
It looks like you might have a misconception about how pointers work (or maybe it's a simple mistake).
A pointer such as int* is not a variable for keeping track of an int. An int* pointer allows you to access some other variable that keeps track of an int. The pointer "points" at some other memory location; that is what they are for.
In your case, min is pointing to the integer in the array. min is not some separate storage for an integer. The fact that array[i] == *min is not merely because they happen to have equivalent integers; even more than that, they are pointing to the same memory location. If you change one, you change the other since they are the same memory location.
What you probably want to do instead, assuming you want to keep your current loop&pointer style you have, is to have a separate pointer for iterating over the array and set min (not *min) equal to the iterating pointer. That is:
int* iterator;
for(...)
if(*iterator < *min)
min = iterator
iterator += 1;
Personally, I would just access the array with array[i]
You let the pointer min point to the first value of the array, hence this value gets overwritten. Instead you should declare min as of the non-pointer type that array elements are of, e.g.:
auto min = array[0];
for (int i = 1; i < size; i++)
{
if (min > array[i])
min = array[i];
}
In any case, since you use c++, you might consider using the function std::minmax_element.

Implementing own realloc [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I would like to change the size of array when it exceed its limit. I have created a function for that.
void AddElemenet( int i , int value){
if( i > index - 1){
int tmp = index;
double *new_arr;
while( i > tmp){
tmp*=2
}
new_arr = new double[tmp]();
for( int j = 0; j < index ; j++){
new_arr[j] = arr[j];
}
index = tmp;
delete[] arr;
arr = new_arr;
}
arr[i] = value;
}
index refers to the max size of an array ; and arr is dynamicly allocated array using new itself.
The problem is , that i am assigning arr to a local variable that get destroyed. I tried assignign it as refference or pointer using *arr=*new_arr
but nothing worked so far. How can i change the array using this local variable?
The various bugs in your implementation demonstrate why it is almost always a good idea to use the standard library. It would be very simple to adapt std::vector to this interface.
The essence of your problem is the confusion over what index means. (That's a terrible name for a member variable. It says nothing. Index of what? And actually, it's not an index; it's the size of the array. At least, that's what it should be.)
Suppose that your array has 4 elements, so index is 4 (based on the assumption that it is the size of the array). Now you want to AddElement(4, 42);. The condition in if( i > index - 1) is certainly true: i is 4, and index - 1 is 3. So the reallocation block will be entered. However, the first thing you do is tmp = index; while( i > tmp) tmp *= 2;. i is not greater than tmp -- both of them are 4 -- so the loop will never run and tmp will still be 4. Now you allocate a new array with four elements, copy the existing four elements to it, "update" index to 4 (its current value), and delete the old array. Right afterwards, you attempt to set the element with index 4 to 42. But the array only has four elements, so that is Undefined Behaviour.
Since you have not actually changed the size of the array, or the value of index which indicates its size, your later attempt to print the values of the array will stop at its actual size, ignoring the value you modified outside the storage area of the array (which may belong to some other datastructure, so its value is meaningless anyway.)
If you rename index as size and tmp as new_size, the code is much clearer, and the fix is also clear:
if (i >= size) {
size_t new_size = size;
while (i >= new_size) new_size *= 2; /* NOT > */
double* new_array = new double[new_size]();
for (size_t j = 0; j < size; ++j) new_array[j] = array[j];
array = new_array;
size = new_size;
}
array[i] = value;
This would all have been much simpler and less error-prone if you used a std::vector:
class MyVector {
public:
void AddElement(size_t i, double value) {
if (i >= data_.size()) data_.resize(i + 1);
data_[i] = value;
}
/* Many implementation details omitted */
private:
std::vector<double> data_;
}
std::vector <int> list;
void AddElemenet(int value)
{
list.push_back (value);
}
Just use vector's. Array's size is fixed. Vector's size is dynamic.

2 dimensional arrays passed to a function in c++

I'm working on doing calculations in a two dimensional array
but keep getting a nasty error.
i call the function by :
if(checkArray(array))
and try to pass it in like this:
bool checkArray(double array[][10]) //or double *array[][10] to no avail
the error is
error: cannot convert ‘double ()[(((unsigned int)(((int)n) + -0x00000000000000001)) + 1)]’ to ‘double’ for argument ‘1’ to ‘bool checkArray(double*)’
code snippet
//array declaration
int n = 10;
double array[n][n];
//function call to pass in array
while(f != 25)
{
cout<<endl;
cout<<endl;
if(checkArray(array)) //this is the line of the error
{
cout<<"EXIT EXIT EXIT"<<endl;
}
f++;
}
//function declaration
bool checkArray(double *array)//, double newArray[][10])
{
double length = sizeof(array);
for(int i = 0; i < length; i++)
for(int j = 0; j < length;j++)
{
double temp = array[i][j];
}
}
When I look at the error you are getting, I have an impression that your function has got invalid declaration. It looks like as if it would expect only one-dimensional array: double*.
However, your question seems a little unclear for me... Could you paste the function code?
Is this really a direct (if edited) copy of your code?
This line:
int n = 10; double array[n][n];
is not valid C++. You can't declare an array with variable dimensions. This would work:
const int n = 10; double array[n][n];
You want to declare checkArray as:
bool checkArray(double array[][10])
and you absolutely do not want to do this:
double length = sizeof(array);
because that will assign to length the size of a pointer, in bytes (4 or 8.) You need to pass in the number of rows explicitly. Also, you're much better off declaring length as an int, or better yet a size_t.
This seems like a decent resource: http://www.fredosaurus.com/notes-cpp/arrayptr/22twodim.html