Issue
I'm using a function void displayStudent(Student stu)
, which displays the student's info. In my main()
, I did this (all the parameters have been created in main()
respectively already, and Student
is my class):
Student student1(name1, id1, dept1, year1);
displayStudent(student1);
Now, in my display function, here is my syntax which works for everything correctly besides the name
of the student:
void displayStudent(Student stu) {
// Does not work, gives error saying "no type named 'type"
// and no operator "<<" matches these operands.
cout << "Name: " << stu.getName() << endl;
cout << "ID Number: " << stu.getidNumber() << endl;
cout << "Department: " << stu.getDepartment() << endl;
cout << "Year: " << stu.getYear() << endl;;
}
Furthermore, for info here are my getter/setter functions for the name
in Student.cpp
, and the struct Name
in Student.h
, which looks like this:
struct Name {
string firstName;
string lastName;
};
The variables in this instance of Student
have already been initialized with random info from another function in this Student.cpp
file:
void Student::setName(Name n) {
name.firstName = n.firstName;
name.lastName = " " + n.lastName;
}
Name Student::getName() const {
return name;
}
Essentially, what I can't figure out is, why can I display everything but the name
in my main()
? How do I fix this to access elements of the name
struct in this instance of Student
? I initialized them earlier like this:
Student::Student(Name a, int b, string c, Year d) {
// Assigns student info to the 4 parameter variables
a.firstName = "Roger";
a.lastName = "Federer";
b = 12345;
c = "Art";
d = SENIOR;
name.firstName = a.firstName;
name.lastName = " " + a.lastName;
idNumber = b;
department = c;
year = d;
}
Solution
On this line:
cout << "Name: " << stu.getName() << endl;
stu.getName()
returns a Name
struct, but by default the compiler doesn't know how to print a Name
struct to an std::ostream
, like std::cout
, when using operator<<
. So you need to implement your own operator<<
overload for printing a Name
, eg:
struct Name {
string firstName;
string lastName;
};
// add this!
ostream& operator<<(ostream &out, const Name &name) {
return out << name.firstName << " " << name.lastName;
}
On a side note, there is code in your Student
constructor that does not belong there:
Student::Student(Name a, int b, string c, Year d) {
/* these assignments do not belong here!!! The caller
is responsible for providing the appropriate values
when it calls Student()...
// Assigns student info to the 4 parameter variables
a.firstName = "Roger";
a.lastName = "Federer";
b = 12345;
c = "Art";
d = SENIOR;
For example:
Name name1;
name1.firstName = "Roger";
name1.lastName = "Federer";
int id1 = 12345;
string dept1 = "Art";
Year year1 = SENIOR;
Student student1(name1, id1, dept1, year1);
*/
name.firstName = a.firstName;
name.lastName = /*" " +*/ a.lastName; // <-- the space character does not belong, either!
// which can be simplified to just this!
// name = a;
idNumber = b;
department = c;
year = d;
}
And same with setName()
:
void Student::setName(Name n) {
name.firstName = n.firstName;
name.lastName = /*" " +*/ n.lastName; // <--
// or, simply:
// name = n;
}
Answered By - Remy Lebeau Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.