Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

error in c++ code in old editors versions

+1 vote
asked Dec 3, 2018 by Nibal Tinawi (190 points)
Hello everybody
I wrote this code but the result did not appear because of errors
the result appear  only in online editor and visual studio new versions
but in my visual studio 2010 many errors appear
maybe it is a small error
and I saw a red color under the = in the code maybe it is the error
thank you very much

#include <iostream>

#include <string>

#include<stdio.h>

#include<conio.h>

using namespace std;

class teacher1 {

private:

std::string str1 = "master";

     std::string str2 = "homework";

    public:

    teacher1();

   ~teacher1();

    void msg();

};

teacher1::teacher1(){cout<<"This is the information about mr.samer najjar"<<endl;}

 teacher1::~teacher1(){cout<<" end of the information"<<endl;}

void teacher1::msg(){

    std::cout << "samer najjar holds a " <<str1 <<" degree ."<< std::endl;

    std::cout << "samer najjer corrects the  students' " <<str2 <<"."<<""<<endl<< std::endl;

}

class teacher2 {

private:

     std::string str3 = "docturate";

     std::string str4 = "homework";

    public:

    teacher2();

    ~teacher2();

    void msg1();

};

   teacher2 ::teacher2(){cout<<"this is the information about mr.rateb hennawi"<<endl;}

   teacher2 ::~teacher2(){cout<<" end of the information"<<endl;}

void teacher2::msg1(){

    std::cout << " rateb hennawi holds a " <<str3 <<" degree ."<< std::endl;

    std::cout << " rateb hennawi corrects the students' " <<str4 <<endl<<""<<std::endl;

}

int main() {
   teacher1 temp;
   temp. msg();
   
   teacher2 nim;
   nim .msg1();

    return 0;
}

1 Answer

0 votes
answered Dec 3, 2018 by Юрий Гуреев (1,100 points)
You can`t initialize class variables in such way (you can do this in Java). You should do it in constructor. For example:

class MyClass {

  string str; // string str = "qwe"; <-- wrong

public:

  MyClass() { str = "Some string"; } // <-- right

};
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...