The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Binary '<<' : no operator found which takes a right-hand operand of type 'dateNode'
Discuss Binary '<<' : no operator found which takes a right-hand operand of type 'dateNode' in the C Programming forum on Dev Shed. Binary '<<' : no operator found which takes a right-hand operand of type 'dateNode' C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 27th, 2009, 10:52 PM
|
 |
Contributing User
|
|
|
|
|
Binary '<<' : no operator found which takes a right-hand operand of type 'dateNode'
Hello, my name is Anthony and I am having some difficulty with a class project. Firstly, my code:
(date.cpp)
PHP Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "DateNode.h";
bool text(string fName);
ostream& operator<<(const dateNode d, ostream& out);
void main()
{ string fName = "C:\Users\Anthony\Documents\Visual Studio 2008\Projects\DateNode.cpp\Dates.txt";
fstream dateText;
int dateSize = 1;
class dateNode date[20];
if(text(fName))
dateText.open(fName.data(), ios::in);
while(!dateText.eof())
{ if(date[dateSize].get(cin, dateText))
dateSize++;
};
dateText.close();
for(int i = 1; i<=dateSize; i++)
cout << date[i] << endl;
}
bool text(string fName)
{ fstream outFile;
outFile.open(fName.data(), ios::out);
if(outFile.is_open())
{ outFile << "Sep. 17, 1987" << endl; //1
outFile << "Jan. 6, 2001" << endl; //2
outFile << "Mar. 4, 1990" << endl; //3
outFile << "Jun. 16, 1993" << endl; //4
outFile << "Jul. 5, 1996" << endl; //5
outFile << "Nov. 11, 2004" << endl; //6
outFile << "Feb. 20, 2007" << endl; //7
outFile << "Sep. 3, 2009" << endl; //8
outFile.close();
};
return outFile.good();
}
ostream& operator<<(const dateNode& d, ostream& out)
{ return out<<d.getMonthName()<<" "<<d.getDay()<<" "<<d.getYear();
}
(DateNode.h)
PHP Code:
//DateNode class definition
class dateNode
{ public:
dateNode();
bool get(istream &in, fstream);
int getYear() const { return year; }
int getMonthNum() const { return monthNum; }
int getDay() const { return day; }
string getMonthName() const { return monthName; }
private:
string monthName;
int day, year, monthNum;
};
(DateNode.cpp)
PHP Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "DateNode.h";
// Null Constructor
dateNode::dateNode()
{ day = 0;
monthNum = 0;
year = 0;
monthName = "Empty";
}
// Retrieve Text Data
bool dateNode::get(istream &in, fstream text)
{ text >> monthName;
text >> day;
text >> year;
return text.good();
}
I am only getting one error, along with several warnings, (using MS Visual C++) but the error as stated by the compiler is:
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(28) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'dateNode' (or there is no acceptable conversion)
The purpose of this program (at least thus far) is to create a text file called dates.txt and put 8 different dates into it. Then, it should open the file and read and store the dates in a dateNode array named date. I am supposed to overload the << operator so that I can use the following snippet of code:
PHP Code:
for(int i = 1; i<=dateSize; i++)
cout << date[i] << endl;
to display the dates. I believe my problem lies somewhere within either my use, definition, or declaration of the overloaded operator, but I do not understand what I am doing wrong. Thanks in advance for any advice.
|

August 27th, 2009, 11:21 PM
|
|
|
|
You need to read the "How to Post" thread. It's the one that says, "READ THIS FIRST." Sounds silly, I know, but your code is too dam' ugly to waste time on.
If you can't read and comprehend that, then you don't need to be farbling with a programming language.
Just sayin'.
|

August 27th, 2009, 11:24 PM
|
 |
Contributing User
|
|
|
|
|
You're right sizablegrin, I will try and fix it up. Sorry about that.
|

August 27th, 2009, 11:32 PM
|
 |
Contributing User
|
|
|
|
|
Is that better? Anything else wrong with my post that I should be aware of? Any advice on my posting or my code is greatly appreciated.
|

August 28th, 2009, 03:29 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
The overload protoyype should be:
Code:
ostream& operator<<( ostream& out, const dateNode d);
you have the arguments the wrong way around. Think about it, in teh expression cout << data, the iostream object is on the left-and side.
BTW regard warnings as errors if you want to be a good software developer. You probably should have posted those too. You can configure the compiler to reject code that has warnings, and increase the warning level; I suggest that you do both.
Clifford
|

