0

Consider following class :

class Subject
{

private:
    char* name; // I must use char pointers, it's for school.
    int grade;

public:
    Subject() {
        name = NULL;
        grade = 0;
    }
    Subject(char *n, int g) {
        name = new char[strlen(n)];
        strcpy(name,n);
        grade = g;
    }
    ~Subject() {
        delete name;
    }
    void operator=(const Subject &obj) {
        strcpy(name, obj.name);
        grade = obj.grade;
    }
}

So it is pretty simple data structure with its special functions. I'm new to overload operators so it is probably not correctly implemented. Now, what I try to do is make a simple array of those objects. Consider my main function :

Subject *collection = new Subject[3];
char tmp[100];
int grade;

for(int i = 0 ; i < 3; i ++){

   cin >> tmp;
   cin >> grade;

   collection[i] = new Subject(tmp,grade);
}

This returns error saying no match for operator= in ..etc. So I get that they don't know what to do when they see '=', so I need to define it. How do I do it. Again, point is to make simple list of Subject objects.(I can't use vector, it is for school)

jpw
  • 44,361
  • 6
  • 66
  • 86
nhrnjic6
  • 171
  • 1
  • 1
  • 9
  • 6
    `new Subject` returns a pointer to a dynamically allocated `Subject`. `new Subject[3]` is an array of 3 `Subject` objects (*not* an array of 3 `Subject *`). – crashmstr Oct 20 '15 at 13:09
  • 1
    Is your teacher in the conviction that he is teaching C++, by the way? – Chiel Oct 20 '15 at 13:10
  • 2
    "it is for school" is a bad reason to have to not learn modern C++. – crashmstr Oct 20 '15 at 13:10
  • he believes we should first learn to implement String DS, before using one. – nhrnjic6 Oct 20 '15 at 13:11
  • `name = new char[strlen(n)];` Your array is too short by one. It needs an extra element for the null terminator. Gazillions of duplicates about that kind of thing here. – juanchopanza Oct 20 '15 at 13:11
  • thanks, can you help me. How do I make array of objects – nhrnjic6 Oct 20 '15 at 13:13
  • 1
    Imagine what would `strcpy` inside your `operator =` do if you tried copying a subject called `"Physical education"` in place of a subject called `"Math"`? – Sergey Kalinichenko Oct 20 '15 at 13:14
  • @Chiel: SO is not a forum. And teaching the complexities of pointers so that students understand the merits of a solution like `std::string` is not necessarily a bad idea. You don't appreciate a solution if you have never experienced the problem. A professional C++ programmer should prefer `std::string` **and** have an idea of how it is implemented, or rather **because** he or she has an idea of how it is implemented (including the fact that it does not use `new[]`, for example). – Christian Hackl Oct 20 '15 at 14:01
  • By the way, `delete name` invokes undedined behaviour. It must be `delete[] name`. – Christian Hackl Oct 20 '15 at 14:04

1 Answers1

6
  1. the operator= should not return void:

    Subject& operator=(const Subject &obj){
        grade = obj.grade;
        strcpy(name, obj.name);
        return *this;
    }
    
  2. Subject *collection = new Subject[3]; this creates an array of 3 Subjet objects. Operator new, however, returns a pointer to a new object created on the heap, so:

    collection[i] = new Subject(tmp,grade);
    \___________/   \____________________/
         ^                    ^-----a pointer to a Subject object
         |--object of Subject type
    

    you are trying to assign a pointer to an object, which is not going to work. To make your operator= work, you should write collection[i] = Subject(tmp,grade);

  3. This line strcpy(name, obj.name); will cause undefined behaviour if obj.name is larger than name. You should change the name size accordingly before copying the strings.

  4. It would be a good idea to read about the so-called rule of three. Basically, if your class defines either the destructor, the copy constructor or the copy assignment operator, it probably should define all of them.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105