invalid conversion from 'int' to int* [-fpermissive] - c++

Previously I had this implemented and it worked:
int *train_X = (int *)mxGetData(IN_a);// pointer to 6th argument matrix train_X
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
cout << train_X[6 * i + j] << endl;
}
}
int sizeTrain_X1 = mxGetM(IN_a);
int sizeTrain_X2 = mxGetN(IN_a);
I could even manage to check if i get the correct sizes with the following and it was all good.
cout <<"Training input NumOfCollum:\n"<< sizeTrain_X1 << endl;
cout << "Training input NumOfRows:\n"<<sizeTrain_X2 << endl;
but then when trying my entire program with the following initialization i get a compilation error:
for (int epoch = 0; epoch<training_epochs; epoch++)
{
for (int i = 0; i<train_S; i++)
{
rbm.contrastive_divergence(train_X[i], learning_rate, k);
}
}
Here is the error message:
RBM.cpp: In function ‘void mexFunction(int, mxArray**, int, const
mxArray**)’:
RBM.cpp:570:64: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
RBM.cpp:81:6: error: initializing argument 1 of ‘void RBM::contrastive_divergence(int*, double, int)’ [-fpermissive]
RBM.cpp:615:32: error: invalid types ‘int[int]’ for array subscript

train_X is an int*. When you do train_X[i] you now get an int. contrastive_divergence() though wants an int*. Since you cannot convert an int to an int* you are getting the subsequent error. You either need to pass the address of train_X[i] as &train_X[i] or just pass train_X

Related

Error "argument type f "int" is incompatable with parameter of type "int*" and 'int assignVal(int[])': cannot convert argument 1 from 'int' to 'int[]'

For a class assignment I have to fill multiple array types with random numbers. Here is the code I have but keep getting the above error message:
Error "argument type f "int" is incompatable with parameter of type "int*"
and 'int assignVal(int[])': cannot convert argument 1 from 'int' to 'int[]'
Any help is greatly appreciated, here the code:
#include <iostream>
using namespace std;
const int len = 5;
int assignVal(int array[len]);
int main()
{
double arr1[len];
int arr2[len];
int arr3[len];
assignVal(arr2[len]);
}
int assignVal(int array[len])
{
for (int i = 0; i <= len; i++)
{
array[i] = rand() % 10;
}
}
The problem is your call:
assignVal(arr2[len]);
THis does not do what you think: it calls the function with the len-th element of arr2 array. So you try to pass an int element instead of the array.
Try:
assignVal(arr2);

Converting parts of char array to Int array for calculations

I have an array of structs which contains Formula Name, and Chemical Compound. I need to calculate atomic weight of compound.
struct compounds{
char name[20]; // Dihydrogen Monoxide (Water)
char formula[30]; //H2O
};
const int SIZE = 100;
compounds collection[SIZE];
int formula_1[30];
int k;
I have trouble converting collection.formula to int values to do the actual weight calculations. I trying to use isdigit() to check for numbers in the formula and atoi() from <studio.h> to convert them to integers. My code is as follows:
for (int i=0; i<SIZE; i++){
infile >> collection[i].name >> collection[i].formula;
//cout << collection[i].name << " " << collection[i].formula << endl;
//Convertion CHAR to INT
for(int j=0; j<30; j++){
if(isdigit(collection[i].formula[j])){
k = atoi (collection[i].formula[j]);
}
}
}
//Store in array of INT
for (int m=0; m<30; m++){
formula_1[m] = k;
}
What can be done to convert parts of char array containing numbers to convert them to int?
The Weight Calculation of Water
The error message is:
midterm_q2.cpp: In function 'int main()':
midterm_q2.cpp:40:39: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
k = atoi (collection[i].formula[j]);
^
In file included from midterm_q2.cpp:3:0:
c:\mingw\include\stdlib.h:335:38: note: initializing argument 1 of 'int atoi(const char*)'
_CRTIMP __cdecl __MINGW_NOTHROW int atoi (const char *);

