0

constant variables and functions in the same program cpp

#include<bits/stdc++.h>
using namespace std;

class student
{
public:

    const int roll;
    const string name;

    student (int r,string n)
        :roll(r),name(n)
    {

        cout<<roll<<endl;
        cout<<name<<endl;

    }

    void display()
    {
        cout<<"Disp\n";
    }

};



int main()
{
    student obj(2003081,"ismail");

    student o;   //ERROR
    o.display();

    return 0;
}

I can't understand, why the compiler shows "no matching function for call to 'student::student()' "? Where is the problem and how can I overcome this?

  • Please [don't post images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question), copy-paste text *as text* into your questions. That goes not only for code but also error messages. – Some programmer dude Nov 24 '22 at 07:59
  • 1
    1. [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) 2. [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – n. m. could be an AI Nov 24 '22 at 08:00
  • As for the problem, you attempt to default-construct the object `o`, but the `student` class doesn't have a default constructor (there's no `student::student()` function). – Some programmer dude Nov 24 '22 at 08:00
  • @n.m. pity you don't have a link for unnecessary and/or excessive use of `endl`... :-) – Tony Delroy Nov 24 '22 at 08:03
  • Avoid `const` member variables, they complicate things. E.g. you can't assign one student to another because of them. – HolyBlackCat Nov 24 '22 at 08:06

2 Answers2

0

When you write a class with a custom constructor (in your case, student (int r,string n), the compiler won't also generate a default constructor (i.e. one that doesn't expect any arguments, allowing student o; to compile). It probably wouldn't make sense to have a default constructor for your class, as you need to get values somehow to initialise the const member variables, as you do in your custom constructor. So, it's good that student o; is an error. That said, sometimes there are reasons to want a default-constructible type (for example, some container you want to use may insist on it). Then you have to get rid of the const members and have a way of setting them after the default constructor has run, which is a bit ugly!

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

You don't have a constructor without parameters, so you can't make an object without passing parameters. Overcome it, by either making a default constructor or not making an object without parameters.

The confusing part may be that you can make an object without parameters if you didn't make any constructor, but from the moment a constructor is made, the default constructor must also be made if needed. This can be done with:

student() : roll(0), name("");

Even though that probably doesn't make a lot of sense, so you're best in not using student o;.

stefaanv
  • 14,072
  • 2
  • 31
  • 53