August 28th, 2009, 01:11 PM
|
 |
Contributing User
|
|
|
|
changed
PHP Code:
ostream& operator<<(const dateNode d, ostream& out);
to
PHP Code:
ostream& operator <<(ostream& out, const dateNode& d);
and I changed
PHP Code:
ostream& operator<<(const dateNode& d, ostream& out)
{ return out<<d.getMonthName()<<" "<<d.getDay()<<" "<<d.getYear();
}
to
PHP Code:
ostream& operator<<(ostream& out, const dateNode& d)
{ return out<<d.getMonthName()<<" "<<d.getDay()<<" "<<d.getYear();
}
and upon attempt to compile I get the following warnings and errors:
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(6) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(12) : warning C4129: 'A' : unrecognized character escape sequence
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(12) : warning C4129: 'D' : unrecognized character escape sequence
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(12) : warning C4129: 'V' : unrecognized character escape sequence
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(12) : warning C4129: 'P' : unrecognized character escape sequence
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(12) : warning C4129: 'D' : unrecognized character escape sequence
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(12) : warning C4129: 'D' : unrecognized character escape sequence
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\fstream(934) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
I think the error has something to do with the fstream library (since it says fstream in it) but all I can guess at is that somehow my fstream object isn't interacting with my class properly? I have no idea what the warnings mean. Completely confused on those.
|

August 28th, 2009, 01:36 PM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
You have an unclosed quote somewhere, based on my experience looking at these sorts of things. In your case the absolutely are not warnings, they are errors.
If you colorize your code (which you can do here, just use php code tags and look in the preview) you will likely immediately know where the fault is.
|

August 28th, 2009, 01:38 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
The errors mean exactly what they say. Did you actually read them!?
#include "DateNode.h";
What is that ';' doing there?
\ is the esacpe sequence introducer, as in '\n' for newline for example. To include a '\' in a string, the escap sequence '\\' must be used.
This is what I mean about warnings; these are errors. The compiler is saying "I can compile this, but it is unlikely to be what you intended".
Also never look at the last message! Fix the ones before it first, the chances are the later ones are related and will go away. Now think about it; what ar ethe chances of tehre being an error in a standard library header? About nil I'd say.
Fix the warnings, report back (and post the modified code if you need further assistance).
Clifford.
|

August 29th, 2009, 01:35 AM
|
 |
Contributing User
|
|
|
|
changed
to
in both Date.cpp and DateNode.cpp
changed
PHP Code:
string fName = "C:\Users\Anthony\Documents\Visual Studio 2008\Projects\DateNode.cpp\Dates.txt";
to
PHP Code:
string fName = "C:\\Users\\Anthony\\Documents\\Visual Studio 2008\\Projects\\DateNode.cpp\\Dates.txt";
in Date.cpp
Those fixes actually did get rid of all of the warnings. However, the error message:
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\fstream(934) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
still plagues me when I try to compile this code.
|

August 29th, 2009, 04:17 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by jakotheshadows
Those fixes actually did get rid of all of the warnings. |
Good now at least you can see the wood for the trees, and more importantly know that you are not wasting your time analysing this message.
fstream::data() is a private member function of the std::ios base class. Where did you get the idea you could call that since it is implementation dependent and undocumented!? You need fName.c_str() to obtain a const char* required by fstream::open(). Or simply declare fName to be a const char* in the first instance since.
The error message is somewhat arcane, and it would have helped if it has included the name of the private member that was being accessed, or indicated where the offending call was being made from. These are things that yopu would get from say GCC. Sometimes when a compiler message really stumps me I compile it in a different compiler (usually GCC and VC++). The different approaches to error mesaging sometimes shed light on the problem. In this case I did not do that, I started out with Occam's razor which suggests the standard header file is correct, so then I looked to see where fstream objects were used in the code being compiled and checked those lines, and the call to a function not documented as a member function stuck out prominently.
Luckily I spotted have the error without you posting the modified code I asked for :rolleyes:. I had intended to use it to attempt to reproduce your error. I would not have bothered to laboriously reproduce it by applying your incremental changes.
Clifford
Last edited by clifford : August 29th, 2009 at 05:42 AM.
|