Vectors in C++ , bucket sort : Segmentation fault

In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions.
Code :
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void bucket_sort(vector<float> & , int); //vector is passed by reference
int main(){
vector<float> array;
float element;
int count;
cout << "\nEnter the size of the vector : ";
cin >> count;
cout << "\nEnter the elements into the vector : ";
for(vector<float>::size_type i = 0 ; i < count ; i++){
cin >> element;
array[i].push_back(element);
}
bucket_sort(array , count);
}
void bucket_sort(vector<float> array, int count){
vector<float> bucket;
for(int i = 0 ; i < count ; i++){
int bucket_index = count * array[i];
bucket[bucket_index].push_back(array[i]);
}
for(int i = 0 ; i < count ; i++)
sort(bucket[i].begin() , bucket[i].end());
int index = 0;
for(int i = 0 ; i < count ; i++)
for(int j = 0 ; j < bucket[i].size() ; j++)
array[index++].push_back(bucket[i][j]);
}
Errors :
bucket_sort.cpp: In function ‘int main()’:
bucket_sort.cpp:24:12: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(i)’, which is of non-class type ‘float’
array[i].push_back(element);
^
bucket_sort.cpp: In function ‘void bucket_sort(std::vector<float>, int)’:
bucket_sort.cpp:36:24: error: request for member ‘push_back’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)bucket_index))’, which is of non-class type ‘float’
bucket[bucket_index].push_back(array[i]);
^
bucket_sort.cpp:40:18: error: request for member ‘begin’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
sort(bucket[i].begin() , bucket[i].end());
^
bucket_sort.cpp:40:38: error: request for member ‘end’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
sort(bucket[i].begin() , bucket[i].end());
^
bucket_sort.cpp:45:33: error: request for member ‘size’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
for(int j = 0 ; j < bucket[i].size() ; j++)
^
bucket_sort.cpp:46:19: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)(index ++)))’, which is of non-class type ‘float’
array[index++].push_back(bucket[i][j]);
^
bucket_sort.cpp:46:40: error: invalid types ‘float[int]’ for array subscript
array[index++].push_back(bucket[i][j]);
Edited code :
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void bucket_sort(vector<float> & , int); //vector is passed by reference
int main(){
vector<float> array;
float element;
int count;
cout << "\nEnter the size of the vector : ";
cin >> count;
cout << "\nEnter the elements into the vector : ";
for(int i = 0 ; i < count ; i++){
cin >> element;
array.push_back(element);
}
bucket_sort(array , count);
cout << "\nSorted vector : ";
for(int i = 0 ; i < count ; i++)
cout << array[i] << " ";
}
void bucket_sort(vector<float>& array, int count){
vector<float> bucket[count];
for(int i = 0 ; i < count ; i++){
int bucket_index = count * array[i];
bucket[bucket_index].push_back(array[i]);
}
for(int i = 0 ; i < count ; i++)
sort(bucket[i].begin() , bucket[i].end());
int index = 0;
for(int i = 0 ; i < count ; i++)
for(int j = 0 ; j < bucket[i].size() ; j++)
array.push_back(bucket[i][j]);
}
Edit :
I have introduced the correction with respect to the push_back() but now upon running my code I hit a segmentation fault. Any suggestions?
push_back is method of vector, not element. To append element (with increasing vector size by 1) use array.push_back(123). To assign something to element use array[i] = 123.
If you want to fill vector using assignment, you have to resize vector first: array.resize(count).
To sort, wirte sort(array.begin() , array.end()).
bucket[i][j] is totally incorrect: bucket is one dimensional vector. You probably want bucket to be vector<vector<float>>.
for(int i = 0 ; i < count ; i++){
int bucket_index = count * array[i];
bucket[bucket_index].push_back(array[i]);
}
You have only count elements in bucket array but ask for count * array[i] element.
Your code have many problems.
Main problem is following:
You can not do this:
array[i].push_back(element);
You must do this instead:
array.push_back(element);
Later you do the same:
bucket[bucket_index].push_back(array[i]);
But this time, probably you need just:
bucket[bucket_index] = array[i];
Or if you want just to obtain a copy from vector called "array", you can just do:
bucket = array;
If you post comment what bucket_sort should do, I can provide future explanations.
Finally, I also would suggest you to add:
using MyVector = vector<float>;
Will safe you lots of typing.
Also you might define the function to use the vector by reference, because else it is copied and you probably do not want this:
void bucket_sort(MyVector &array, int count);
Instead of array[i].push_back(element), use array.push_back(element). Element will go at the position automatically as a for loop index.
So, at the end of loop, array will have count number of elements as desired by you.
Use this logic everywhere in the code.

