Меню

C2679 ошибка вижуал студия

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's user avatar

tmlen

8,1724 gold badges31 silver badges83 bronze badges

asked Oct 27, 2009 at 14:49

asyncwait's user avatar

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

sbi's user avatar

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

Lightness Races in Orbit's user avatar

answered Mar 9, 2018 at 11:35

Guy Avraham's user avatar

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 Delbar's user avatar

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

Akshat Bhatt's user avatar

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 Joe

    Thanks 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 &param) const;
     FrontalTracing* CreateFrontal(TempStorage &tempStorage, const PatientTracingInitParam &param) const;
     ArchTracing* CreateLowerArch (TempStorage &tempStorage, const PatientTracingInitParam &param) const ;
     ArchTracing* CreateUpperArch (TempStorage &tempStorage, const PatientTracingInitParam &param) 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

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

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:

1
2
3
4
5
6
7
8
9
10
11
struct Krolowie
{...};
std::vector<Krolowie> Wektorek;
(...)
void ShowAll()
{
	for( auto y = Wektorek.begin(); y != Wektorek.end(); y++ )
	{
		std::cout << Wektorek[y] << std::endl;//error in this line
	}
}

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:

1
2
3
for ( const auto & kr : Wektorek ) {
  std::cout << kr << 'n';
}

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<< ? 🙁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <ostream>		
#include <istream>

struct Student{
	std::string name;
	int ID = 0;
        // operator overloading
	friend std::istream &operator>> (std::istream &in, Student &cStudent);
	friend std::ostream &operator<< (std::ostream &out, Student &cStudent);
};
// overload >>
std::istream &operator>> (std::istream &in, Student &cStudent)
{
	in >> cStudent.name;
	in >> cStudent.ID;
	return in;
}
// overload << 
std::ostream &operator<< (std::ostream &out, Student &cStudent)
{
	out << cStudent.name << "nt" << cStudent.ID << "n";
	return out;
}

int main()
{
	Student data;
	std::cin >> data;
	
	std::cout << "You entered:n";
	std::cout << data;    
	
	return 0;
}
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(...)
std::ostream &operator<< (std::ostream &out, Krolowie &cKrolowie)
{
	out << cKrolowie.Imie << "nt" << cKrolowie.Urodzenie << "nt" << cKrolowie.RokRopoczecia << "nt" << cKrolowie.RokZakonczenia << "nt"  << cKrolowie.RokSmierci << "n";
	return out;
}
std::istream &operator>> (std::istream &in, Krolowie &cKrolowie)
{
	in >> cKrolowie.Imie;
	in >> cKrolowie.Urodzenie;
	in >> cKrolowie.RokRopoczecia;
	in >> cKrolowie.RokZakonczenia;
	in >> cKrolowie.RokSmierci;
	return in;
}
(...)
void Wczytaj()
{
	std::cout << "Podaj nazwe pliku z ktorego chcesz wczytac dane (uwaga plik musi znajdowac sie w folderze z programem): " << std::endl;
	std::string plik;
	std::cin >> plik;
	plik = plik + ".txt";
	std::ifstream in( plik );
	if ( in.good() )
	{
		for( auto y = Wektorek.begin(); y != Wektorek.end(); y++ )
		{
			//fin >> Wektorek[y].Imie >> Wektorek[y].Urodzenie >> Wektorek[y].RokRozpoczecia >> Wektorek[y].RokZakonczenia >> Wektorek[y].RokSmierci;
			in >> *y >> std::endl;
		}
	}
	else
	{
		std::cout << "Nie istnieje taki plik. n";
	}
}

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:

1
2
3
4
5
		for( auto y = Wektorek.begin(); y != Wektorek.end(); y++ )
		{
			in >> y->Imie >> y->Urodzenie >> y->RokRozpoczecia >> y->RokZakonczenia >> y->RokSmierci;
			// in >> *y >> std::endl;
		}

I used your code and I got another problem. It compiled, yay! But it doesnt really work 🙁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Wczytaj()
{
	std::cout << "Podaj nazwe pliku z ktorego chcesz wczytac dane (uwaga plik musi znajdowac sie w folderze z programem): " << std::endl;
	std::string plik;
	std::cin >> plik;
	plik = plik + ".txt";
	std::ifstream fin( plik );
	if ( fin.good() )
	{
		for( auto y = Wektorek.begin(); y != Wektorek.end(); y++ )
		{
			fin >> y->Imie >> y->Urodzenie >> y->RokRopoczecia >> y->RokZakonczenia >> y->RokSmierci;
			std::cout << "Cant see that" << std::endl;
		}
		std::cout << "Can see this" << std::endl;
	}
	else
	{
		std::cout << "Nie istnieje taki plik. n";
	}
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>		
#include <string>		
#include <vector>		
#include <fstream>		
#include <cstdlib>		
#include <iomanip>		

using namespace std;

struct Spells{
      string name;
      int cost = 0;
      friend ostream &operator<< (ostream &out, Spells &cSpells);
      friend istream &operator>> (istream &in, Spells &cSpells);
};

ostream &operator<< (ostream &out, Spells &cSpells)
{
	out << setw(20) << left << cSpells.name << " :: " << setw(20) << right << cSpells.cost << endl;
	return out;
}

istream &operator>> (istream &in, Spells &cSpells)
{
	getline(in, cSpells.name);
	in >> cSpells.cost;
	return in;
}

int main(int argc, char **argv)
{
      vector <Spells> spellList;
      Spells *data = new Spells[50];
      ifstream spellFile("spells");
      if(!spellFile){
		  cout << "Can't open file.n";
      }
      int index = 0;
      while(!spellFile.eof())
      {
		  spellFile >> data[index];
		  spellList.push_back(data[index]);
		  spellFile.ignore(1, 'n');
		  index++;
		  if(spellFile.eof())break;
      }
      cout << setw(22) << left << "Spell Name" << setw(22) << right << "Cost in Gold" << endl;
      cout << setw(44) << setfill('=') << "" << setfill(' ') << endl;
      for (unsigned count = 0; count < spellList.size() - 1; count++)
      {
		  cout << spellList[count];
      }
	  
      delete[] data;
      spellFile.close();
      
      return 0;
}
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
istream &operator>> (istream &in, Spells &cSpells)
{
	getline(in, cSpells.name);
	in >> cSpells.cost;
	in.ignore(1, 'n'); // Note: Better place
	return in;
}

int main(int argc, char **argv)
{
      vector <Spells> spellList;
      Spells *data = new Spells[50];
      ifstream spellFile("spells");
      if(!spellFile){
		  cout << "Can't open file.n";
      }
      int index = 0;
      Spells data; // Note
      while(spellFile >> data)
      {
		  spellFile >> data[index];
		  spellList.push_back(data[index]);
		  spellFile.ignore(1, 'n');
		  index++;
		  if(spellFile.eof())break;
      }

@Furjoza
Similar in your case:

1
2
3
4
5
6
7
8
9
Krolowie data;
	while ( fin >> data.Imie >> data.Urodzenie >> data.RokRopoczecia >> data.RokZakonczenia >> data.RokSmierci )
	{
		Wektorek.push_back(data);
	}
	if( !fin.eof() )
	{
		std::cout << "Nie istnieje taki plik. n";
	}

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.

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • C2540 ошибка приус 10
  • C2498 ошибка мерседес w211