August 30th, 2009, 02:11 PM
|
 |
Contributing User
|
|
|
|
Here is my complete modified code thus far:
(DateNode.h)
PHP Code:
//DateNode class definition
class dateNode
{ public:
dateNode();
bool get(istream &in, fstream);
int getYear() const { return year; }
int getMonthNum() const { return monthNum; }
int getDay() const { return day; }
string getMonthName() const { return monthName; }
private:
string monthName;
int day, year, monthNum;
};
(DateNode.cpp)
PHP Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "DateNode.h"
// Null Constructor
dateNode::dateNode()
{ day = 0;
monthNum = 0;
year = 0;
monthName = "Empty";
}
// Retrieve Text Data
bool dateNode::get(istream& in, fstream text)
{ text >> monthName;
text >> day;
text >> year;
return text.good();
}
(Date.cpp)
PHP Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "DateNode.h"
bool text(fstream& f);
ostream& operator <<(ostream& out, const dateNode d);
void main()
{ fstream dateText;
int dateSize = 1;
class dateNode date[20];
if(text(dateText))
dateText.open("dates.txt", fstream::in);
while(!dateText.eof())
{ if(date[dateSize].get(cin, dateText))
dateSize++;
};
dateText.close();
for(int i = 1; i<=dateSize; i++)
cout << date[i] << endl;
}
bool text(fstream& f)
{ f.open("dates.txt", fstream::out);
if(f.is_open())
{ f << "Sep. 17, 1987" << endl; //1
f << "Jan. 6, 2001" << endl; //2
f << "Mar. 4, 1990" << endl; //3
f << "Jun. 16, 1993" << endl; //4
f << "Jul. 5, 1996" << endl; //5
f << "Nov. 11, 2004" << endl; //6
f << "Feb. 20, 2007" << endl; //7
f << "Sep. 3, 2009" << endl; //8
};
if(f.good()&&f.is_open())
{ f.close();
return true;
}
else
return false;
}
ostream& operator<<(ostream& out, const dateNode d)
{ return out<<d.getMonthName()<<" "<<d.getDay()<<" "<<d.getYear();
}
all of this still gives me the same error I mentioned most recently.
I tried your advice with changing fName to a const char* with the following version of Date.cpp:
PHP Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "DateNode.h"
bool text(const char* f);
ostream& operator <<(ostream& out, const dateNode d);
void main()
{ fstream dateText;
int dateSize = 1;
class dateNode date[20];
const char* fName = "C:\\Users\\Anthony\\Documents\\Visual Studio 2008\\Projects\\DateNode.cpp\\Dates.txt";
if(text(fName))
dateText.open(fName.data(), ios::in);
if(dateText.is_open())
{ while(!dateText.eof())
{ if(date[dateSize].get(cin, dateText))
dateSize++;
};
};
dateText.close();
for(int i = 1; i<=dateSize; i++)
cout << date[i] << endl;
}
bool text(const char* f)
{ fstream out;
out.open(f.data(), ios::out);
if(out.is_open())
{ out << "Sep. 17, 1987" << endl; //1
out << "Jan. 6, 2001" << endl; //2
out << "Mar. 4, 1990" << endl; //3
out << "Jun. 16, 1993" << endl; //4
out << "Jul. 5, 1996" << endl; //5
out << "Nov. 11, 2004" << endl; //6
out << "Feb. 20, 2007" << endl; //7
out << "Sep. 3, 2009" << endl; //8
};
if(out.good()&&out.is_open())
{ out.close();
return true;
}
else
return false;
}
ostream& operator<<(ostream& out, const dateNode d)
{ return out<<d.getMonthName()<<" "<<d.getDay()<<" "<<d.getYear();
}
but that gives me these two errors:
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(17) : error C2228: left of '.data' must have class/struct/union
1> type is 'const char *'
1>c:\users\anthony\documents\visual studio 2008\projects\datenode.cpp\datenode\date.cpp(35) : error C2228: left of '.data' must have class/struct/union
1> type is 'const char *'
I'm such a n00b 
|