Error: invalid conversion from int to int(*)[4] in function calls

I'm working on a homework project where I have to do a modified preorder traversal through an array-based tree and I need to pass the array to the function in order to use it but I keep getting an error in the function saying that it's an invalid conversion from int to int(*)[4] and I can't seem to figure out why.
Here's a copy of my code:
#include <iostream>
#include <fstream>
char code[10];
void preOrder(int tree[][4], int index, int treeDepth)
{
int tempIndex;
if(tree[index][2] == -1 && tree[index][3] == -1)
{
std::cout << char(tree[index][1]) << ": ";
for(int i = 0; i < treeDepth; i++)
std::cout << code[i];
std::cout << "\n"
}
else
{
for(int i = 0; i < 2; i++)
{
code[treeDepth] = 0;
tempIndex = tree[index][2];
preOrder(tree[index][2], tempIndex, treeDepth + 1);
code[treeDepth] = 1;
tempIndex = tree[index][2];
preOrder(tree[index][3], tempIndex, treeDepth + 1);
}
}
return;
}
int main()
{
int numNodes = 0, i = 0, j = 0;
int root = 0, treeDepth = 0;
int numcols = 4;
std::fstream inFile;
inFile.open("tree.dat");
inFile >> root;
inFile >> numNodes;
int huffmanTree[numNodes][numCols];
for(i = 0; i < numNodes; i++)
for(j = 0; j < numCols; j++)
inFile >> huffmanTree[i][j];
preOrder(huffmanTree, root, treeDepth);
inFile.close();
return 0;
}
Any help would be greatly appreciated.
And also I can't use pointers and I can also declare huffmanTree as global but couldn't figure out how to get that to work either. And also sorry for the way everything it placed. This is my first time posting.
The errors are:
ola.cpp: In function ‘void preOrder(int (*)[4], int, int)’:
ola.cpp:28:53: error: invalid conversion from ‘int’ to ‘int (*)[4]’ [-fpermissive]
ola.cpp:10:6: error: initializing argument 1 of ‘void preOrder(int (*)[4], int, int)’ [-fpermissive]
ola.cpp:31:53: error: invalid conversion from ‘int’ to ‘int (*)[4]’ [-fpermissive]
ola.cpp:10:6: error: initializing argument 1 of ‘void preOrder(int (*)[4], int, int)’ [-fpermissive]
ola.cpp: In function ‘int main()’:
ola.cpp:67:39: error: cannot convert ‘int (*)[(((sizetype)(((ssizetype)numCols) + -1)) + 1)]’ to ‘int (*)[4]’ for argument ‘1’ to ‘void preOrder(int (*)[4], int, int)’
Regarding your error on line 67: in Standard C++ this is not permitted:
int huffmanTree[numNodes][numCols];
Only constant expressions may be used as array dimensions.
Some compilers implement a non-standard extension to allow this sort of array, but not very well: it appears that, for your compiler, this extension doesn't extend to passing the array to a function that expects an array of fixed width.
Possibly it would fix your problem to use const int numcols = 4; instead.
The errors on line 28 and 31 are due to calling preOrder(tree[index][2] . Your first argument is an int but the function expects pointer to array. If you're trying to create a slice of the array starting at some particular row and column - that actually isn't possible. You'd have to pass tree, index, 2, tempIndex, treeDepth + 1 instead, adding 2 more parameters to your function which indicate the starting point; and modifying the function logic accordingly.

Help with type conversion error

When I try to compile my program, I get the following error:
main.cpp: In function ‘int main()’:
main.cpp:67: error: cannot convert ‘int (*)[(((long unsigned int)(((long int)mapSizeY) - 1)) + 1u)]’ to ‘int (*)[10]’ for argument ‘3’ to ‘void initializeMap(int, int, int (*)[10])’
main.cpp:68: error: cannot convert ‘int (*)[(((long unsigned int)(((long int)mapSizeY) - 1)) + 1u)]’ to ‘int (*)[10]’ for argument ‘3’ to ‘void paintMap(int, int, int (*)[10])’
My code looks like this:
#include <iostream>
using namespace std;
void initializeMap(int mapSizeX, int mapSizeY, int map[][10])
{
// Map details:
// 0 = # (wall)
// 1 = space (free space)
// 2 = x (player)
for(int x = 0; x < mapSizeX; x++)
{
map[x][0] = 0;
}
for(int y = 0; y < (mapSizeY - 2); y++)
{
map[0][y] = 0;
for(int x = 0; x < (mapSizeX - 2); x++)
{
map[x][y] = 1;
}
map[mapSizeX][y] = 0;
}
for(int x = 0; x < mapSizeX; x++)
{
map[x][mapSizeY - 1] = 0;
}
}
void paintMap(int mapSizeX, int mapSizeY, int map[][10])
{
for(int y = 0; y < mapSizeY; y++)
{
for(int x = 0; x < mapSizeX; x++)
{
switch(map[x][y])
{
case 0:
cout << "#";
break;
case 1:
cout << " ";
break;
case 2:
cout << "x";
break;
}
cout << map[x][y];
}
cout << endl;
}
}
int main()
{
int mapSizeX = 10;
int mapSizeY = 10;
int map[mapSizeX][mapSizeY];
initializeMap(mapSizeX, mapSizeY, map);
paintMap(mapSizeX, mapSizeY, map);
cout << endl << endl;
return 0;
}
I've spent an hour trying to solve the issue and about twenty minutes searching for a solution. Can any of you help me out?
C++ does not support variable-length arrays, which is what map is in your code. However, some compilers may support it as a non-standard extension. However, it certainly won't be compatible with a function expecting a "standard" array.
If you make mapSizeX and mapSizeY constants, this should work.
Declare mapSizeX and mapSizeY const. Your current code, as is, is basically not wellformed according the C++ language specification which allows only constants as array size specifiers.
The error message is utterly misleading, that's due to the fact that some compiler support this as an extension and the latest C language standard includes it as well.
I tested it here.
Your function calls expect the second dimension to be exactly 10, always. Your code has that dimension in a variable (mapSizeY), which is not guaranteed to be 10, even though you set it a line earlier.
Change mapSizeY to const int so the compiler can optimize it away.
If you make mapSize[X|Y] const in the main method, this should work. Alternatively, as you're passing the dimensions to each of your methods, why not pass the matrix as an int**?
The problem is the variable-length automatic array. One way to fix this is to make the item look the same in main() as it does in initializeMap() and paintMap().
int main()
{
int mapSizeX = 10;
// int mapSizeY = 10;
int map[mapSizeX][10];
initializeMap(mapSizeX, 10, map);
paintMap(mapSizeX, 10, map);
cout << endl << endl;
return 0;
}
One thing I don't suggest is simply declaring MapSize? to be const. Then it looks like the array is variable-length when it really isn't.
You should also be aware that variable-length automatic arrays are a GNU extension to C89 and C++.
Change the definition map[][10] to map[][] or *map in the initializeMap an paintMap functions.