Please don’t confuse with the title as it was already asked by someone but for a different context
The below code in Visual C++ Compiler (VS2008) does not get compiled, instead it throws this exception:
std::ifstream input (fileName);
while (input) {
string s;
input >> s;
std::cout << s << std::endl;
};
But this code compiles fine in cygwin g++. Any thoughts?
tmlen
8,1724 gold badges31 silver badges83 bronze badges
asked Oct 27, 2009 at 14:49
![]()
0
Have you included all of the following headers?
<fstream><istream><iostream><string>
My guess is you forgot <string>.
On a side note: That should be std::cout and std::endl.
answered Oct 27, 2009 at 15:07
sbisbi
217k45 gold badges254 silver badges439 bronze badges
12
Adding to @sbi answer, in my case the difference was including <string> instead of <string.h> (under VS 2017).
See the following answer: similar case answer
![]()
answered Mar 9, 2018 at 11:35
![]()
Guy AvrahamGuy Avraham
3,3523 gold badges39 silver badges48 bronze badges
In addition to what others said. The following code was necessary in my application to compile succesfully.
std::cout << s.c_str() << std::endl;
Another work-around to this is go to project properties -> General -> Character Set and choose «Ues Multi-Byte Character Set» (You won’t need to use c_str() to output the string)
There’s disadvantages to using MBCS so if you plan to localize your software, I’d advize against this.
answered Oct 24, 2018 at 16:10
![]()
Nick DelbarNick Delbar
1012 silver badges9 bronze badges
include <string>
Try including string header file along with <iostream> file.
It will work in some compilers even without the <string> because settings for different compilers are different and it is the compiler that is responsible for reading the preprocessor files that start with ‘#’ symbol to generate a obj file.
answered Aug 30, 2018 at 16:04
2
I’m required to write a function to overload the ==operator to compare width, height and colour. I need to return ‘Y’ if its equal and ‘N’ if its not.
This is my code which I think is correct, but keeps giving me the error:
error C2679: binary ‘<<‘ : no operator found which takes a right-hand operand of type ‘Rectangle’ (or there is no acceptable conversion)
I’ve searched for an answer and nothing came close to comparing 3 data as most examples are for comparing 2 datas.
#include <iostream>
#include <string>
using namespace std;
class Rectangle
{
private:
float width;
float height;
char colour;
public:
Rectangle()
{
width=2;
height=1;
colour='Y';
}
~Rectangle(){}
float getWidth() { return width; }
float getHeight() { return height; }
char getColour() { return colour; }
Rectangle(float newWidth, float newHeight, char newColour)
{
width = newWidth;
height = newHeight;
colour = newColour;
}
char operator== (const Rectangle& p1){
if ((width==p1.width) && (height==p1.height) && (colour==p1.colour))
return 'Y';
else
return 'N';
}
};
int main(int argc, char* argv[])
{
Rectangle rectA;
Rectangle rectB(1,2,'R');
Rectangle rectC(3,4,'B');
cout << "width and height of rectangle A is := " << rectA.getWidth() << ", " << rectA.getHeight() << endl;
cout << "Are B and C equal? Ans: " << rectB==rectC << endl;
return 0;
}
I’m required to write a function to overload the ==operator to compare width, height and colour. I need to return ‘Y’ if its equal and ‘N’ if its not.
This is my code which I think is correct, but keeps giving me the error:
error C2679: binary ‘<<‘ : no operator found which takes a right-hand operand of type ‘Rectangle’ (or there is no acceptable conversion)
I’ve searched for an answer and nothing came close to comparing 3 data as most examples are for comparing 2 datas.
#include <iostream>
#include <string>
using namespace std;
class Rectangle
{
private:
float width;
float height;
char colour;
public:
Rectangle()
{
width=2;
height=1;
colour='Y';
}
~Rectangle(){}
float getWidth() { return width; }
float getHeight() { return height; }
char getColour() { return colour; }
Rectangle(float newWidth, float newHeight, char newColour)
{
width = newWidth;
height = newHeight;
colour = newColour;
}
char operator== (const Rectangle& p1){
if ((width==p1.width) && (height==p1.height) && (colour==p1.colour))
return 'Y';
else
return 'N';
}
};
int main(int argc, char* argv[])
{
Rectangle rectA;
Rectangle rectB(1,2,'R');
Rectangle rectC(3,4,'B');
cout << "width and height of rectangle A is := " << rectA.getWidth() << ", " << rectA.getHeight() << endl;
cout << "Are B and C equal? Ans: " << rectB==rectC << endl;
return 0;
}
- Forum
- General C++ Programming
- C2679 Error I can not solve, would appre
C2679 Error I can not solve, would appreciate help greatly.
Hello!
This is my first time posting here, I hope I am doing so correctly, if not I firmly apologize! I simply am having a lot of trouble with my code and finally hit a breaking point. I have been working on this for hours, and googled heavily, and i think i understand what its saying but I have no idea how to fix it at all.
I am supposed to do code that makes a class template for arrays, and operator overload the information. But whenever I try to check and see if my operator+ overloading works, it says ‘error C2679: binary ‘+’ : no operator found which takes a right-hand operand of type ‘int’ (or there is no acceptable conversion)».
This is my operator+ overloading
(this is in tlist.cpp which has the declaration of the class template and the member functions to it)
TLIST<t_type> & operator+(const TLIST<t_type> & org);
(this is the member function of operator+)
|
|
this is in the other .cpp file which has only the main
|
|
Could someone tell me what is making it say i cant put an int into the TLIST<int>? I dont know how to fix it, i thought i saw online using ‘const’ would fix it, but i used const, so now i am lost.
Thank you!
> This is my operator+ overloading
> this is the member function of operator+
It should be
operator+=
, shouldn’t it?
|
|
Last edited on
Oh, well I was told to use operator+. this is an assignment for class, I hope that is ok. They said to overload the + operator, and I tried to do it as they showed in class, but i got that specific error time and time again.
I edited the member function to be += like you showed and got this
C2805: Binary operator += has too few parameters.
Also a C2784 error.
> I edited the member function to be += like you showed and got this
|
|
> Also a C2784 error.
The code C2784 won’t be meaningful to someone who does not use the same compiler as you do.
Post the actual text of the error diagnostic.
my apologies!
It was
error C2784: ‘std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘char’
Ignore that std::reverse_iterator<_RanIt> error. It will go away when the earlier error is fixed.
(The compiler tries to match all possibile operator+ that it can see, one by one).
Alright, I looked, because I have 62 errors and most of them seem to be the C2784 error i mentioned above. 3 of them are C2676 which is
error C2676: binary ‘+’ : ‘TLIST<char>’ does not define this operator or a conversion to a type acceptable to the predefined operator
(the reason there are 3 is because I have the ‘char_list’ used in the main.cpp code 3 times.
|
|
One for each one of those code lines above
2 are C2244
C2244: ‘TLIST<t_type>::operator +=’ : unable to match function definition to an existing declaration
EDIT: Should I post my full code here? I do not mind, but I should say, it is probably bad. I just tried to mimic as the teacher taught, but I am really rough with understanding the concepts, and trying my best to.
Last edited on
Declare the member function (operator+=) in the class
|
|
Define the functions:
|
|
Use the functions:
|
|
(I presume that you have written a user-defined assignment operator.)
Alright, I added operator+= to the class,
then i defined both functions.
I now have only 3 errors. It says in the class declaration, it says that
TLIST<t_type> & operator+(TLIST<t_type> lst, const t_type & org);
has error C2804 twice, saying «binary operator+ has too many parameters’.
The third error C2676: binary ‘+’ : ‘TLIST<std::string>’ does not define this operator or a conversion to a type acceptable to the predefined operator
Also: I would like to add a quick and sincere THANK YOU for helping me right now. It means a lot to me. I am not sure how much gratitude matters on this forum, but I want to thank you regardless.
Last edited on
Here is my full code if you care to see (incase it helps)
tlist.cpp
EDIT: I hid some code because I was trying to focus on getting the +operator working first. I have no clue if the other stuff works.
Last edited on
operator+ is not a member function; do not declare it in the class. (Just define it outside the class).
(We declare operator+= in the class because it is a member function.)
i removed it from the class, and got a bunch of those errors you said to ignore, and then 2 new errors.
error C2782: ‘TLIST<t_type> operator +(TLIST<t_type>,const t_type &)’ : template parameter ‘t_type’ is ambiguous
error C2676: binary ‘+’ : ‘TLIST<std::string>’ does not define this operator or a conversion to a type acceptable to the predefined operator
String_List + std::string("hello") + std::string("everyone") etc.
After fixing the other errors:
|
|
http://coliru.stacked-crooked.com/a/7a7f8ddaa7859325
wow you got it to work! Thank you so so much! I will look over this, and see what you did (I notice the (string) on the strings helped a lot. I need to get ‘remove’ to work. Thank you again! I really owe you so much!
Topic archived. No new replies allowed.
The problem occurs in Visual Studio 2005 but works in Vc++6.0
rx_fastset.h
struct SortedAppender
{
typedef Std:: output_iterator_tag iterator_category;
typedef BASE value_type;
typedef ptrdiff_t difference_type;
typedef BASE* pointer;
typedef BASE& reference;
sortedAppender(FastSet& fastSet): m_fastSet(fastSet) {}
sortedAppender& operator=(value_type v) { m_fastSet.sortedAppend(v); return *this;}
sortedAppender& operator*() { return *this; }
sortedAppender& operator++() { return *this; }
sortedAppender& operator++(int) { return *this; }
FastSet& m_fastSet;
};
rx_vist.cpp
inline
void SetUnion(const NodeIdSet& leftSet, const NodeIdSet& rightSet, NodeIdSet& unionSet)
{
assert(unionSet.empty());
std:: set_union(leftSet.begin(), leftSet.end(),
rightSet.begin(), rightSet.end(),
NodeIdSet:: sortedAppender(unionSet));
}
I am getting the following errors..
rx_vist.cpp
c:program filesmicrosoft visual studio 8vcincludealgorithm(3815) : error C2679: binary ‘=’ : no operator found which takes a right-hand operand of type ‘RX:: FastSet<BASE>:: sortedAppender’ (or there is no acceptable conversion)
with
[
BASE=RX::NodeId
]
c:arx_fastset.h(63): could be ‘RX::FastSet<BASE>:: sortedAppender &RX:: FastSet<BASE>:: sortedAppender:: operator =(unsigned int)’
with
[
BASE=RX::NodeId
]
while trying to match the argument list ‘(RX::FastSet<BASE>:: sortedAppender, RX::FastSet<BASE>:: sortedAppender)’
with
[
BASE=RX::NodeId
]
c:program filesmicrosoft visual studio 8vcincludealgorithm(3850) : see reference to function template instantiation ‘_OutIt std:: _Set_union<std:: _Vector_const_iterator<_Ty,_Alloc>,std::_Vector_const_iterator<_Ty,_Alloc>,_OutIt>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt,std::_Range_checked_iterator_tag)’ being compiled
with
[
_OutIt=RX::FastSet<RX::NodeId>:: sortedAppender,
_Ty=RX::NodeId,
_Alloc=std::allocator<RX::NodeId>,
_InIt1=std::_Vector_const_iterator<RX::NodeId,std::allocator<RX::NodeId>>,
_InIt2=std::_Vector_const_iterator<RX::NodeId,std::allocator<RX::NodeId>>
]
c:arx_vist.cpp(77) : see reference to function template instantiation ‘RX::FastSet<BASE>:: sortedAppender std:
et_union<std::_Vector_const_iterator<_Ty,_Alloc>,std::_Vector_const_iterator<_Ty,_Alloc>,RX::FastSet<BASE>:: sortedAppender>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)’ being compiled
with
[
BASE=RX::NodeId,
_Ty=RX::NodeId,
_Alloc=std::allocator<RX::NodeId>,
_InIt1=std::_Vector_const_iterator<RX::NodeId,std::allocator<RX::NodeId>>,
_InIt2=std::_Vector_const_iterator<RX::NodeId,std::allocator<RX::NodeId>>,
_OutIt=RX::FastSet<RX::NodeId>:: sortedAppender
]
c:program filesmicrosoft visual studio 8vcincludealgorithm(3815) : error C2582: ‘operator =’ function is unavailable in ‘RX::FastSet<BASE>:: sortedAppender’
with
[
BASE=RX::NodeId
]
Any help in this would be grateful.