-2

I create a table in c++. I have a class with the table skills. I would like to get memory for the square table with function, but I don't know how I should code the constructor and the function for memory allocation.

I get the size from keyboard and I'd like to give this return statement for an other function that allocates the memory. Table must be an 2-dimensions array[][] or matrix.

#include <iostream>

using namespace std;

class Table {
    unsigned int size;

    public:
        unsigned int GetTableSize();
        unsigned int *GetMemory(unsigned int);
};

unsigned int Table::GetTableSize() {
    cout << "Give size: " << endl;
    cin >> size;
    return size;
}

unsigned int *Table::GetMemory(unsigned int s){
    s = size;
    return new unsigned int[s * s];
}

int main()
{
    Table tab;
    tab.GetTableSize();
    tab.GetMemory();

    return 0;
}

*GetMemory function must return with the memory size of the table. I have problem with tab.GetMemory. I tried tab.*GetMemory as well.

tab*GetMemory: QT creator says: GetMemory is not declared. tab.GetMemory: Qt creator says: not matching function for call 'Table::Getmemory'.

  • `GetTableSize` and `GetMemory` are two pointless member functions. Then there's also `s = size;` and not knowing what function parameters and return values are for. Pick a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – LogicStuff Feb 14 '16 at 17:34
  • "I have a class with the table skills" does not parse. It's not clear what you're asking. Please rewrite this to express your question in a more clear way. – Sam Varshavchik Feb 14 '16 at 17:54
  • GetTableSize get the size from keyboard. After I would like to allocate memory for the Table. The Table is an array. I need memory for size * size (multiplication), because it's a square. –  Feb 14 '16 at 18:02
  • For example GetTable size get 5 from cin. So size = 5. 5 rows and 5 columns. I need memory for 5 * 5 unsigned int. –  Feb 14 '16 at 18:05

1 Answers1

2

First of all your Question is not that clear, I would like to inform regarding the error you are getting in Qt.

In Your Code, You are not passing any value in the below line "tab.GetMemory();"

How ever your function expects an unsigned integer value.

I would like to suggest you that you can modify the below code snippet as follows

Your Code:

unsigned int *Table::GetMemory(unsigned int s){
    s = size;
    return new unsigned int[s * s];
}

Modified Code:

unsigned int *Table::GetMemory(){
    //s = size;
    return new unsigned int[size * size];
}

Hope that Helps.

Sudipta Kumar Sahoo
  • 1,049
  • 8
  • 16
  • Yeah. That works. Sorry for misunderstanding. I just learn coding now. –  Feb 14 '16 at 18:09
  • You should at least mention that this is quite bad design. A `Table` should manage its resources and not just provide a method that returns a pointer to some array that has to be managed outside of the class. Who will delete the array? – 463035818_is_not_an_ai Feb 14 '16 at 18:27
  • You are right tobi303, he has missed a lot of things in that code, but as he has already mentioned, he is just learning coding now. I am sure with time he will learn those as well. – Sudipta Kumar Sahoo Feb 14 '16 at 18:40
  • Well I need learn a lot. So do you say the class Table mustn't include the memory allocation and the GetTableSize things? –  Feb 14 '16 at 18:41