views:

80

answers:

2
+1  Q: 

operator overload

I am working on this project just trying to keep up my c++ knowledge. Anyways, I am getting many, many errors when i try to implement an 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, Mattis", "9081923456", 4.0));

cout << "\noriginal data set -- " << *students;

and this is where i am getting the errors with the operator. Oddly enough if i comment out the overloaded operator and leave it in students.cpp it 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. But not by 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 it is a self copy, don't do anything
if(this == &student)
    return *this;
//make current object *this a copy of the passed in student
else
{
    setName(student.name);
    setoitId(student.oitId);
    //setGpa(student.gpa);
    return *this;
}

}


void students::setName(char *name)
{

//release the exisisting memory if there is any
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 is the errors i get

syntax error : missing ';' before '&'
: error C2433: 'ostream' : 'friend' not permitted on data declarations
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2061: syntax error : identifier 'ostream'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2805: binary 'operator <<' has too few parameters
1>Generating Code...
1>Compiling...
1>students.cpp

my eyes are burning and i cant figure out why its unhappy with the overloaded operator..

+7  A: 

You are using ostream without qualifying it with namespace std::

The compiler errors/warnings are vaguely telling you that it has encountered a type that has not been declared yet.

friend std::ostream& operator << (std::ostream& out,const students& student);
birryree
A: 

Inline with comment by sbi, if your goal is to improve your C++ knowledge:

  1. consider using C++ objects - such as std::string etc. rather than the char* (then you don't need the explicit memory management - always a good candidate for bugs and weird crashes), your getters can then return a const reference to the std::string (presumably in your implementation code, you're doing a copy, think about the added issues of this...)
  2. Presumably quack is some kind of container, again point 1, think about using some of the existing libraries (STL for example).
  3. "this->" is redundant, class members can be referred to directly - just clutters the code, if worried about variable name clashes, think about a naming convention and stick to it.

Now, looking specifically at your code: 1. I'll assume that

cout << "\noriginal data set -- " << *students;

is a typo, students is a type, you can only dereference a pointer, which this isn't (at least in the code snippet you've posted - if not, think carefully about variable names!). 2. presumably you meant it to be

cout << "\noriginal data set -- " << *classmates;

If so, check your stream operator for quack is implemented correctly.

Nim