Parallel_for instance of class member function - c++

I have a class that looks like this:
class testy {
int *arr1, *arr2, *resu;
int n;
testy() {
n = 100000000;
arr1 = new int[n];
arr2 = new int[n];
resu = new int[n];
for (int i = 0; i < n; i++) {
arr1[i] = i;
arr2[i] = -i;
}
}
void worker(int Idx) {
resu[Idx] = arr1[Idx] + arr2[Idx];
}
void doTest() {
parallel_for(0, n, worker);
}
};
however I cant compile it since "testy::worker function call missing argument list"
the test worked fine while the worker wasn't a member of the class so I'm guessing I need to specify class instance here or something? How would I do that?

Related

Problem with printing copy constructor OOP

Having trouble with the printing copy constructors elements from Arr s to Arr s1 , also having troubles with << operator it doesn't work, please help cuz I am not pro just a student.
#include <iostream>
using namespace std;
class Arr {
private:
int size;
int *arr;
public:
Arr(int size,int *arr) {
this->size = size;
this->arr = new int [size];
for (int i = 0; i < size ; ++i) {
this->arr[i] = arr[i];
}
}
~Arr() {
delete[] arr;
}
Arr(const Arr &x) {
this->size = x.size;
this->arr = new int[x.size];
for (int i = 0; i < x.size; ++i) {
this->arr = x.arr;
}
}
Arr() : size(0),arr(0) {}
Arr(Arr &&x) {
this->size = x.size;
this->arr = new int[x.size];
for (int i = 0; i < x.size; ++i) {
this->arr = x.arr;
}
}
friend ostream &operator<<(ostream &out, const Arr &t) {
out << t.size;
for (int i = 0; i < t.size; i++) {
out << t.arr[i];
}
return out;
}
friend istream &operator>>(istream &in, Arr &t) {
in >> t.size;
for (int i = 0; i < t.size; ++i) {
in >> t.arr[i];
}
return in;
}
};
int main() {
int size = 3;
int arr [] = {1,2,3};
Arr s1(size,arr);
cin << s1;
cout<<s1;
Arr s(s1);
cout<<s; // not working at all
return 0;
}
You are writing
this->arr = x.arr;
in the copy constructor, rather than
this->arr[i] = x.arr[i];
That is, currently your code is copying the array pointer, not the elements of the array.
When you're debugging, first look over your code to make sure each part is doing what you want it to do and that you didn't make a typo like this. Then, if you don't find the problem, you can use a debugger like gdb to step through your code and monitor the values of the variables to make sure your program is doing what you'd like.

How to initialize dynamic array inside a class?

