-1

So I was trying implement a particular class with this declaration:

class Student
{
    public:
        Student ();
        Student (int, int);
        ~Student ();
        void setMod (int, int);
        void setId (int);
        void setScore (int);
        int getId () const;
        int getScore () const;
        void print () const;

    private:
        int idNo, score, mod;
};

Student::Student ()
{
    idNo = -999;
    score = -999;
}

Student::Student (int idNo, int score)
{
    this -> idNo = idNo;
    this -> score = score;
}

Student::~Student ()
{
    static int i = 0;
}

void Student::setMod (int idNo, int size)
{
    this -> mod = idNo % size;
}

void Student::setId (int idNo)
{
    this -> idNo = idNo;
}

void Student::setScore (int score)
{
    this -> score = score;
}

int Student::getId () const
{
    return idNo;
}

int Student::getScore () const
{
    return score;
}

void Student::print () const
{
    cout << idNo << "\t\t" << mod << "\t" << score << endl; 
}

And then I have problem with these implementation:

1.

if (table [k].getId () == -999)
        {
            table [k].setId(idNo);
            table [k].setScore(score);
            table [k].setMod(idNo, score);
        }

2.

   Student *table;
   int tSize = 20;
   table = new Student [tSize];
        for (int i = 0; i < tSize; i++)
            table [i] (-999, -999);

My problems are:

at 1, I got error: request for member 'method', which is of non-class type 'int'.

at 2, I got error: no match to call (class) (int, int)

can someone help me understand what I'm doing wrong here? Noted that I haven't covered vector yet, so I'm forced to use an array of objects.

Kay
  • 1
  • 1
  • 1
    dead wrong. you need to read a textbook first. – Jason Hu Aug 21 '14 at 13:44
  • You can't call a constructor after you have created an object. As for the first error, *which line* do you get the error on? And what is the *exact* error? – Some programmer dude Aug 21 '14 at 13:46
  • Your table doesn't point to anything. – Neil Kirk Aug 21 '14 at 13:47
  • I would have to agree with the first comment. You should probably read a bit more than start from scratch. There a numerous errors here. In 2 alone, you have created a pointer, than tried to write into that memory block, without allocating the memory, with what i can assume are new students, although that's not how you would create new objects. – pseudonym Aug 21 '14 at 13:56
  • @JoachimPileborg I get that error for each 3 lines I tried to call a function from the class. You're all correct, I'm a new student. Are there any good text books I should be looking for? I'm supposed to be working with classes and objects for now. – Kay Aug 22 '14 at 02:53
  • See [this question](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for C++ book recommendations. – Stephan Aug 22 '14 at 12:38

3 Answers3

2

The second error is related to this line:

table [i] (-999, -999);

You call Student::operator()(int, int) which does not exist.

I suppose you want to assign a new Student to the array element at i. This could be achieved by

table [i] = Student(-999, -999);

But this would lead to runtime errors because table is not initialized. I would suggest to use an std::vector instead of a dynamic array. Your whole table initialization would look like this:

std::vector<Student> table(20, Student(-999, -999));

Another advantage is that you don't have to worry about freeing the resources (delete[] table; is not necessary)

Stephan
  • 4,187
  • 1
  • 21
  • 33
  • shldn't it be *table[i] = Student(-999,-999)? – lakshmen Aug 21 '14 at 14:26
  • @lakesh, no. `table[i]` returns a reference (`Student&`) and not a pointer. No need to dereference anything. – Stephan Aug 21 '14 at 14:45
  • sorry, forgot to put in the initialization, this is a code fragment from my files. Updated question to reflect that. Thanks for the `table [i] = Student (-999, -999)` tip. – Kay Aug 22 '14 at 12:07
0

Change the second example to

#include <vector>
...
std::vector<Student> table;
int tSize = 20;
for (int i = 0; i < tSize; i++)
    table.push_back( Student(-999, -999));

or, if you can't use vector:

int tSize = 20;
Student *table = new Student[tSize];
for (int i = 0; i < tSize; i++)
    table[i] = Student(-999, -999);
...
delete[] table;

The first one looks OK, if table is declared as in the second.

Henrik
  • 23,186
  • 6
  • 42
  • 92
  • I can't use vector in this implementation, but I'll take that into consideration. Also i've updated the missinng details in my question, please check it uot if you can. – Kay Aug 22 '14 at 11:34
0

This is a working main that uses the Student class that you've defined:

int main() {
     int totNumStudent = 20;
     Student* table = new Student[totNumStudent];

     // Your table contains all the students initialized using the constructor
     // with no-parameters

     for(int i = 0; i < totNumStudent; i++) 
         std::cout << table[i].getId() << std::endl;

     delete [] table; // deallocate the memory for the table
}

I think that you need to read a well-written book for C++. There are tons of them.

I feel to suggest to you Accelerated C++ which is pretty simple and let you to understand the basic C++'s topics.

Alessandro Suglia
  • 1,907
  • 1
  • 16
  • 23
  • I forgot to add the allocation in the edited code, thanks for the head up. I'll check out the book if I ever get my hand on it. – Kay Aug 22 '14 at 03:39