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,3485 gold badges31 silver badges84 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

219k46 gold badges256 silver badges443 bronze badges

10

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,4323 gold badges37 silver badges50 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

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

1111 gold badge2 silver badges9 bronze badges

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Compiler Error C2679

Compiler Error C2679

11/04/2016

C2679

C2679

1a5f9d00-9190-4aa6-bc72-949f68ec136f

Compiler Error C2679

binary ‘operator’ : no operator found which takes a right-hand operand of type ‘type’ (or there is no acceptable conversion)

To use the operator, you must overload it for the specified type or define a conversion to a type for which the operator is defined.

The following sample generates C2679:

// C2679.cpp
class C {
public:
   C();   // no constructor with an int argument
} c;

class D {
public:
   D(int) {}
   D(){}
} d;

int main() {
   c = 10;   // C2679
   d = 10;   // OK
}

HI. Before my question, I’ve looked in other threads with same error, nothing worked for me.
My problem code is at in >> a.name >> a.extension;. As I tested myself, if I change the type from string to char for my variables, it will work, but I can’t make it work with a string type of value.

Am I doing something wrong?
Below full error code on compillation (Visual Studio 2015)

Error C2679 binary ‘>>’: no operator found which takes a right-hand
operand of type ‘const std::string’ (or there is no acceptable
conversion)

Thanks in advance.

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

class Soft {
private:
    int *size;
    time_t *dateTime;
public:
    string name;
    string extension;
    Soft();
    Soft(string, string);
    Soft(const Soft&source);
    ~Soft();

    friend istream& operator>> (istream& in, const Soft& a) 
    {
        in >> a.name >> a.extension;
        return in;
    };

    void changeName(string);
    void changeExtension(string);
};

Soft::Soft() {
    size = new int;
    *size = 0;
    name = string("");
    extension = string("");
    dateTime = new time_t;
    *dateTime = time(nullptr);
}

Soft::Soft(const Soft&source) {
    name = source.name;
    extension = source.extension;
    size = new int;
    *size = *source.size;
    dateTime = new time_t;
    *dateTime = time(nullptr);
}

Soft::Soft(string a, string b) {
    name = a;
    extension = b;
    dateTime = new time_t;
    *dateTime = time(nullptr);
}

Soft::~Soft() {
    delete[] size;
    delete[] dateTime;
}

void Soft::changeExtension(string a) {
    extension = a;
}
void Soft::changeName(string a) {
    name = a;
}


int main() {

    Soft a;

    getchar();
    getchar();
    return 0;
}

asked Oct 1, 2016 at 14:24

M. Eugen's user avatar

0

The key word here is const, which means that the thing cannot be modified.

You are trying to modify a const thing. You cannot do that.

Your function, like any operator>> in general, should be declared like this:

friend istream& operator>>(istream& in, Soft& a) 

The change I have made is to remove the const.

By the way, I see no reason at all for your member variables size and dateTime to be pointers to dynamically allocated integers. Your code will be much simpler if you just make them normal integers.

answered Oct 1, 2016 at 14:27

Lightness Races in Orbit's user avatar

5

Assignment operator’s l-Value mustn’t be constant because this operator changes the values so trying to change a constant value is a trying to break the rules.

look at insertion operator:

ostream& operator <<(ostream&, const myCalss&); // here const is ok

here it’s ok because the insertion operator is just used to print values (const and non-const) without changing them.

*** if your really need to Break the rules of constness declare your member data as mutable:

     mutable int *size;
     mutable time_t *dateTime;

but in your example you needn’t to do so, I just explain that you can change cont variable.

answered Oct 1, 2016 at 15:05

Raindrop7's user avatar

Raindrop7Raindrop7

3,8893 gold badges16 silver badges27 bronze badges

Hey Spiceworks Users,

I couldn’t find a C/C++ Programming group when choosing a group for this question, so I apologize in advance.

I am having to go through someone’s sloppy code and convert a program that was made in Visual Studio 6 to be useable in Visual Studio 2010, and I’ve hit this wall that I can’t seem to get over. 

Here is the error I’m getting…

  • c:\program files\microsoft visual studio 10.0\vc\include\utility(217): error C2679: binary ‘=’ : no operator found which takes a right-hand operand of type ‘const CFGArray’ (or there is no acceptable conversion)
    1>          c:\work\pb\library\vc++\config.h(22): could be ‘CFGArray &CFGArray::operator =(CFGArray &)’
    1>          while trying to match the argument list ‘(CFGArray, const CFGArray)’
    1>          c:\program files\microsoft visual studio 10.0\vc\include\utility(215) : while compiling class template member function ‘std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(const std::pair<_Ty1,_Ty2> &)’
    1>          with
    1>          [
    1>              _Ty1=CString,
    1>              _Ty2=CFGArray
    1>          ]
    1>          c:\work\pb\library\vc++\config.cpp(39) : see reference to class template instantiation ‘std::pair<_Ty1,_Ty2>’ being compiled
    1>          with
    1>          [
    1>              _Ty1=CString,
    1>              _Ty2=CFGArray
    1>          ]

Here is, what I think, is the relevant code…

Config.h

  • class CFGArray : public CArray<cfgValue, cfgValue>{
    public:
    CFGArray();
    CFGArray(const CFGArray &copy);
    CFGArray& operator= (CFGArray&); // This is line Line 22
    };

Config.cpp

  • CFGArray::CFGArray(){this->RemoveAll();}
    CFGArray::CFGArray(const CFGArray &copy){
    this->RemoveAll();
    int i;
    for(i = 0; i < copy.GetSize(); ++i)
      this->Add(copy[i]);
    }
    CFGArray& CFGArray::operator= (CFGArray& rhs){
    if (this != &rhs) {
      int i;
      this->RemoveAll();
      for(i = 0; i < rhs.GetSize(); ++i)
       this->Add(rhs[i]);
    }
    return *this;
    }

Any idea how I can fix this?

HiHelios,

Are you sure the rest of the code in the loop does what you think it does? What it really does is not swap two numbers. It’s swapping two vectors; two rows, if you will.

Can’t I use iter_swap() function to swap numbers?

Hilastchance,

I think it would be easier to advise if you gave us VERBATIM the bit of your assignment BEFORE what you have quoted: we are only getting a very confused story and it is unclear what you are being asked.

Sorry for inconvenience. I was given a task to generate two random numbers between 3 to 9.
For example: first random number = 5 and second random number = 9.

Then, I need to find the position of these random number for each row vector as follow:
{ 9, 8, 7, 6, 5, 4, 3, 2, 1 } —> first random number = position 5, second random number = position 1
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } —> first random number = position 5, second random number = position 9
{ 5, 4, 3, 2, 1, 9, 8, 7, 6 } —> first random number = position 1, second random number = position 6.

Then, swap these random numbers for each row
{ 9, 8, 7, 6, 5, 4, 3, 2, 1 } —> { 5, 8, 7, 6, 9, 4, 3, 2, 1 }
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } —> { 1, 2, 3, 4, 9, 6, 7, 8, 5 }
{ 5, 4, 3, 2, 1, 9, 8, 7, 6 } —> { 9, 4, 3, 2, 1, 5, 8, 7, 6 }

Возможно, вам также будет интересно:

  • C2678 ошибка приус 10
  • C2600 ошибка приус 10
  • C2579 ошибка приус 10
  • C2558 konica minolta 185 сброс ошибки
  • C2544 ошибка приус 10

  • Понравилась статья? Поделить с друзьями:
    0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

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