Here's a piece of my code that I divided. I basically need to create 3 functions:
Fill array with random numbers
Print array on the screen
Is not included in this piece.
My problem is that I get the
C2664 error:cannot convert parameter 1 from int[6][6] to int(*)[].
And I cant figure what's wrong with my code. I would also like to check if how I wrote my pointers to fill and print the array is correct.
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include <iomanip>
#include <array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int *);
void printing(int *);
int c;
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
FillingRandomly(Array);
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
system("PAUSE");
return 0;
}
void FillingRandomly(int *Array) {
*Array = rand()%87 +12;
*Array ++;
}
void printing(int *ArrayPtr) {
int counter = 0;
while(*ArrayPtr<AS*AS) {
cout<<*ArrayPtr;
*ArrayPtr++;
if (*ArrayPtr%AS == 0)
cout << endl << endl;
}
}
Don't use pointers to int, because you want to pass an array, not a pointer, to your functions.
In addition, pass it by reference. To do this, change the declarations for your functions:
void FillingRandomly(int (&Array)[AS][AS]);
void printing(const int (&Array)[AS][AS]);
(the declaration is different for printing because it doesn't need to change the array)
The definition must be written in the same manner:
void FillingRandomly(int (&Array)[AS][AS])
{
...
}
Note also that in C++ arrays are sometimes (or most always - depends on whom you ask) represented by the standard-library classes, like std::vector or std::array. I'll not show the code for using them to keep my answer focused.
Related
I have a c++ program where I need to pass the square root of a number in a for loop.
#include<random>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <omp.h>
using namespace std;
int main()
{
vector<int>inputDataBits(49); // vector of randomly generated input data bits
#ifdef printDebug
std::cout << "the input data bits are" << endl;
std::cout << "-------------------------" << endl << endl;
int var =49;
const int r=(int)sqrt(var);
float input2d[r][r];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
input2d[i][j] = inputDataBits[(j %r) + (i *r)];
std::cout << input2d[i][j] << "\t";
}
std::cout << endl << endl;
}
std::cout << endl << endl;
#endif
return 0;
}
I get an error 'expression must have a constant value'. Is there a way to do this in c++?
This is the purpose of the constexpr keyword (make the value known at compile time).
constexpr int var=49;
constexpr int r=(int)sqrt(var);
Unfortunately, in the documentation sqrt() is not declared as a constexpr function.
Only gcc seems to consider it as constexpr but it is not portable.
The size of an array needs to be known at compile-time.
Instead you can use a std::vector, which has a dynamic size.
std::vector<std::vector<float>> input2d(std::vector<float>(r), r);
The following code inserts only one value to the vector col.
The code is extracted from DBMS code base (for importing files), specifically, it is from 1
The code uses void* to be able to read any field type (int, float, and so on).
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<void*> &col){
reinterpret_cast<std::vector<int>&>(col).push_back( 1);
reinterpret_cast<std::vector<int>&>(col).push_back( 2);
reinterpret_cast<std::vector<int>&>(col).push_back( 13);
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout <<reinterpret_cast<std::vector<int>&> (col)[i] <<endl;
return 0;
}
I am not sure how this code work?
Your code is exhibiting undefined behavior.
std::vector<void*> and std::vector<int> are two completely separate and unrelated types, you can't safely cast between them the way you are, especially since there is no guarantee that void* and int are the same byte size.
Cast the values you are pushing, don't cast the vector itself, eg:
#include <iostream>
#include <vector>
#include <cstdint>
using namespace std;
void add(std::vector<void*> &col) {
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(1)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(2)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(13)));
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << reinterpret_cast<intptr_t>(col[i]) << endl;
return 0;
}
Of course, you really should be using the proper container type to begin with:
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<int> &col) {
col.push_back(1);
col.push_back(2);
col.push_back(13);
}
int main() {
std::vector<int> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << col[i] << endl;
return 0;
}
I extremely need to pass data types to the constructor of classes. I’m working on image processing (till now in Matlab) and now need to use ITK and VTK libraries. In fact, my images are coming from different types and sizes. Unfortunately, I don’t know how to pass a data type to my classes. It’s really painful to have different copies of same classes with different data types!!!
Here is a very simple example to express what I mean and need. In this example, I’m going to create a dynamic array of “int” type. This exaple works well but I don't know how to employ templates. Could you please let me know how I can change the code to be able define any type of dynamic array?
Thanks in advance,
Main
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Myclass.h"
using namespace std;
int main()
{
Myclass c1;
system("pause");
return 0;
}
Class definition:
Myclass.h
class Myclass
{
public:
Myclass();
~Myclass();
private:
int size;
int* my_dynamic_array;
void SetPrivate();
};
Myclass.cpp
#include "stdafx.h"
#include "Myclass.h"
#include <iostream>
using namespace std;
Myclass::Myclass()
{
cout << "Please enter the size of array: ";
cin >> size;
my_dynamic_array = new int[size];
SetPrivate();
}
void Myclass::SetPrivate() {
cout << "\n\nPlease enter " << size << " numbers for the array elements...";
for (int i = 0; i < size; i++) {
cout << "\n\n#" <<i+1<<" : ";
cin >> my_dynamic_array[i];
}
}
Myclass::~Myclass()
{
delete[] my_dynamic_array;
}
Not commenting on whether you could use vector instead of int *my_dynamic_array, I just translated your code into a template. Hope it helps somehow:
template<typename T>
class ImageClass {
public:
ImageClass<T>() {
cout << "Please enter the size of array: ";
cin >> this->size;
my_dynamic_array = new T[this->size];
SetPrivate();
}
void SetPrivate() {
cout << "\n\nPlease enter " << this->size << " numbers for the array elements...";
for (int i = 0; i < this->size; i++) {
cout << "\n\n#" <<i+1<<" : ";
cin >> my_dynamic_array[i];
}
}
protected:
int size;
T *my_dynamic_array;
};
int main() {
ImageClass<int> ic_i;
ImageClass<double> ic_d;
}
I am starting a c++ class having never done anything in the language before, and I'm pretty confused by vectors.I made a toy example and I don't quite understand why the doubles that I put into a vector become vector's. Here is the code:
#include <iostream>
#include <vector>
using namespace std;
vector<double> a;
void func(vector<double> *);
int main()
{
for (int i = 0; i < 100; i++)
{
a.push_back(double(i));
}
func(&a);
return 0;
}
void func(vector<double> *vec)
{
cout << double(vec[0]) << endl;
return;
}
It ends up giving me this error:
error: cannot convert 'vector' to 'double' without
a conversion operator
and I have no idea what that means. What exactly is happening here and how can I cast a vector into a double?
You don't need to mix using pointers * with std::vector.
Method 1 (not recommended):
Change
cout << double(vec[0]) << endl;
to
cout << double((*vec).at(0)) << endl;
Method 2:
Change
void func(vector<double> *vec)
to
void func(vector<double> vec)
or
void func(const vector<double> &vec)
Change
func(&a);
to
func(a);
The reason for the actual error is more due to a misunderstanding of pointers than vectors . The problem is that you are passing a pointer to a vector. If you want to use the vector itself, then you would do:
cout << (*vec)[0] << endl;
* will dereference the pointer to get the actual vector instance rather than an index into an address which is what you were doing before.
But, as pointed out in the other answer, it is safer just to pass the vector by reference (or better yet, const reference):
void func(const vector<double>& vec)
{
cout << vec[0] << endl;
return;
}
then call as:
func(a);
Another way to use vectors is with a std::vector< TYPE >::const_iterator like this;
#include <iostream>
#include <vector>
double func(std::vector<double>::const_iterator iter);
int main()
{
std::vector<double> vec;
for (double i = 0.0; i < 1; i += 0.1){
vec.push_back(i);
}
std::cout<< func(vec.begin()+3) << " "
<< func(vec.begin()+6) << std::endl;
return 0;
}
double func(std::vector<double>::const_iterator iter){
return *iter;
}
outputs;
0.3 0.6
I have this code, but it won't compile and i can't understand what is wrong - i guess the pointering of the vector is not correct.
My idea was to collect some numbers in main() and store them in a vector and array, and then pass the memory address of them to a function, and using a pointers to print the data stored.
I came up with this when i read something about pointers which said that i should use them in order to save memory, so IMO the code below will not copy the contents of the vector and the array but use a pointer to access their location in memory - that's what i want to do.
#include <iostream>
#include <vector>
using namespace std;
void function(vector<int>* a, int *s)
{
cout << "function starts.." << endl;
for(int i=0;i<a->size();i++)
{
cout << a[i] << endl;
cout << s[a[i]] << endl;
}
cout << "function ends..." << endl;
}
int main(void)
{
vector<int> m;
int s[102];
for(int i=0;i<10;i++)
{
m.push_back(i*i);
s[i*i] = i-2;
}
function(&m, &s);
return 0;
}
I receive several errors on compiling, something is wrong.
Please tell me what's wrong with my code and how to fix it. thank you...
You should pass the vector by reference, not by pointer:
void function(vector<int>& a, int *s)
And then
function(m, ...);
Using [] on a pointer to a vector would certainly cause strange problems because it behaves as if a pointed to an array of std::vectors (while it actually only points to one). The vectors itself are never indexed by that. You could also use (*a)[...] to index the vector by the pointer.
if you insist in parsing by pointer then the correct syntax shoulld be:
void function(vector<int>* a, int *s[])
{
cout << "function starts.." << endl;
for(int i=0;i<a->size();i++)
{
cout << (*a)[i] << endl;
cout << (*s)[(*a)[i]] << endl;
}
cout << "function ends..." << endl;
}
First of all in the main program s is a pointer to an int, while m is a vector. Thus the function call should be as follows:
function(&m, s);
Secondly in the function a is a pointer to a vector, so should be indexed as follows: (*a)[i].
However you should really be using const references to pass your vector around:
void function(const vector& a, int *s)
{
..
cout << a[i] << endl;
..
}
And call it like:
function(m, s);
(corrected)
&s is in fact int(*)[102]: pointer to a pointer to an array of 102 items.
You should just say
function(&m, s);
This is because by old C legacy rule, an array is essentially a const pointer to its item with index 0. So s is already int*
This version works:
#include <iostream>
#include <vector>
using namespace std;
void function(const vector<int>& a, int s [102])
{
cout << "function starts.." << endl;
for(int i=0;i<(int)a.size();i++)
{
cout << a [i] << endl;
cout << s[a [i]] << endl;
}
cout << "function ends..." << endl;
}
int main(void)
{
vector<int> m;
int s[102];
for(int i=0;i<10;i++)
{
m.push_back(i*i);
s[i*i] = i-2;
}
function(m, s);
return 0;
}