kunnu120 kunnu120 - 3 years ago 136
C++ Question

How to access elements of the array just by having the array's memory location?

So, this is just few methods from my myArray.cpp class. It gives me error on

setSize(orig.getsize());
for(int i = 0; i<getSize();i++)
setData(i,orig.getData(i));


these above code(error - which is non-class type double). can anyone please help me? I'm trying to copy an array to the object array

myArray::myArray(double* orig, int size) {


setSize(orig.getsize());
for(int i = 0; i<getSize();i++)
setData(i,orig.getData(i));

}

void myArray::setSize(int value) {
if (value > 0) {
size = value;
}
}

void myArray::setData(int index, double value) {
if ((index >= 0) && (index < size)) {
arr[index] = value;
} else {
// cout << "NO!" << endl;
}
}

double myArray::getData(int index) const {
if ((index >= 0) && (index < size)) {
return arr[index];
} else {
return arr[size - 1];
}
}


That's my main.cpp class

#include <iostream>
#include "myArray.h"
//#include "myArray.cpp"

using namespace std;



int main (int argc, char **argv)
{
cout << "**************Testing Default Constructor*****************" << endl;
myArray A1;
cout << "A1: ";
A1.print();

cout << "**************Testing Alt Constructor 1*****************" << endl;
myArray A2(5,0);
cout << "A2: ";
A2.print();

cout << "**************Testing init*****************" << endl;
A2.init();
cout << "A2 after init: ";
A2.print();

int size = 5;
double *temp = new double[size];
for(int i = 0; i < size; i++)
{
temp[i] = i;
}

cout << "**************Testing Alt Constructor 2*****************" << endl;
myArray A3(temp, size);
cout << "A3: ";
cout << A3.getSize();
cout << endl;
cout << "Fe";
A3.print();


That's my myArray.cpp class

#ifndef MYARRAY_H_INCLUDED
#define MYARRAY_H_INCLUDED

/***************************************************************************
* myArray class header file
***************************************************************************/

class myArray
{
public:
myArray();
myArray(int,double);
myArray(double*, int);
~myArray();

int getSize() const;
bool equal(const myArray &rhs) const;
void setData(int index, double value);
void insert(int, double);
void remove(int);
double get(int);
void clear();
int find(double);
bool equals(myArray&);
void print() const;
void init();
double getData(int index) const;
// void init();
// void print() const;
void expand();


private:
int size;
double *arr;
void setSize(int value);

};


#endif // MYARRAY_H_INCLUDED


That's my myArray.cpp class where I'm getting the error in the default paramaterized constructor

#include "myArray.h"

#include <iostream>
using namespace std;

myArray::myArray() : size(0) {
// size = 10;
arr = new double [size];
}

/*myArray::myArray(int _size) : size(_size) {
// size = _size;
arr = new double [size];
for (int i = 0; i < size; i++) {
arr[i] = i;
}
} */

myArray::myArray(int _size, double value) : size(_size) {
// size = _size;
arr = new double [size];
for (int i = 0; i < size; i++) {
arr[i] = value;
}
}

/*myArray::myArray(myArray* temp, int size)
{
setSize(temp.getSize());
for (int i = 0; i < getSize(); i++) {
setData(i, temp.getData(i));
}
} */
myArray::myArray(double* orig, int size) {


setSize(orig.getsize());
for(int i = 0; i<getSize();i++)
setData(i,orig.getData(i));
//double arr[size];
// arr = new double[size];
// p = orig;

// for(int i = 0;i<size;i++)
// {
// arr[i] = orig[i];
// cout << arr[i] << " ";
// }

// cout << endl;
// setSize(size);
// for (int i = 0; i < getSize(); i++)
// setData(i, orig.getData(i));

// cout << "hell";
// for (int i = 0; i < size; i++) {
// arr[i] = myArray[i];
// cout << arr[i];
//}
// arr = myArray;
}

myArray::~myArray() {
delete [] arr;
}

int myArray::getSize() const {
return size;
}

void myArray::setSize(int value) {
if (value > 0) {
size = value;
}
}

void myArray::setData(int index, double value) {
if ((index >= 0) && (index < size)) {
arr[index] = value;
} else {
// cout << "NO!" << endl;
}
}

double myArray::getData(int index) const {
if ((index >= 0) && (index < size)) {
return arr[index];
} else {
return arr[size - 1];
}
}

void myArray::print() const {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

void myArray::expand() {
double *localArray = new double[size + 1];

for (int i = 0; i < size; i++) {
localArray[i] = arr[i];
}
localArray[size] = size;

delete [] arr;
setSize(size + 1);
arr = localArray;
// myArray = new int[size];
//
// //Is this a deep-copy or a shallow-copy?
// //Can you replace one with the other?
// //What are the advantages and disadvantages?
// for(int i=0; i < size; i++) {
// myArray[i] = localArray[i];
// }
// delete [] localArray;
}

bool myArray::equal(const myArray& rhs) const {
bool result(true);

if (getSize() != rhs.getSize()) {
result = false;
} else {
for (int i = 0; i < getSize(); i++) {
if (getData(i) != rhs.getData(i)) {
result = false;
}
}
}
return result;
}

void myArray::init()
{
cout << "Enter the " << size << " elements to populate the array " << endl;
for(int i = 0;i<getSize();i++)
{
int value;
cin >> value;
setData(i,value);
}

}

Answer Source

Sorry I somehow I thought you wanted help with a runtime error but you want help with the compile errors.

The compile error in this part of the code

myArray::myArray(double* orig, int size) {
    setSize(orig.getsize());
    for(int i = 0; i<getSize();i++)
        setData(i,orig.getData(i));

is specifically about the orig.getSize() part. orig is of type double* (pointer to double) and pointers do not have member functions only classes do which is why the compiler says: "which is non-class type double"

Actually there is no way in c++ to know from a pointer to how many elements it points but luckily your function already has a parameter size which i guess is meant to pass in the size of the orig array. So that line should be setSize(size);

Now two lines lower you get a similar error on setData(i,orig.getData(i)); orig is still a double* so it still doesn't have member functions. The correct way is setData(i, orig[i]);

EDIT: BTW, i quick look through the rest of your code shows me that your setSize method doesn't allocate an array of appropriate size so you should fix that to.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download