![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
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???????????????????????? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 766
Rep Power: 3
![]() |
you can also put
using std::ostream; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|