-4
  #include < vector >

 using namespace std;

 class Rclass
 {
 public:

       vector<int> ir0T;
       vector<int> ir1T;

 private:
      int f();
 }


 int Rclass::f()
 {
           ir0T.clear();
           ir1T.clear();

           ir0T.push_back(1);
           ir1T.push_back(2);

 }

this throws error

"Rclass.cpp:90: error: member function 'clear' not viable: 'this' argument has type 'const vector', but function is not marked const ir0T.clear(); ^~~~"

Rclass.cpp:91: error: member function 'clear' not viable: 'this' argument has type 'const vector', but function is not marked const ir1T.clear();"

why? ^~~~

I tried adding "const vector ir0T;"

Turbo
  • 77
  • 1
  • 10

4 Answers4

1

You cannot set the matrix member variable to a local varable created in a local member function - the local variable will be destroyed when the function ends and then the matrix member variable won't be pointing to anything. So instead, if you insist on using a raw pointer, use calloc() because it allocates the memory like malloc and then it sets it all to zero. The main problem with this is that then you need a copy constructor, assignment operator and destructor - That's not the way to go if you can help it. It would be better to use a std::vector<std::vector<int>> because all the dynamic allocation and deallocation is hidden from you. Plus you can reserve the size if you know it ahead of time. How to initializ the "vector"-ized version to zero can be seen here: Initializing a two dimensional std::vector

#include <vector>

class CS
{
  private:
    std::vector<std::vector<int> > Rightalpha;

  public:
    void CreateMtrx(int a, int b)
    {
        // Defaults to zero initial value
        Rightalpha = std::vector<std::vector<int> >(a, std::vector<int>(b));
    }
};

int main()
{
    CS cs;
    cs.CreateMtrx(4,4);
    return 0;
};

A better alternative if it is fixed and you know ahead of time how big the matrix is: you can just use a plain array directly as a member variable instead of using a pointers to dynamically allocated memory. If the matrix is small (like 4x4) this will give you cache locality and a performance improvement. Plus if you are using c++11 you can clear the array at the declaration and you don't need a CreateMatrix() member variable at all - something like this:

class CS
{
  private:
    int Rightalpha[4][4] = {};
};

int main()
{
    CS cs;
    return 0;
};

Or like one of the comments suggested you could use std::array instead of a plain array, if you want a nice STL-like interface to the array. There are some advantages listed here: Replace fixed size arrays with std::array?

Community
  • 1
  • 1
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
  • for me the 4 x 4 is passed as a parameter (not a constant as I have simplified in my post) – Turbo Aug 01 '16 at 23:14
0

Firstly a few fundamentals.

When CreateMtrx() returns Rightalpha will become invalid as a will destruct.

And I would recommend using lower camel case naming for variables and upper camel case for types. i.e. rightAlpha instead of Rightalpha, to avoid confusion with types.

As for your actual question you can initialise a 2D array with a nested loop:

for(unsigned int i = 0; i < 4; i++)
{
   for(unsigned int j = 0; j < 4; j++)
   {
      rightAlpha[i][j] = 0;
   }
}

Finally, when asking for help 'craps up' is not conducive to constructive answers. It is important to be clear on what your expected behaviour is and what results you are actually seeing.

  • how do you make such that array will still be valid when we return? – Turbo Aug 01 '16 at 23:00
  • Google is your friend: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/ and http://www.learncpp.com/cpp-tutorial/65-multidimensional-arrays/ – Aaron Baumbach Aug 02 '16 at 13:38
0

If Rightalpha is a data member of your class it doesn't need to be an int**. You probably just want it to be an int[4][4] and skip using a local variable 'a' in your create function.

If you really want it to be a pointer, just make it an int*, and use it with 2D-array syntax. Instead of: int a[4][4]; Do: int* a = new [4*4];

ChrisG0x20
  • 281
  • 1
  • 6
  • I cannot change the variable types – Turbo Aug 01 '16 at 22:53
  • Well, you've got a pointer-to-a-pointer and an array of integers on the stack. If you can't change the variable types, you'll have to write some ridiculously confusing code like: int* p = new int[4*4]; Rightalpha = &p; ... and then copy your values from a to **Rightalpha. Good luck keeping track of that in your head and managing the memory correctly. ;) – ChrisG0x20 Aug 01 '16 at 23:16
  • I feel you are a novice also – Turbo Aug 01 '16 at 23:18
0

I see from the comment that you can't change the type of Rightalpha. You will then need to do manual memory management. You will need to initialize you int** with the new operator.

You will need to allocate each array in the 2D array.

rightAlpha = new int*[4];

for (int i = 0 ; i < 4 ; i++) {
    rightAlpha[i] = new int[4];
}

You can read more about initialisation of a multi-dimentional arrays here:

How do I declare a 2d array in C++ using new?

Even if that works, you will need to free and manage memory and deal carefully with all the pitfalls of manual memory management. That's why I strongly suggest to use a std::vector<int>:

struct CS {
    createMatrix() {
        rightAlpha = std::vector<int>(4*4);
    }

 private:
    std::vector<int> rightAlpha;

With this solution, you don't need to worry about memory stuff as the std::vector will do it for you.

If you need matrix semantics, you can add a function that returns the right element according to a j i position.

int operator()(int i, int j) const {
    return rightAlpha[j+4*i];
}

It may be used like this:

CS myCs;

myCs(3, 2);
Community
  • 1
  • 1
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • finally some sensibility here (may be I asked query bad way?). – Turbo Aug 01 '16 at 23:10
  • also this seems to create array though (hmmm ) not matrix – Turbo Aug 01 '16 at 23:11
  • @Turbo Well, the answer I linked is pretty easy to find. I didn't know how to initialize a 2D array in c++ before I posted that answer. I litterally learned from stackoverflow, to then help you on stackoverflow. It may explain all the downvote. – Guillaume Racicot Aug 01 '16 at 23:13
  • I don't see the difference between a matrix and an array. Both are lists of numbers. If it's a matrix or a 2D array or a simple `std::vector`, the only thing that will make it a matrix is how you manage your list in the code. – Guillaume Racicot Aug 01 '16 at 23:15
  • If you want to directly use a `Matrix` type, I'm sure there's a load of library out there. – Guillaume Racicot Aug 01 '16 at 23:15
  • are you semantically creating an matrix object that can be indexed by rightAlpha[i][j] or or you using rightAlpha[j+4*i]? – Turbo Aug 01 '16 at 23:17
  • Both are valid. You can have a `Matrix` type that has a `[i][j]` semantic, but has a `array[j+4*i]` inside. This is a matter of abstraction. You can as well use my solution with the vector and has a method `get(int i, int j)` that returns `rightAlpha[j+4*i]`. – Guillaume Racicot Aug 01 '16 at 23:17
  • cool so I can use your method as int c= rightAlpha[i][j] instead of int c=rightAlpha[j+4*i]? that is the requirement – Turbo Aug 01 '16 at 23:23