0
#include <iostream>
#include <memory>
#include <initializer_list>
#include <cassert>
#include <map>


template <typename T>
class Matrix
{
    private:
    int i,j;
    T value;
    std::map<std::array<int, 2>,double> Mat;
    public:


    Matrix():i(0),j(0)
    {
        std::cout<<"Empty boi called"<<std::endl;
    }
    Matrix(int row, int column)
    {
       std::cout<<"Main boi called"<<std::endl;
    }
// I'm not really sure where to plug this code in
// In the main function, this code works but not really in class
    for (std::map<std::array<int, 2>,int>::iterator it=Mat.begin(); it!=Mat.end(); ++it)
       {
        i   = it->first[0];
        j   = it->first[1];
        value = it->second;  
         std::cout << "["<< it->first[0] <<","<<it->first[1]<<"]"<< " => " << it->second << '\n';
       }
};


int main()
{
    std::map<std::array<int, 2>,int> Mat;
    Mat[{1,5}]=20;
    Mat[{2,6}]=30;
    for (std::map<std::array<int, 2>,int>::iterator it=Mat.begin(); it!=Mat.end(); ++it)
    std::cout << "["<< it->first[0] <<","<<it->first[1]<<"]"<< " => " << it->second << '\n';

// The same code done in main function is what I want to do in the class

    Matrix<double> M(10,10);
    M{[1,1]} = 3.14; // This returns an error
    return 0;
}

As seen, the mapping is possible when its directly in the main function, but I want to send multiple positions of M to the class and have it initialised. But completely unable to do so. Tried looking in a lot of places to no avail.

  • you cannot simply put code in class scope. You have to put it inside a member function – 463035818_is_not_an_ai Feb 25 '20 at 12:35
  • 3
    I recommend that you pick up [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read about *operator overloading*. Then you could do something like `M[{1,1}] = 3.14` – Some programmer dude Feb 25 '20 at 12:35
  • 1
    "I'm not really sure where to plug this code in" where do you want to put it? Perhaps in a method called `print` ? – 463035818_is_not_an_ai Feb 25 '20 at 12:36
  • If you are not planning to become a professional too soon, there are various "crash courses"/tutorials for C++ out there on the internet. Surely not for deep understanding like some books, but will give you a good start for issues like those. – AlexGeorg Feb 25 '20 at 12:50
  • Aside: `template class Matrix { ... T value; std::map,double> Mat;` is suspicious. I would expect the *elements of `Mat`* to be `T`. You currently have *exactly one* `T` in every `Matrix` – Caleth Feb 25 '20 at 13:32

0 Answers0