-3

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);
    }

}
help
  • 5
  • 4
  • 1
    Please provide minimal working code that reproduces the problem, see http://stackoverflow.com/help/mcve – Eelke Oct 01 '17 at 06:32
  • @Eelke I did it please answer now – help Oct 01 '17 at 06:42
  • 1
    Please read [mcve] and [ask] – juanchopanza Oct 01 '17 at 06:52
  • @kunnu120 `setSize(orig.getsize());` is invalid, due to the fact that `orig` is declared as pointer to `double`, **not** as an object type, hence it can't have a method named `getsize`. Consider learning from [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), so you wouldn't make such assumptions in the future. – Algirdas Preidžius Oct 01 '17 at 06:54
  • @AlgirdasPreidžius can you please tell me the right way then please thank you – help Oct 01 '17 at 07:05
  • @AlgirdasPreidžius what if I set the setSize(size) it works fine but how should I setData() please explain thanks – help Oct 01 '17 at 07:06
  • @kunnu120 "_how should I <...>_" Everything depends on what you want to do, which you didn't explain. So that's why I directed you to the [list of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) so you could learn the language, instead of trying to code randomly. SO is **not** a tutorial service. – Algirdas Preidžius Oct 01 '17 at 17:20

1 Answers1

0

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.

Eelke
  • 20,897
  • 4
  • 50
  • 76
  • I tried what you said and it compiles but when I run it says folder.exe stopped working the part until "cout << "**************Testing Alt Constructor 2*****************" << endl;" this works fine – help Oct 01 '17 at 07:12
  • Have you seen my edit (last alinea)? I mentioned your setSize is not correct. – Eelke Oct 01 '17 at 07:14
  • I'm sorry but I don't know what it means can you please show me how? – help Oct 01 '17 at 07:16
  • Yes now I got it Thank you so much – help Oct 01 '17 at 07:18