Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Problem with << overlaoding (http://www.programmingforums.org/showthread.php?t=10527)

sobank Jun 26th, 2006 12:00 AM

Problem with << overlaoding
 
well i am trying to overload <<. And i am getting the errors that i have no idea how to fix.

here is the header file.

#define MAX_FILENAME 21

class DataPoint {
double x;
double y;
public:
DataPoint();
~DataPoint();
double getx() const;
double gety() const;
friend bool operator==(const DataPoint&, const DataPoint&);
int loadFromFile(FILE*);
bool saveToFile(FILE*);
void xStore(double);
void yStore(double);
};

class DataSet {
DataPoint* points;
int n; //number of records
char dataLabel[MAX_FILENAME];//label
double t; //slope
double r; //correlation
double b; //intercepts
double m; // mean
double d; // standard deviation
double tol; //tolorence

public:
DataSet();
~DataSet();
DataSet(const DataSet&);
void include();
void include(const double);
bool outlier(int) const;
bool saveToFile(const char* filename);
int outlier()const;
void display() const;
DataSet& operator=(const DataSet&);
bool loadFromFile(const char*);
const char* label() const;
double slope() const;
double intercept() const;
double correlation() const;
int nPoints() const;
DataPoint point(int) const;
void setlabel(const char*);
void sum(int,const double*, const double*);
double mean() const;
double stdDev() const;
double tolorence() const;
void memCreate(int);
friend bool operator==(const DataSet&, const DataSet&);
friend DataSet operator+(const DataSet&, const DataSet&);
friend ostream& operator<<(ostream&, const DataSet&);
};


now in .cpp file I try to overload the << operator

ostream& operator<<(ostream& os, const DataSet& ds){
os<<left<<setw(10)<<ds.slope()<<setw(15)<<ds.intercept()<<setw(15)<<ds.correlation()<<setw(30)<<ds.label()<<endl;
return os;
}

and these are the list of errors that i am getting on visual studio

------ Build started: Project: assignment 333, Configuration: Debug Win32 ------
Compiling...
DataSet.cpp
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : error C2143: syntax error : missing ';' before '&'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : error C2433: 'ostream' : 'friend' not permitted on data declarations
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : error C2061: syntax error : identifier 'ostream'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : error C2805: binary 'operator <<' has too few parameters
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2872: 'ostream' : ambiguous symbol
could be 'd:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : int ostream'
or 'c:\program files\microsoft visual studio 8\vc\include\iosfwd(701) : std::ostream'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2143: syntax error : missing ';' before '&'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2086: 'int ostream' : redefinition
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : see declaration of 'ostream'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2872: 'ostream' : ambiguous symbol
could be 'd:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : int ostream'
or 'c:\program files\microsoft visual studio 8\vc\include\iosfwd(701) : std::ostream'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2872: 'ostream' : ambiguous symbol
could be 'd:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.h(55) : int ostream'
or 'c:\program files\microsoft visual studio 8\vc\include\iosfwd(701) : std::ostream'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2065: 'os' : undeclared identifier
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2059: syntax error : 'const'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2143: syntax error : missing ';' before '{'
d:\my documents\visual studio 2005\projects\assignment 333\assignment 333\dataset.cpp(408) : error C2447: '{' : missing function header (old-style formal list?)
Generating Code...
Compiling...
regressionF.cpp
Generating Code...
Build log was saved at "file://d:\My Documents\Visual Studio 2005\Projects\assignment 333\assignment 333\Debug\BuildLog.htm"
assignment 333 - 16 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



I have tried to match them with prof. lectures but it looks like every thing is right.

Any idea????????????????????????

The Dark Jun 26th, 2006 12:22 AM

1. You need to post code inside [ code ] tags (no spaces) - this preserves indentation and makes it easier for people to answer you.
2. You need to include <ostream> before you include your .h file (or include <ostream> inside your include file)
3. You either need to use std:: in front of your use of ostream in your .h file (recommended), or put "using namespace std;" at the top of your include file (not recommended).

The errors are because the C compiler does not know, at line 55 in your include file, what "ostream" is. This is either because of 2 or 3 above.

Jimbo Jun 26th, 2006 1:05 AM

you can also put
:

using std::ostream;
if you don't want to put the std:: prefix everywhere and not use the whole std namespace.


All times are GMT -5. The time now is 8:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC