-3

I try to implement basic class of student in the following way:

class Student
{
    public:
        Student(std::string name_ , int const id_);
        virtual ~Student();
        void addGrade(int const grade2add);
        void print();

    private:
        std::string name;
        int const id;
        std::vector<int> grades;
        int cost maxGrade;
};

The constructor:

Student::Student(std::string name_, int const id_): name(name_), id(id_)
{
    if (name.size()>=20)
    {
        cout<<"Name should be less than 20 chars"<<endl;
    }
    if (id.size()!=5)
    {
         cout<<"Id should be 5 nums"<<endl;
    }
}

The main: using namespace std;

int main()
{
    cout << "Hello School!" << endl;
    Student sarit_student("Sarit Rotshild",12345);
    return 0;
}

*all the relevant libraries and file are included. I got the following error: error: uninitialized member 'Student::maxGrade' with 'const' type 'const int' [-fpermissive]

sarar
  • 13
  • 2
  • 5
    Your error message does not match the code you have. What is `Student::maxGrade` and what line in the source code does this error come from? – NathanOliver Mar 15 '16 at 14:18
  • 1
    In your constructor, are you supposed to check if the `id` has five digits? Then that's not the way to do it (you can't call "member functions" on simply types like `int`). It's also a little late to do the check, especially if you are not doing anything about but print the message. – Some programmer dude Mar 15 '16 at 14:20
  • You should put the hole code with the problem. – Joel Mar 15 '16 at 14:25
  • @ NathanOliver- in the line of the constuctor- Student::Student(std::string name_, int const id_): name(name_), id(id_) – sarar Mar 15 '16 at 14:31

1 Answers1

0

To answer your question, you can implement a function with a const parameter just like that

#include <iostream>


class A {
public:

    A(const int a_foo) 
    : m_foo(a_foo)
    {}

    void bar(const int a_baz) {
        std::cout << "member variable : " << m_foo << std::endl;
        std::cout << "const parameter/argument : " << a_baz << std::endl;
        //m_foo = a_baz;  //won't work
    }

private :
    const int m_foo;
};

int main() {

    A a(10);
    a.bar(20);

    return 0;
}

Also, as NathanOliver pointed out, the error you are showing us doesn't fit with the code or at least we don't see any maxGrade member variable anywhere. The error seems to be that your maxGrade member has not been initialized (which should be done with every variable before including them into an operation). On a side note, you can write int const foo = 10;, but a more standardized way to declare your variables would be const int foo = 10; as explained here. You just need to be aware of what they represent which can be confusing when you're mixing it with pointers for instance where you could do const int const* foo;.

Also, I have some difficulties to believe that the code you are showing us would compile even if the error you are showing wouldn't exist, since you are calling id.size() with id being an int...

Community
  • 1
  • 1
pandaman1234
  • 523
  • 7
  • 17