Search code examples
c++classobjectinitialization

Having Problem for Object Initialization by Constructor in C++


I want to create a Student object in C++ and it has the properties of name, major, age and id. The object initialization will be done in the main() part and Student object has the get and set methods for all the constructors. I want to print the student objects in the main() part but I get this error: in C++98 's1' must be initialized by constructor, not by '{...}'

I am using GNU GCC Complier in Codeblocks. I haven't written specifically any code for compiling or debugging.

I tried to initialize the objects by assigning them to this, making them null, giving them zero and random values but they haven't worked.

Student.h file

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>

using namespace std;

class Student
{
    public:
        string name, major;
        int age, id;
        Student(string name, string major, int age, int id);

        string getName();
        void setName(string name);

        string getMajor();
        void setMajor(string major);

        int getAge();
        void setAge(int age);

        int getId();
        void setId(int id);

};

ostream & operator << (ostream &out, Student &s);

#endif // STUDENT_H

Student.cpp file

#include "Student.h"
#include <iostream>

using namespace std;

Student::Student(string newName, string newMajor, int newAge, int newId)
{
    name = newName;
    major = newMajor;
    age = newAge;
    id = newId;
}

string Student::getName(){
    return name;
}

void Student::setName(string newName){
    name = newName;
}

string Student::getMajor(){
    return major;
}

void Student::setMajor(string newMajor){
    major = newMajor;
}

int Student::getAge(){
    return age;
}

void Student::setAge(int newAge){
    age = newAge;
}

int Student::getId(){
    return id;
}

void Student::setId(int newId){
    id = newId;
}

ostream & operator << (ostream &out, Student &s)
{
    out << "Name: " << s.getName() << " Major: " << s.getMajor() << " Age: " << s.getAge() << " Id:" << s.getId() << endl;
    return out;
}

Main.cpp file

#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

int main()
{
    Student s1 {"John","MATH",24,123456};
    Student s2 {"Steve","ENG",22,654321};

    cout << s1 << endl;
    cout << s2 << endl;

    return 0;
}

I expect to print out the properties of the students as a list but when I run it the program crashes and I get this error: ** in C++98 's1' must be initialized by constructor, not by '{...}' **


Solution

  • I fixed my problem. There were a few problems so here I will explain my solutions in detail.

    1-My code is written in C++11 syntax but I was using C++98 syntax so I changed my complier to C++11.

    2-My initialization was wrong, I used new variables such as newName, newAge... to change the properties of the Student object.

    3-My set methods were wrong so I changed them similar to my initialization.

    4-I added an opeator to print out properties more easily.

    All the changes are updated for the code in the question