c++ - operator overload -
i working on project trying keep c++ knowledge. anyways, getting many, many errors when try implement operator overload. not sure why.
#include "students.h" #include <iostream> #include "quack.h" using namespace std; void main() { quack* classmates = new quack; classmates->pushfront(students("corey", "9081923456", 4.0)); cout << "\noriginal data set -- " << *students;
and getting errors operator. oddly enough if comment out overloaded operator , leave in students.cpp compiles find.
#ifndef students_h #define students_h #include <iostream> class students { // causing errors friend ostream& operator << (ostream& out,const students& student); public: students(); students(char * name, char* oitid, float gpa); students(const students& student); // copy constructor; ~students(); const students& operator=(const students& student); void getname(char* name) const; void getoitid(char* oitid) const; float getgpa(void) const; void setname(char* name); void setoitid(char* oitid); void setgpa(float gpa); private: char* name; char* oitid; float gpa; }; #endif }
and, causes errors alo. not itself..
#include "students.h" #include <iostream> #include <iomanip> using namespace std; #pragma warning(disable:4996) private: char* name; char* oitid; float gpa; students::students(): name(null), oitid(null), gpa(0) { } students::students(char *name, char *oitid, float gpa): name(null), oitid(null), gpa(0) { setname(name); setoitid(oitid); } students::~students() { if(name) delete[] name; if(oitid) delete[] oitid; } const students& students::operator=(const students& student) { //if self copy, don't if(this == &student) return *this; //make current object *this copy of passed in student else { setname(student.name); setoitid(student.oitid); //setgpa(student.gpa); return *this; } } void students::setname(char *name) { //release exisisting memory if there if(this->name) delete [] this->name; //set new name this->name = new char[strlen(name)+1]; strcpy(this->name, name); } void students::setoitid(char *oitid) { if(this->oitid) delete [] this->oitid; //set new id this->oitid = new char[strlen(oitid)+1]; strcpy(this->oitid, oitid); } ostream& operator<< (ostream& out, const students& student) { //out << setw(20) << student.name //<< setw(15) << student.pccid //<< setw(8) << fixed << setprecision(2) << student.gpa; return out; }
here errors get
syntax error : missing ';' before '&'
: error c2433: 'ostream' : 'friend' not permitted on data declarations
error c4430: missing type specifier - int assumed. note: c++ not support default-int
error c2061: syntax error : identifier 'ostream'
error c4430: missing type specifier - int assumed. note: c++ not support default-int
error c2805: binary 'operator <<' has few parameters
1>generating code...
1>compiling...
1>students.cpp
my eyes burning , cant figure out why unhappy overloaded operator..
you using ostream
without qualifying namespace std::
the compiler errors/warnings vaguely telling has encountered type has not been declared yet.
friend std::ostream& operator << (std::ostream& out,const students& student);
Comments
Post a Comment