-1

I have a problem with assign object 2d [5][5] array to object.

This is my array definition

class PrintRectangle : public QWidget
{

public:
    bool clicked[5][5] = {};
    teacher *tech;
    perceptron *p;

};

And fragment perceptron class

class perceptron
{
public:
    perceptron& operator=(const perceptron&);
};

When I try to assign object to my perceptron *p

PrintRectangle::PrintRectangle(QWidget *parent) : QWidget(parent)
{
    tech = new teacher(clicked);

    *p = new perceptron[5][5];

    for(int i=0; i<5; i++)
    {
        for(int j=0; j<5; j++)
        {
            p[i][j] = new perceptron();
            p[i][j].randweight();
        }
    }

    double learnConst = 0.1;
    tech->learnPerceptrons(p);
}

I get an error

    E:\Sieci Neuronowe\Perceptron\printrectangle.cpp:10: error: no match for 'operator=' (operand types are 'perceptron' and 'perceptron (*)[5]')
         *p = new perceptron[5][5];
            ^

E:\Sieci Neuronowe\Perceptron\printrectangle.cpp:16: error: no match for 'operator[]' (operand types are 'perceptron' and 'int')
             p[i][j] = new perceptron();
                 ^

I have only

perceptron& perceptron::operator=(const perceptron&){

    return * this;
}

In my perceptron class. How can I correct this? I don't cleary understend pointers.

lukassz
  • 3,135
  • 7
  • 32
  • 72

1 Answers1

1
*p = new perceptron[5][5];

is wrong for the following reasons.

  1. Type mismatch.

    Type of *p is perceptron.
    Type of new perceptron[5][5]; is perception (*)[5].

    There is no conversion from perception (*)[5] to perceptron.

  2. Dereferencing p.

    Dereferencng p, i.e. *p is going to be valid at run time only after you have allocated memory for p.


Solution:

You can fix your memory allocation and type mismatch problems but I strongly recommend using containers from the standard library.

class PrintRectangle : public QWidget
{
  public:

    std::vector<teacher> tech;               // 1D array
    std::vector<std::vector<perceptron>> p;  // 2D array.
};

You can initialize them in the constructor with:

PrintRectangle::PrintRectangle(QWidget *parent) :
   QWidget(parent),
   tech(clicked),
   p(5, std::vector<perceptron>(5))
{
   ...
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Ok, but how can I change my for loop for this? – lukassz Nov 23 '16 at 23:27
  • @lukassz, you can access `tech` like you would access a 1D array and `p` like a 2D array. Take a look at http://en.cppreference.com/w/cpp/container/vector to get started with using `std::vector`. – R Sahu Nov 23 '16 at 23:30
  • Ok, why this is the better way? – lukassz Nov 23 '16 at 23:32
  • @lukassz, it removes the complexity of managing dynamically allocated memory from your code. That's already taken care of by the implementors of the standard library. – R Sahu Nov 23 '16 at 23:34
  • Ok. I assign to `tech` only one element `tech = new teacher(clicked);` it's good to make `tech` vector? – lukassz Nov 23 '16 at 23:36
  • @lukassz, I am not sure what you mean by that. – R Sahu Nov 23 '16 at 23:37
  • Ok, when I use your constructor I get an error `E:\Sieci Neuronowe\Perceptron\printrectangle.cpp:12: error: cannot convert 'bool (*)[5]' to 'teacher*' in initialization p(std::vector(5), 5) ^` – lukassz Nov 23 '16 at 23:39
  • `clicked` is a field in `PrintRectangle` class `bool clicked[5][5] = {};` – lukassz Nov 23 '16 at 23:40
  • @lukassz, now you are bringing up issues that are not mentioned in the original post. That's why we emphasize posting a [mcve]. – R Sahu Nov 23 '16 at 23:42
  • Yes but your code It's not working `E:\Sieci Neuronowe\Perceptron\printrectangle.cpp:11: error: no matching function for call to 'std::vector >::vector(std::vector, int)' p(std::vector(5), 5) ^` – lukassz Nov 23 '16 at 23:44
  • @lukassz, that was a mistake on my part. It's fixed now. – R Sahu Nov 23 '16 at 23:46
  • Ok, it works thanks. Now I sould put `new perceptron();` into vector in loop? – lukassz Nov 23 '16 at 23:48
  • Or I must make `operator=` but I get `E:\Sieci Neuronowe\Perceptron\printrectangle.cpp:24: błąd: no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits >::value_type {aka perceptron}' and 'perceptron*') p[i][j] = new perceptron; ^` – lukassz Nov 23 '16 at 23:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128885/discussion-between-lukassz-and-r-sahu). – lukassz Nov 24 '16 at 00:11