Меню

Ошибка 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

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+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class t_type>
TLIST<t_type> & operator+(const TLIST<t_type> & org)
{
	if (count < capacity)
	{
		DB[count++] = org;
	}
	else
	{
		Double_Size();
		(*this) + org;

	}
}

this is in the other .cpp file which has only the main

1
2
	TLIST<char> Char_List, TempChar1, TempChar2; //default called
	Char_List + 'W' + 'E' + 'L' + 'C' + 'O' + 'M' + 'E';

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// member function
template <class t_type> // note: org is of type t_type ( not TLIST<t_type> )
TLIST<t_type>& operator+= ( const t_type & org )
{
	if (count < capacity)
	{
		DB[count++] = org;
                return *this ;
	}
	else
	{
		Double_Size();
		return *this += org;
	}
}


// non-member function
template <class t_type> // note: lst is passed by value
TLIST<t_type> operator+ ( TLIST<t_type> lst, const t_type & org )
{ return lst += org ; }

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

1
2
3
4
5
6
7
// member function
template <class t_type> // note: org is of type t_type ( not TLIST<t_type> )
// TLIST<t_type>& operator+= ( const t_type & org )
TLIST<t_type>& TLIST<t_type>::operator+= ( const t_type & org ) 
{
     // ... 
}

> 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.

1
2
3
	Char_List + 'W' + 'E' + 'L' + 'C' + 'O' + 'M' + 'E';
	Int_List + 1 + 2 + 3 + 4 + 5 + 6 + 7;
	String_List + "hello" + "everyone" + "study" + "hard" + "do not" + "copy" + "learn";

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

1
2
3
4
5
6
7
8
9
10
template <class t_type>
class TLIST
{
    // ....
 
    public:
       TLIST<t_type>& operator+= ( const t_type & org ) ; // ****

       // ....
};

Define the functions:

1
2
3
4
5
6
7
8
9
template <class t_type> 
TLIST<t_type>& TLIST<t_type>::operator+= ( const t_type & org ) 
{
     // ... 
}

template <class t_type>
TLIST<t_type> operator+ ( TLIST<t_type> lst, const t_type & org )
{ /* ... */; }

Use the functions:

1
2
3
Char_List = Char_List + 'W' + 'E' + 'L' + 'C' + 'O' + 'M' + 'E';
Int_List = Int_List + 1 + 2 + 3 + 4 + 5 + 6 + 7;
String_List = String_List + "hello" + "everyone" + "study" + "hard" + "do not" + "copy" + "learn";

(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:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <string>

using namespace std;

template <class t_type>
class TLIST
{

public:

    TLIST();
	~TLIST();
	TLIST( const TLIST<t_type> &); // *** const added
	bool IsEmpty();
	bool IsFull();
	int Search(t_type);
	void Double_Size();
	void Add(t_type item);
	void Remove(const t_type org);
	TLIST<t_type> & operator=(const TLIST<t_type> & org) ;

	TLIST<t_type> & operator+=(const t_type & org);

	friend ostream & operator<<(ostream & out, TLIST<t_type> & org)
	{
		int i;

		for (i = 0; i<org.count; i++)
		{
			out << "DB[i" << i << "] = " << org.DB[i] << endl;
		}
		return out;
	}


private:

	t_type *DB;
	int count;
	int capacity;

};

template <class t_type>
TLIST<t_type>::TLIST()
{
	cout << "You're inside the default constructorn";
	cout << "t_type has size of " << sizeof(t_type) << " bytesnn";
	count = 0;
	capacity = 2;
	DB = new t_type[capacity];

}

template <class t_type>
TLIST<t_type>::~TLIST()
{
	cout << "The destructor has been calledn";
	delete[] DB;
	count = 0;
	DB = 0;
}

template <class t_type>
TLIST<t_type>::TLIST( const TLIST<t_type> & org) // *** const added
{
	count = org.count;
	capacity = org.capacity;
	DB = new t_type[capacity];
	for (int i = 0; i<count; i++)
	{
		DB[i] = org.DB[i];
	}
}


template <class t_type>
int TLIST<t_type>::Search(t_type item)
{
	// int i;
	for (int i = 0; i < count; i++)
	{
		if (item == DB[i])
		{
			return i;
		}
		return -1;
	}
	DB = 0;
}


template <class t_type>
void TLIST<t_type>::Add(t_type item)
{
	if (count < capacity)
	{
		DB[count++] = item;
	}
	else
	{
		Double_Size();
		Add(item);
	}
}

template <class t_type>
bool TLIST<t_type>::IsEmpty()
{
    /*
	if (count == 0)
	{
		return TRUE;
	}
	return FALSE;
	*/
	return count == 0 ;
}

template <class t_type>
bool TLIST<t_type>::IsFull()
{
    /*
	if (count == capacity)
	{
		return TRUE;
	}
		return FALSE;
    */
    return count == capacity ;
}


template <class t_type>
TLIST<t_type> operator+ (TLIST<t_type> lst, const t_type & org)
{
	return lst += org;
}


template <class t_type>
void TLIST<t_type>::Double_Size()
{
	cout << endl << endl;
	cout << "Double_Size has been calledn";
	cout << "Old Size = " << capacity << endl;
	cout << "New Size = " << capacity * 2 << endl;

	capacity *= 2;
	t_type *temp = new t_type[capacity];
	for (int i = 0; i < count; i++)
	{
		temp[i] = DB[i];
	}
	delete[] DB;
	DB = temp;
}

template <class t_type>
TLIST<t_type> & TLIST<t_type>::operator=(const TLIST<t_type> & org)
{
	if (this != &org)
	{
		count = org.count;
		capacity = org.capacity;
		DB = new t_type[capacity];
		for (int i = 0; i < count; i++)
		{
			DB[i] = org.DB[i];
		}
		//return *this;
	}
	return *this ;
}

template <class t_type>
TLIST<t_type>& TLIST<t_type>::operator+= (const t_type & org)
{
	if (count < capacity)
	{
		DB[count++] = org;
		return *this;
	}
	else
	{
		Double_Size();
		return *this += org;
	}
}

// main.cpp
// #include ....

int main()
{
	TLIST<char> Char_List ;
	Char_List = Char_List + 'W' + 'E' + 'L' + 'C' + 'O' + 'M' + 'E';
	std::cout << Char_List << 'n' ;


	TLIST<int> Int_List ;
	Int_List = Int_List + 1 + 2 + 3 + 4 + 5 + 6 + 7;
	std::cout << Int_List << 'n' ;

	TLIST<string> String_List ;
	String_List = String_List + std::string("hello") + std::string("everyone") +
	                            std::string("study") + std::string("hard") +
	                            std::string("do not") + std::string("copy") +
	                            std::string("learn");
	std::cout << String_List << 'n' ;
}

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:Tongue Tiedet_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.

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

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

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

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Ошибка c2589 недопустимая лексема справа от
  • Ошибка c1611 киа сид