I wish to initialize a multidimensional, dynamic array inside a class. But, I am getting an error.
I have seen several examples on the net. They seem to be difficult. I am new to coding. I would like a simple solution if possible.
class myp
{
int ntc = 5;
public:
double** y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new int[3];
};
int main()
{
int x;
myp mp;
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;;
return 0;
}
test.cpp:12:2: error: expected unqualified-id before ‘for’
for(int i = 0; i < ntc; i++)
^~~
test.cpp:12:17: error: ‘i’ does not name a type
for(int i = 0; i < ntc; i++)
^
test.cpp:12:26: error: ‘i’ does not name a type
for(int i = 0; i < ntc; i++)
You need to do class initialisation in the constructor function, and cleanup in the destructor.
class myp
{
int m_numColumns;
int m_numRows;
double** y;
public:
// overload array operators
double* operator [] (size_t row) { return y[row]; }
const double* operator [] (size_t row) const { return y[row]; }
// return dimensions of array
int numColumns() const { return m_numColumns; }
int numRows() const { return m_numRows; }
// constructor
myp(int nc, int nr) : m_numColumns(nc), m_numRows(nr)
{
y = new double*[m_numRows];
for(int i = 0; i < m_numColumns; ++i)
y[i] = new int[m_numColumns];
}
// destructor
~myp()
{
for(int i = 0; i < m_numColumns; ++i)
delete [] y[i];
delete [] y;
}
// be careful of the copy ctor. I'm deleting it in this case!
myp(const myp&) = delete;
// edit: as per user4581301's suggestion
myp() = delete;
myp(myp&&) = delete; // remove move ctor
myp& operator = (const myp&) = delete; // remove assignment
myp& operator = (myp&&) = delete; // remove move assignment
};
int main()
{
myp mp(5, 3);
mp[1][1] = 3;
cout << mp[1][1]<<endl;
return 0;
}
Just For Run.
class myp
{
int ntc = 5;
public:
double **y;
void initArray()
{
y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new double[3]; // i change this line [new int] to [new double]tv
}
};
int main()
{
int x;
myp mp;
mp.initArray();
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;;
return 0;
}
using constructor & destructor
class myp
{
int ntc = 5;
public:
double **y;
myp() // run at created
{
y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new double[3];
}
~myp() // run at the end of life cycle
{
/* free memory here */
}
};
int main()
{
int x;
myp mp; // myp() called
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;
return 0;
}
using constructor with parameter, for dynamic size
class myp
{
// int ntc = 5; // using at created
public:
double **y;
myp(int ntc, int size) // run at created
// if you want to use only myp mp;
// myp(int ntc = 5, int size = 3) {} will be helpful
{
y = new double*[ntc];
for(int i = 0; i < ntc; ++i)
y[i] = new double[size];
}
~myp() // run at the end of life cycle
{
/* free memory here */
}
};
int main()
{
int x;
myp mp(5, 3); // myp(int, int) called
mp.y[1][1] = 3;
cout<<mp.y[1][1]<<endl;
return 0;
}

How to avoid returning pointers in a class

Assume I have a class A that has say 3 methods. So the first methods assigns some values to the first array and the rest of the methods in order modify what is computed by the previous method. Since I wanted to avoid designing the methods that return an array (pointer to local variable) I picked 3 data member and store the intermediate result in each of them. Please note that this simple code is used for illustration.
class A
{
public: // for now how the class members should be accessed isn't important
int * a, *b, *c;
A(int size)
{
a = new int [size];
b = new int [size];
c = new int [size];
}
void func_a()
{
int j = 1;
for int(i = 0; i < size; i++)
a[i] = j++; // assign different values
}
void func_b()
{
int k = 6;
for (int i = 0; i < size; i++)
b[i] = a[i] * (k++);
}
void func_c()
{
int p = 6;
for int (i = 0; i < size; i++)
c[i] = b[i] * (p++);
}
};
Clearly, if I have more methods I have to have more data members.
** I'd like to know how I can re-design the class (having methods that return some values and) at the same time, the class does not have the any of two issues (returning pointers and have many data member to store the intermediate values)
There are two possibilities. If you want each function to return a new array of values, you can write the following:
std::vector<int> func_a(std::vector<int> vec){
int j = 1;
for (auto& e : vec) {
e = j++;
}
return vec;
}
std::vector<int> func_b(std::vector<int> vec){
int j = 6;
for (auto& e : vec) {
e *= j++;
}
return vec;
}
std::vector<int> func_c(std::vector<int> vec){
//same as func_b
}
int main() {
std::vector<int> vec(10);
auto a=func_a(vec);
auto b=func_b(a);
auto c=func_c(b);
//or in one line
auto r = func_c(func_b(func_a(std::vector<int>(10))));
}
Or you can apply each function to the same vector:
void apply_func_a(std::vector<int>& vec){
int j = 1;
for (auto& e : vec) {
e = j++;
}
}
void apply_func_b(std::vector<int>& vec){
int j = 6;
for (auto& e : vec) {
e *= j++;
}
}
void apply_func_c(std::vector<int>& vec){
// same as apply_func_b
}
int main() {
std::vector<int> vec(10);
apply_func_a(vec);
apply_func_b(vec);
apply_func_c(vec);
}
I'm not a big fan of the third version (passing the input parameter as the output):
std::vector<int>& func_a(std::vector<int>& vec)
Most importantly, try to avoid C-style arrays and use std::vector or std::array, and don't use new, but std::make_unique and std::make_shared
I'm assuming you want to be able to modify a single array with no class-level attributes and without returning any pointers. Your above code can be modified to be a single function, but I've kept it as 3 to more closely match your code.
void func_a(int[] arr, int size){
for(int i = 0; i < size; i++)
arr[i] = i+1;
}
void func_b(int[] arr, int size){
int k = 6;
for(int i = 0; i < size; i++)
arr[i] *= (k+i);
}
//this function is exactly like func_b so it is really unnecessary
void func_c(int[] arr, int size){
int p = 6;
for(int i = 0; i < size; i++)
arr[i] *= (p+i);
}
But if you just want a single function:
void func(int[] arr, int size){
int j = 6;
for(int i = 0; i < size; i++)
arr[i] = (i+1) * (j+i) * (j+i);
}
This solution in other answers is better, if you are going to allocate memory then do it like this (and test it!) also if you are not using the default constructor and copy constructor then hide them, this will prevent calling them by accident
class A{
private:
A(const &A){}
A() {}//either define these or hide them as private
public:
int * a, *b, *c;
int size;
A(int sz) {
size = sz;
a = new int[size];
b = new int[size];
c = new int[size];
}
~A()
{
delete[]a;
delete[]b;
delete[]c;
}
//...
};

