Implementing the extraction and insertion operator C++ -


i have basic student class (has class)(yes there no encapsulation , namespace pollution pls forgive) , create custom extraction , insertion operators. after countless searches still not work. have this

#ifndef student #define student  #include <iostream> #include <string>  using namespace std;  class student {     public:         string name;         double score;          student();         student(string name, double score) : name(name), score(score) {}          friend ostream& operator<<(ostream &out, student &student);         friend istream& operator>>(istream &in, student &student); };  #endif 

#include "student.h"  ostream& operator<<(ostream &out, student &student) {     out << student.name << ", " << student.score;     return out; }  istream& operator>>(istream &in, student &student) {     if (!in >> name || !in >> score)     {         in.setstate(ios::failbit);     }     return in; } 

i have tried many things this->name student::name name student::student.name changing function signature did end working except did not overload operator. pls halp :d

edit: specific problem, acessing member of student class within method. student.name , student.score throwing an

expected primary-expression before '.' token 

and bottom 1 relic of throwing different solution @ it's scope error.

edit2: problem turn out conflict guard in header being called student pre-processor nuke word student everywhere -_- help

various problems have been pointed out in comments , in captain giraffe's answer. more critical problem this:

you declare variable called student in functions; header #defines student in header guard! #defined symbol clashes variable name, resulting in errors see. recommended syntax header guard like

#define student_h_included 

Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -