-1
class Student{
public:
    int sNumber;
    int wantedCoursesNumber;
    int qStudent;
    int courselist;
    Student(int cN,int qS,int i){
        this->sNumber = i;
        this->wantedCoursesNumber = cN;
        this->qStudent = qS;
        this->courselist = new int[cN];
    }

when i create this class,it gives the error "invalid conversion from 'int*' to 'int' [-fpermissive]" for courselist.I just want to create in every student object an array that has the size of cN can anyone help me , i'm new in c++

McOne
  • 3
  • 3
  • 1
    Should be `int* courselist;` – VladimirM Dec 13 '13 at 21:26
  • courselist is of type `int` it needs to be a pointer of ints. And you need to save the array size somewhere, otherwise you will probably segfault. – Tim Seguine Dec 13 '13 at 21:26
  • Thx a lot what is the difference between int* and int ? – McOne Dec 13 '13 at 21:27
  • @McOne, `int` is just an ìnteger, `int*` is a *pointer* to an integer. – kviiri Dec 13 '13 at 21:30
  • 5
    vector vector vector, please do yourself a favor: use `std::vector`. – stefan Dec 13 '13 at 21:32
  • what is pointer anyway, I know i should search for this but when I searched, it seemed complicated,is there a simple explaination for pointers in c++ ? Is there any difference between int* courselist and int *courselist ? because it compiled both ways. – McOne Dec 13 '13 at 21:34
  • Take the pointer answers with extra caution. Neither mention the Rule of Three. Anyway, there are tons of pointer resources and also tons of vector resources. – chris Dec 13 '13 at 21:48
  • If you dont know about pointers then (a) read up on them, and (b) dont ever ever use them. Look at the vector answer below. – RichardPlunkett Dec 13 '13 at 21:49
  • I thought like since I know what size the list will be , it would be faster to use arrays when I want to reach an element in the list but I might have thought wrong.I have a project to do in 2 days , I didn't want to waste time by learning pointers thinking that it would have most of my time but I know how to use vectors, should I use vector or array in case I know what the size is at the beginning,because the time of program is also important in project.Btw how am I going to mark these comments as accepted. – McOne Dec 13 '13 at 22:47

3 Answers3

6

Avoid the memory leak operator—aka new[]. Instead, C++ has a data type called std::vector<int> which allows you to create arrays of integers of dynamic size.

#include <vector>

class Student {
public:
    int sNumber;
    int wantedCoursesNumber;
    int qStudent;
    std::vector<int> courselist;

    Student(int cN, int qS, int i)
        : sNumber(i)
        , wantedCoursesNumber(cn)
        , qStudent(qS)
        , courseList(cN) {

    }
};

Since you’re new (pun not intended) to C++, I highly recommend The Definitive C++ Book Guide and List.

Community
  • 1
  • 1
  • 1
    @OneOfOne, Idk about proper. Vectors have overhead. It would be nice if this post mentioned/explained RAII, initialization lists or destructors. These are skills that need to be learned even if your good at avoiding there use. – 8bitwide Dec 13 '13 at 21:54
0
class Student{
public:
int sNumber;
int wantedCoursesNumber;
int qStudent;
int *courselist;
Student(int cN,int qS,int i){
    this->sNumber = i;
    this->wantedCoursesNumber = cN;
    this->qStudent = qS;
    this->courselist = new int[cN];
}

change veriable to pionter

Hammad
  • 101
  • 1
  • 10
-1

You are assigning an int array to courselist, which fails because courselist is declared as int, not int* (a pointer to int). Try declaring the variable like this:

int* courselist;

kviiri
  • 3,282
  • 1
  • 21
  • 30