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
- Remove From My Forums
-
Question
-
Hi Everyone,
I’m totally new to programming, but I’m trying to learn. I thought I would reach out to the experts out in the community. When I compile an old C++ project in Visual Studio 2010 I get a C2679 error. Like I said, I am total newbee.
I bolded and underlined the line that pulls up as the issue. Here’s a copy of the code:Error 40 error C2679: binary ‘=’ : no operator found which takes a right-hand operand of type ‘const auto_orth_case’ (or there is no acceptable conversion) c:documents and settingsbsmith.rmodesktopjoe32updated joe232x 1.0.0.5 release
20040217includeksfilereader.h 83 1 JoeThanks for the help!
// KsFileReader.h: interface for the KsFileReader class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_KSFILEREADER_H__68B53D77_778B_11D1_A97E_0040051E93CB__INCLUDED_)
#define AFX_KSFILEREADER_H__68B53D77_778B_11D1_A97E_0040051E93CB__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000#include «FileFormatException.h»
#include «CaseData.h»
#include «LateralTracing.h»
#include «FrontalTracing.h»
#include «ArchTracing.h»class KsFileReader {
struct TempStorage {
//
CString m_name;
____ m_sex;
Race m_race;
Race m_norm;//
float m_age;
float m_skeletalAge;
float m_height;COleDateTime m_birthday, m_xRayDate;
ArchInfo m_upperArchInfo, m_lowerArchInfo;bool m_bLipsOpen;
int m_lateralCount;
CPoint m_lateralBuffer[169];int m_frontalCount;
CPoint m_frontalBuffer[123];int m_lowerArchCount;
CPoint m_lowerArchBuffer[29];int m_upperArchCount;
CPoint m_upperArchBuffer[29];
};auto_orth_case m_pOrthCase;
public:
KsFileReader(CArchive &ar);
virtual ~KsFileReader() {}static bool ReadPatientName(CString& patientName, CArchive& ar);
void GetOrthCase(auto_orth_case &pCaseFolder) const;private:
// File Reading
void ProcessCardNo1(TempStorage &tempStorage, PCSTR pszBuf);
void ProcessCardNo2(TempStorage &tempStorage, PCSTR pszBuf);
void ProcessTeethInfoCard(TempStorage &tempStorage, PCSTR pszBuf);static bool IsDigits(char* pszBuf);
static int ReadCardNo(PCSTR pszBuf);
static bool ReadDate(PCSTR pszBuf, COleDateTime &time);
static int ParsePointsCard (PCSTR pszBuf, CPoint* pPoints);static void AppendPoints(CPoint* pBuffer, int &nCount,
const CPoint* pSrc, int nPoints, int nMax);// Build Case object
void ConstructCase(TempStorage &tempStorage);
LateralTracing* CreateLateral(TempStorage &tempStorage, const PatientTracingInitParam ¶m) const;
FrontalTracing* CreateFrontal(TempStorage &tempStorage, const PatientTracingInitParam ¶m) const;
ArchTracing* CreateLowerArch (TempStorage &tempStorage, const PatientTracingInitParam ¶m) const ;
ArchTracing* CreateUpperArch (TempStorage &tempStorage, const PatientTracingInitParam ¶m) const ;
};inline void KsFileReader::GetOrthCase(auto_orth_case &pOrthCase) const {
pOrthCase = m_pOrthCase;
}#endif // !defined(AFX_KSFILEREADER_H__68B53D77_778B_11D1_A97E_0040051E93CB__INCLUDED_)
-
Edited by
Thursday, December 9, 2010 3:02 PM
-
Edited by
Answers
-
Hi Everyone,
Jinzai sent me a comment, which I don’t see posted here that fixed the issue. Thanks everyone.
Bryan
…I think that you will have to cast that reference in any event. m_pOrthCase is an auto_orth_case and you have the const modifier to contend with….
Does this eliminate the error? I tried to recreate a trivial example to test this, but…I did not receive the error that you got and it was not necessary to cast, either. Neither did I receive any
warnings when I cast the variable that was being set. (I used integers so as to avoid having to create a dummy class in a seperate file.)inline void KsFileReader::GetOrthCase(auto_orth_case &pOrthCase) const {
pOrthCase = (auto_orth_case &)m_pOrthCase;The Microsoft Developer Network
————————————————————————
-
Marked as answer by
brjan999
Thursday, December 16, 2010 4:06 PM
-
Marked as answer by
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
- Beginners
- Struct with vector, error C2679.
Struct with vector, error C2679.
Hello,
I would like to display the data which is in my vector. When Im trying to compile my program an error like this apears:
error C2679: binary ‘[‘ : no operator found which takes a right-hand operand of type ‘std::_Vector_iterator<_Myvec>’ (or there is no acceptable conversion)
with
1> [
1> _Myvec=std::_Vector_val<Krolowie,std::allocator<Krolowie>>
1> ]
1> C:Program FilesMicrosoft Visual Studio 10.0VCincludevector(911): could be ‘const Krolowie &std::vector<_Ty>::operator [](unsigned int) const’
1> with
1> [
1> _Ty=Krolowie
1> ]
1> C:Program FilesMicrosoft Visual Studio 10.0VCincludevector(927): or ‘Krolowie &std::vector<_Ty>::operator [](unsigned int)’
1> with
1> [
1> _Ty=Krolowie
1> ]
1> while trying to match the argument list ‘(std::vector<_Ty>, std::_Vector_iterator<_Myvec>)’
1> with
1> [
1> _Ty=Krolowie
1> ]
1> and
1> [
1> _Myvec=std::_Vector_val<Krolowie,std::allocator<Krolowie>>
1> ]
Most crucial elements of my code:
|
|
I would be very thankful for help. 🙂
*y, not Wektorek[y].
The error message has:
a right-hand operand of type 'std::_Vector_iterator<_Myvec>'
and
could be 'const Krolowie &std::vector<_Ty>::operator [](unsigned int) const'
‘Wektorek’ is a vector<Krolowie> and vectors do have an operator[]. Like the message says, that operator expects unsigned int. There are no overloads.
‘y’ has type of … whatever vector::begin() returns. That happens to be an iterator to vector. Rightly so, the error message says ‘_Vector_iterator’.
You can use iterators or you can use [] (with indices).
You could use range-based for loop too:
|
|
To be honest I dont really understand ;( When I replaced Wektorek[y] with *y I recived almost identical error. When I tried to use range-based loop I recieve many errors:
1>Poczet.cpp(96): error C2143: syntax error : missing ‘,’ before ‘:’
1>Poczet.cpp(96): error C2530: ‘kr’ : references must be initialized
1>Poczet.cpp(96): error C3531: ‘kr’: a symbol whose type contains ‘auto’ must have an initializer
1>Poczet.cpp(97): error C2143: syntax error : missing ‘;’ before ‘{‘
Is it possible that my compiler is «to old» for that?
When I replaced Wektorek[y] with *y I recived almost identical error.
You probably didn’t define an overload for operator<<(std::ostream &, Krolowie &). You can’t just send any type to an output stream.
Yes. Range-based for-loop syntax is in C++11 and not all compilers support that. (MSVC 2012 and 2013 do document it.)
And how do I define an overload for operator<< ? 🙁
|
|
James 2859324 You entered: James 2859324 |
Isnt there any easier way to do this? It looks a bit complicated. Isnt it possible to i/o data like std::cout << Vector[y].ID << Vector[y].Name instead of *y? I used that operator overload and output seems to work but there is an error with input 🙁 What I got:
|
|
The error:
1>Poczet.cpp(141): error C2678: binary ‘<<‘ : no operator found which takes a left-hand operand of type ‘std::ifstream’ (or there is no acceptable conversion)
1> Poczet.cpp(18): could be ‘std::ostream &operator <<(std::ostream &,Krolowie &)’
1> while trying to match the argument list ‘(std::ifstream, Krolowie)’
Sorry for other language than English. If its annoying Ill change it, but I think it doesnt really matter.
Last edited on
Isnt there any easier way to do this? It looks a bit complicated. Isnt it possible to i/o data like std::cout << Vector[y].ID << Vector[y].Name instead of *y?
Yes:
|
|
I used your code and I got another problem. It compiled, yay! But it doesnt really work 🙁
|
|
It looks like it doesnt get into the for loop. I got this vector as global variable. But by default it has length of 0 yes? So I guess I should add at the beginning of this function something that will make some space in the vector. Maybe I should use .push_back inside this loop?
Last edited on
Here is some sample code I have that may help you (lines 40 — 46), but included full code and file for you to experiment with if you want.
|
|
Spell Name Cost in Gold ============================================ Thunder :: 100 Fire :: 100 Ice :: 100 Water :: 100 Heal :: 100 Thunder II :: 500 Fire II :: 500 Ice II :: 500 Water II :: 500 Heal II :: 500 Thunder III :: 1000 Fire III :: 1000 Ice III :: 1000 Water III :: 1000 Heal III :: 1000 Ultima :: 2000 Meteor :: 2000 |
spells (file)
Thunder 100 Fire 100 Ice 100 Water 100 Heal 100 Thunder II 500 Fire II 500 Ice II 500 Water II 500 Heal II 500 Thunder III 1000 Fire III 1000 Ice III 1000 Water III 1000 Heal III 1000 Ultima 2000 Meteor 2000
Last edited on
@BHX Specter
Using eof() like you did is a typical beginner error. You shouldn’t use ignore like you did. Better like so:
|
|
@Furjoza
Similar in your case:
|
|
But overloading the operator<< and operator>> for Krolowie seems not the worst idea ever
Last edited on
@coder777
Yeah, I should have took more time to tweak the code since I was offering it as an example. The original code was for me to learn using new and delete when I was playing with an terminal RPG when I first started programming. The operator overloading, and vector wasn’t in the original code so I added them real fast and copy/pasted chunks, but should have removed the obsolete code and double checked the logic before submitting it. As for the ‘beginner error’, that is the first time in all these years I’ve seen anything about that as I’ve always done !infile.eof() or infile.eof().
[EDIT] Forgot the second variant infile.eof().
[EDIT.:2:.] I see what you mean, an acquaintance pointed it out to me. What you call a «beginner error» is actually a common error because programmers sometimes forget that in order to use infile.eof(), as I intended there, you MUST read in from infile at least once before checking it for eof(). Another way to fix it (rather than doing while(infile >> data) as you did) you can either add two needless calls to infile >> data; and vector.push_back(data); before the while() or simply switch my original while to a do{}while(); so that it at least read in from infile once.
Last edited on
Thanks for your replies guys. It works 😀 I made it the way cover777 has shown 😉
Topic archived. No new replies allowed.