1
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Course
{
    public:
        string name;
        string instructorInCharge;
        int numberOfStudents;
        int totalMarks;
        Course(string u, string v, int p, int q){
            this->name=u;
            this->instructorInCharge=v;
            this->numberOfStudents=p;
            this->totalMarks=q;

        }
    
    vector<int> studentMarks (numberOfStudents);

    

};
class Labs:public Course
{
   
        vector<int>labMarks(numberOfStudents);

};

int main()
{
    Course c("Rahul","Hota",200,300);
    cout<<"hello there";
}

When I compile this code I’m getting the following error:

1.cpp:20:31: error: 'numberOfStudents' is not a type
   20 |     vector<int> studentMarks (numberOfStudents);
      |                               ^~~~~~~~~~~~~~~~
1.cpp:28:29: error: 'numberOfStudents' is not a type
   28 |         vector<int>labMarks(numberOfStudents);
      |                             ^~~~~~~~~~~~~~~~

Please tell me what my mistake is. numberostudents was supposed to be the size of the vector. But not any function.

Dragon
  • 27
  • 1
  • 5
  • 2
    What do you expect that line to mean? – bereal Jan 19 '22 at 09:32
  • `vector studentMarks (numberOfStudents);` Is `numberOfStudents` supposed to be an argument? What type should the argument have? – Some programmer dude Jan 19 '22 at 09:32
  • 1
    Look up most vexing parse. Compiler views `vector studentMarks (numberOfStudents);` as a declaration of a function named `studentMarks()` that accepts an argument of type `numberOfStudents` and returns a `vector`. Remove the bit in `()` entirely, and initialise the vector in the constructor initialiser list. Same in class `Labs`. – Peter Jan 19 '22 at 09:38

3 Answers3

8
vector<int> studentMarks (numberOfStudents);

This is a function declaration. The return type is vector<int>, the name is studentMarks and it accepts a single parameter of type numberOfStudents. Except, as the error points out, numberOfStudents is not a type.

You cannot use parentheses to specify a default member initialiser. You have to either use curly braces:

T member {value};

or "equals" initialiser:

T member = value;

Note however, that you don't initialise numberOfStudents member, but rather assign it later in the constructor body. This assignment is after studentMarks has been initialised, and thus the default member initialiser of studentMarks won't reflect the size that was assigned. Rather, the behaviour of the program would be undefined due to the use of an indeterminate value. This can be fixed by initialising numberOfStudents in the member initialiser list.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • numberofstudents was supposed to be the size of the vector so that it could hold all the marks of the students of particular count numberofstudents, why is it interpreting it as a function – Dragon Jan 19 '22 at 09:42
  • 4
    @SaiRahul It is interpreted as a function declaration because that is the syntax of a function declaration. If you write a function declaration, the compiler cannot know that you didn't intend to write a function declaration. If you intend to define a member with a default member initialiser, then follow the advice in my answer. – eerorika Jan 19 '22 at 09:43
  • @SaiRahul, read this: https://stackoverflow.com/q/18222926/4688321 – kiner_shah Jan 19 '22 at 09:45
2

If you want in-class initialization, you may do this:

vector<int> studentMarks = vector<int>(numberOfStudents);

Now the default constructor will initialize studentMarks with vector<int>(numberOfStudents) when an instance of Course is created.

However, this will cause undefined behavior since numberOfStudents is not initialized before the initialization of studentMarks. You could use the member initalizer list instead:

Course(std::string u, std::string v, int p, int q)
    : name(std::move(u)),
      instructorInCharge(std::move(v)),
      numberOfStudents(p),
      totalMarks(q),
      studentMarks(numberOfStudents)
{
}

and likewise for the Labs class:

class Labs : public Course {
public:
    Labs(std::string u, std::string v, int p, int q)
        : Course(u, v, p, q),  // initialize the base class part of a `Labs`
          labMarks(p)          // initialize the added member variable
    {
    }
    std::vector<int> labMarks;
};
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
digito_evo
  • 3,216
  • 2
  • 14
  • 42
1

You can also use resize method of std::vector.

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Course
{
public:
    string name;
    string instructorInCharge;
    int numberOfStudents;
    int totalMarks;

    
    vector<int> studentMarks;
    
    Course(string u, string v, int p, int q){
        this->name = u;
        this->instructorInCharge = v;
        this->numberOfStudents = p;
        this->totalMarks = q;

        this->studentMarks.resize(numberOfStudents);
    }
};
class Labs : public Course
{
    vector<int> labMarks;
public:
    Labs(string u, string v, int p, int q) : Course(u, v, p, q)
    {
        labMarks.resize(p);
    }
};

int main()
{
    Course c("Rahul","Hota",200,300);
    cout<<"hello there";
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37