no matching function for call to [closed] - c++

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
#include <iostream>
using namespace std;
int n;
void displaysum(double mat[n][n])
{
double sum= 0;
for(int j=0;j<n;j++)
sum += mat[j][j];
cout<<"Sum of Diagnols Elements is \n"<<sum;
}
int main()
{
cout << "what are the number of rows or column in the matrix" << endl;
cin >> n;
double matrix[n][n];
for (int row = 0; row < n; row++)
{
for (int column = 0; column < n; column++)
cin >> matrix[row][column];
}
displaysum(matrix)
return 0;
}
I don't understand why I get an error for no matching function to call inXCODE. Even if I try to change the variable in my function prototype it still gives me the same error.

I don't understand why I get an error for no matching function to call inXCODE.
The basic issue is that C++ wants the second dimension to be constant at compile-time for the sake of type-checking. If you want to get around this you'll have to use pointers (AFAIK). You can make this work by changing the declaration of displaysum to
void displaysum(double **mat)
and making appropriate allocations for matrix in the original function.
If you don't like this, well, welcome to C++'s type system. In the function declaration, double mat[n][n] is seen as double (*)[n]. This actually makes sense, but why it doesn't see matrix as being of that type is because n isn't constant. You can change the call
displaysum(matrix);
to this:
displaysum(static_cast<double (*)[n]>(matrix);
and receive the curious error
static_cast from 'double (*)[n]' to 'double (*)[n]' is not allowed
(and that's not the strangest error you'll get out of the type system)

Related

How to a hash an array value in c++ [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 1 year ago.
Improve this question
i got a 5x5 matrix and i need to hash its values.
so i'm trying to hash the 2nd lines and 3rd columns like this
void hashing(int matris[5][5]) {
int x = 0;
int y = 0;
cin >> x;
cin >> y;
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
if ((i==x-1)|| (k==y-1))
{
matris[i][k] = "*";
}
}
}
}
thats the code but i got a error that follows:
"a value of type "const char*" can not be assigned to an entity of type "int""
does anyone knows how to do it ?
When you declare int matrix[5][5] you are telling your program that you want to store integers in that matrix, basically numbers. So the error says that your can't put into a matrix that stores integers, a char like "*", and infact you can't.
Edit: a possible solutions will be changing your matrix from int to string (parsing integers to strings) as #Some Programmer Dude suggested, but that depends on your needs

invalid types ‘int[int]’ for array subscript in 2D array [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 5 years ago.
Improve this question
Working on a project that reads integers from a file and puts them into a 2D array. I tested it with a 1D array but when I tried it with a 2D array I kept getting this error "invalid types ‘int[int]’ for array subscript" in my class called "Image" in this function:
void Image::read(int* arr)
{
//Nested loop that reads the file
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width; k++)
{
inputFile >> arr[i][k]; //Here's where I get the error
}
}
}
And here is my main function:
int main()
{
Image test("colorado1.dat"); //File with the integers
test.setWidth(500);
test.setHeight(500);
int array[test.getHeight()][test.getWidth()];
test.read(array);
//Loop to test if the function worked
for(int i = 0; i < 500; i++)
{
for(int k = 0; k < 500; k++)
{
cout << array[i][k] << " ";
}
}
}
Take a look at this function signature:
void Image::read(int* arr);
Here, you've said that the type of arr is int*, a pointer to an integer. As a result, when you write
arr[i][k]
C++ says "hold on a second... arr is an int *, so arr[i] is an int, and you can't apply the square bracket operator to a pair of int values."
To fix this, you'll need to change up the way that you handle parameters to this function. You could either pass in an int**, which would represent a pointer to a pointer to an int, or consider using something like std::vector<std::vector<int>>, which is safer and easier to use.

C++ For Loop: invalid operands to binary expression [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 8 years ago.
Improve this question
I am trying to copy data from one vector to another but am getting an error , "Invalid operands to binary expression 'int' and 'Card' " when I try to compile the following for loop:
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Would anyone have any suggestions?
I believe what you meant is
for (int i = 0; i <= vecCapacity; i++)
or even more likely
for (int i = 0; i < vecCapacity; i++)
The error message is clear enough: in this loop
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
i has type int while vectorOne[vecCapacity] has type Card and there is no defined operator <= for these types.
So this loop makes no sense.
Maybe you mean
for (int i = 0; i < vecCapacity; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Also take into account that you have to guarantee that the size of vectorTwo is not less than the size of vectorOne or at least vecCapacity.
You could use standard algorithm std::copy declared in header <algorithm>
For example
#include <algorithm>
//...
std::copy( vectorOne, vectorOne + vecCapacity, vectorTwo );
You should be looping from 0 to vectorOne's size.
for (int i = 0; i < vectorOne.size(); i++) { //step 3
vectorTwo[i] = vectorOne[i];
}`
`
Also, if you're doing it this way, make sure vectorTwo is big enough before the loop.
vectorTwo.resize(vectorOne.size());

srand(time(NULL)) not giving strings anymore [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
As a beginner in programming I can not find why srand would just not change the string anymore.
#include <stdlib.h>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int v[100], i, p, n,
srand(time(NULL));
cout<<"Numbers of numbers: "; cin>>n; cout<<endl;
for(i=0; i<=n-1; i++)
v[i]=rand()%100;
cout<<"Numbers: "<<endl;
for(i=0; i<=n-1; i++)
cout<<v[i]<<" ";
for(i=0; i<=(n-1)/2; i++){
p=v[i];
v[i]=v[n-(i+1)];
v[n-(i+1)]=p;
}
cout<<"Reversed numbers: "<<endl;
for(i=0; i<=n-1; i++)
cout<<v[i]<<" ";
return 0;
}
Tried rebuilding it, rewriting it from 1, and such. Even if it did work perfectly before it's just one type of bug that yeah, simply won't work.
Edit: Weird because I copy pasted the beginning and it worked with that... I guess it was my mistake. Not paying enough attention T_T
If you enable more warnings you will see something like
program.cpp:11:1: warning: unused variable 'srand' [-Wunused-variable]
srand(time(NULL));
^
The reason can be seen on the previous line: It ends in a comma
int v[100], i, p, n,
// Note comma here ^
So what the code is doing is declaring a variable named srand, and not calling a function called srand.
Take this as a lesson to always build with extra warnings enabled. Enabling more warnings made this problem very obvious, but it can also give hints about cases of undefined behavior.

Using a vector as a parameter (C++) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to print the even numbers of the first 25 Fibonacci numbers. However, I think I have a problem when using a vector as a parameter for my function below. Do you see what I'm doing wrong?
#include <iostream>
#include <vector>
using namespace std;
int main(){
int j=1, k=1, sum;
vector<int> myvector(25);
for (int i=0; i<25; i++) {
//cout << j << " ";
myvector[i] = j;
sum=j+k;
j=k;
k=sum;
}
findeven(myvector);
system("pause");
}
int findeven (vector<int>){
for (int i = 0, i < 25; i++){
if (vector[i] % 2 == 0){
cout << vector[i];
}
}
else{
}
}
vector<int> is just a type name. You need to name the parameter to be able to use it. You also can't use a type name as variable as you attempt to do in your loop. Fixed code:
int findeven( vector<int> v ) {
if (v[i] % 2 == 0)
cout << v[i];
//...
}
Since you don't change the vector inside the function, it'd be a good idea to pass it by const reference to avoid copying it:
int findeven( const vector<int>& v );
You'll also need to make the function visible before you use it. Right now, it's defined after main function and you'll get an error because you're trying to call it where the compiler hasn't seen its declaration yet. Put it before main (or at least its declaration).