August 30th, 2009, 02:20 PM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
I have no idea what the heck "if(text(fName))" is supposed to do, but I can tell you with 100% certainty that "dateText.open(fName.data(), ios::in);" isn't doing anything you want it to do. Do some research on the capabilities of char, pointers to char (char *) and what happens when you make one const. Then look around for the documentation on the .data() method and report back here with what you learned.
By the by, if I simply tell you what you need to know you really aren't learning how to learn. You have made some critical mistakes and I am trying to help you learn what they are.
You might also want to read up on RTFM a bit also as that will become critical if you choose to continue as a programmer (in C++ or any other language).
|

August 30th, 2009, 05:20 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
|
The code you posted still includes the error I have already told you about, except this time you have changed the type of fName so that it is no longer a class, so attempting to call a member function of fName is now nonsense. I would have thought that "left of '.data' must have class/struct/union" was a pretty big clue - "left of .data" being fName.
If you change fName to a const char* as I suggested, is it not obvious also that fName.data() no longer makes even less sense than it did before!? You just need fName there.
I believe I have already suggested that you actually read the error messages. This feels like spoon feeding. This is not the quick easy method of getting your work done that you might be thinking. Actually thinking would be far quicker.
Clifford
|

September 1st, 2009, 12:40 AM
|
 |
Contributing User
|
|
|
|
OK! I've got some code that compiles now, but it crashes as soon as I try to run it. I know you are all trying to encourage me to think more about my own code instead of coming here, and I do appreciate it, but now I have no compiler errors to work with. I'm just stuck staring at my code until I see whats making it crash. I'm using Vista 64 if that means anything.. Here is the code I have now:
DateNode.cpp
PHP Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "DateNode.h"
// Null Constructor
dateNode::dateNode()
{ day = 0;
monthNum = 0;
year = 0;
monthName = "Empty";
}
void dateNode::setDay(int d)
{ day = d;
}
void dateNode::setYear(int y)
{ year = y;
}
void dateNode::setMonthNum(int n)
{ monthNum = n;
}
void dateNode::setMonthNam(string s)
{ monthName = s;
}
DateNode.h
PHP Code:
//DateNode class definition
class dateNode
{ public:
dateNode();
int getYear() const { return year; }
int getMonthNum() const { return monthNum; }
int getDay() const { return day; }
string getMonthName() const { return monthName; }
void setYear(int);
void setDay(int);
void setMonthNum(int);
void setMonthNam(string);
private:
string monthName;
int day, year, monthNum;
};
date.cpp
PHP Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "DateNode.h"
bool text();
ostream& operator <<(ostream& out, const dateNode d);
void main()
{ ifstream dateText;
int dateSize = 1;
class dateNode date[20];
bool good;
string textMonth;
int textDay, textYear;
good = text();
if(good)
dateText.open("dates.txt");
if(dateText.is_open())
{ while(!dateText.eof())
{ dateText >> textMonth;
dateText >> textDay;
dateText >> textYear;
date[dateSize].setMonthNam(textMonth);
date[dateSize].setDay(textDay);
date[dateSize].setYear(textYear);
dateSize++;
};
};
dateText.close();
for(int i = 1; i<=dateSize; i++)
cout << date[i] << endl;
}
bool text()
{ ofstream out;
out.open("dates.txt");
if(out.is_open())
{ out << "Sep. 17, 1987" << endl; //1
out << "Jan. 6, 2001" << endl; //2
out << "Mar. 4, 1990" << endl; //3
out << "Jun. 16, 1993" << endl; //4
out << "Jul. 5, 1996" << endl; //5
out << "Nov. 11, 2004" << endl; //6
out << "Feb. 20, 2007" << endl; //7
out << "Sep. 3, 2009" << endl; //8
};
if(out.good()&&out.is_open())
{ out.close();
return true;
}
else
return false;
}
ostream& operator<<(ostream& out, const dateNode d)
{ return out<<d.getMonthName()<<" "<<d.getDay()<<" "<<d.getYear();
}
Please help 
|

September 1st, 2009, 01:38 AM
|
|
|
|
Where is it failing? Don't you have a debugger? Why don't you learn to use it? It repays the effort many times over.
Why would you name a variable "good" when you know you have a method named "good." Not the same, but don't you think you're confusing issues when you should be simplifying?
Just curious.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|