Classes with "Public Members" ERROR! Expected Expression

0

I would like to find the error or what my code lacks in order for it to compile successfully.

#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    string firstName, lastName;
    int dob;
    int phoneNumber;
    int id;
    double overallGPA;
};

int main()
{   
Student Student1;

cout << "\t Student 1 Info:" << endl;
cout << "Student 1 ID: ";
Student1.id = 444444444;

cout << "Student 1 Phone Number: ";
Student1.phoneNumber = {828, 888, 8888};

cout << "Student 1 Date Of Birth: ";
Student1.dob = {11, 21, 1985};

cout << "Student 1 Overall GPA: ";
Student1.overallGPA = 3.45;

cout << "Student 1 First Letter of Last Name: ";
Student1.firstName = "T";

cout << "Student 1 First Letter of First Name: ";
Student1.lastName = "R";


return 0;
}

In the lines Student1.dob and Student1.phoneNumber are an arrangement, but I do not know how to handle it with the classes.

I get the following error in the terminal:

preLab.cpp:30:25: error: expected expression
        Student1.phoneNumber = {828, 888, 8888};
                               ^
preLab.cpp:33:17: error: expected expression
        Student1.dob = {11, 21, 1985};
                       ^
2 errors generated.
    
asked by Gabriel Valedon 04.02.2017 в 18:21
source

1 answer

1
class Student
{
public:
    string firstName, lastName;
    int dob;
    int phoneNumber;
    int id;
    double overallGPA;
};

First, if all the members of a class are public, then you better change class by struct and you save the public:

struct Student
{
    string firstName, lastName;
    int dob;
    int phoneNumber;
    int id;
    double overallGPA;
};
  

In lines Student1.dob and Student1.phoneNumber are an arrangement, but I do not know how to handle it with the classes.

The structure you have created does not have those fields as fixes but as variables to use. To create arrangements you have to use or brackets or dynamic memory:

struct Student
{
    string firstName, lastName;
    int dob[3];
    int phoneNumber[3];
    int id;
    double overallGPA;
};

And now if you could do the following:

Student1.phoneNumber[0] = 828;
Student1.phoneNumber[1] = 888;
Student1.phoneNumber[2] = 8888;

You can not do what you want:

Student1.phoneNumber = {828, 888, 8888};

Since this mechanism only works during initialization.

    
answered by 04.02.2017 / 18:50
source