-2

I have a problem.

The compiler keeps warning me for invalid use of the constructor.

All i wanted to do is to create a new course in the class. whats wrong?

int StArray::addCS_Course(int id, int CourseNum, char* CourseName,int HwNum, float HwWeigh, bool Takef, char* BookName){
    int i;
    CS_Course* course;
    if ((CourseNum<0)||(HwNum<0)||(HwWeigh<0)||(HwWeigh>1)) 
        return 0;
    for (i=0;i<StudentNum_;i++){
        if (Arr_[i]->getID()==id) {
            course=(CS_Course*)malloc(sizeof(CS_Course*));
            if (course==NULL)  { 
                fprintf(stderr,"Malloc failed\n");
                exit(0);
            }
            course->CS_Course::CS_Course(CourseNum,CourseName,HwNum,HwWeigh,Takef, BookName);
            if (Arr_[i]->addCS_Course(course)==1)
                return 1;
            else 
            { 
                free(course);
                return 0;
            }
        }
    }
    return 0;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 3
    Use `new`, not `malloc()`! – Fred Larson Jun 02 '14 at 15:21
  • course->CS_Course::CS_Course is not a C++ construction. What are you trying to do - are you creating a list of CS_Course's? – Yulia V Jun 02 '14 at 15:23
  • If you actually have to call a constructor on already-allocated memory, that's called [placement new](http://stackoverflow.com/q/222557/89999). But use plain `new` for the code as shown (or better `std::make_unique(CourseNum, ...)` in C++14). – Michael Urman Jun 02 '14 at 15:27
  • The question is what book or tutorial on C++ is showing code examples using `malloc` and not `new`? I ask since this seems to crop up often on SO, where a beginner is using `malloc`. – PaulMcKenzie Jun 02 '14 at 16:20

2 Answers2

2

To create a new object in C++, you don't do this:

course = (CS_Course*) malloc(...);
course->CS_Course::CS_Course(...);

you do this:

course = new CS_Course(...);

That code looks after both allocating memory and calling the constructor.

You then delete your object with delete course; rather than free(course);

(But as juanchopanza points out in the comments, it's considered bad form to create objects on the heap in C style like this - you should prefer to use standard library containers and avoid the use of new. That's a whole nother discussion - you might want to read a tutorial on modern C++.)

Edit by @RemyLebeau: If you need to construct an object in existing memory, use placement new instead:

buffer = malloc(...);
course = new (buffer) CS_Course(...);

But then you have to call the destructor manually:

course->~CS_Course();
free(buffer);
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 1
    @juanchopanza Without knowing more about `Arr_[i]` that's a tough sell, but stack allocations are definitely well worth keeping in mind. – Michael Urman Jun 02 '14 at 15:30
  • @juanchopanza, isn't that a function declaration? – eerorika Jun 02 '14 at 15:30
  • @user2079303 No, because the stuff between the `()` are arguments. – juanchopanza Jun 02 '14 at 15:31
  • @RemyLebeau: In my opinion your edit (on placement new) is completely out of place here. This is a question from a novice; placement new is a specialist construct that is at best of no relevance and at worst terribly misleading. The novice OP could easily read this and think that placement new is the accepted way to create objects in C++. – RichieHindle Jun 02 '14 at 16:00
1

malloc(sizeof(CS_Course*)) allocates enough space for a pointer to a CS_Course, not a CS_Course itself. If malloc were the right way to dynamically allocate memory for an object, you would need to call it like this:

malloc(sizeof(CS_Course));

However, malloc isn't the right way to do this; in C++, you use new to dynamically allocate memory for objects:

course = new CS_Course; //Use the default constructor

or

//Use constructor with 2 parameters
course = new CS_Course(constructor_param1, constructor_param2);

Of course, if you don't need a pointer, you can (and should) create a CS_Course object like this (generally referred to as allocating on the stack):

CS_Course course; //default constructor
//constructor with 2 parameters
CS_Course course2(constructor_param1, constructor_param2);
Eric Finn
  • 8,629
  • 3
  • 33
  • 42