What is the meaning of `A vec*[5] = new B*[5]`

Array memory Allocation doesn't work
I saw the following code and found that it doesn't compile.
Is the code in the OP correct?
Thank you
class A {
};
class B : public A {
int num;
};
int main() {
/* Original Post
error: expected initializer before ‘*’ token
A vec*[5] = new B*[5];
A vec*[5] = new B*[5]; // <<< I don't understand this line
for(int i = 0; i < 5; i++)
{
vec[i] = new B();
}
*/
// My modified version
A* vec[5];
for(int i = 0; i < 5; i++)
{
vec[i] = new B();
}
return 0;
}

How to overload [][] operator for the class representing dynamically allocated 2d array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Operator[][] overload
I have made class which contains an array containing (in one row) all the numbers from the given 2d array. For example given: {{1,2}{3,4}} the b field in the object of class T contains {1,2,3,4}. I would like to overload[][] operator for this class so it will work like that
T* t.....new etc.
int val = (*t)[i][j]; //I get t->b[i*j + j] b is an 1dimension array
class T{
public:
int* b;
int m, n;
T(int** a, int m, int n){
b = new int[m*n];
this->m = m;
this->n = n;
int counter = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
b[counter] = a[i][j];
counter++;
}
}
}
int main()
{
int m = 3, n = 5, c = 0;
int** tab = new int*[m];
for(int i = 0; i < m; i++)
tab[i] = new int[n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
tab[i][j] = c;
c++;
cout<<tab[i][j]<<"\t";
}
cout<<"\n";
}
T* t = new T(tab,3,5);
};
You cannot. You have to overload operator[] to return a proxy object, that in turn, overloads operator[] to return the final value.
Something like:
class TRow
{
public:
TRow(T &t, int r)
:m_t(t), m_r(r)
{}
int operator[](int c)
{
return m_t.tab[m_t.n*m_r + c];
}
private:
T &m_t;
int m_r;
};
class T
{
friend class TRow;
/*...*/
public:
TRow operator[](int r)
{
return TRow(*this, r);
}
};
Instead of saving a T& in TRow you could save directly a pointer to the row, that's up to you.
A nice feature of this solution is that you can use the TRow for other things such as operator int*().
In the case of a 2d array, you don't need to create a proxy type. Just use int*:
#include <iostream>
class T {
public:
int m, n;
int *b;
T(int m, int n) : m(m), n(n), b(new int[m*n]) {
}
int*operator[](std::size_t i) {
return &b[i*m];
}
};
int main () {
T t(2,2);
t[0][0] = 1;
t[0][1] = 2;
t[1][0] = 3;
t[1][1] = 4;
std::cout << t.b[0] << t.b[1] << t.b[2] << t.b[3] << "\n";
}