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 constructor\n";
	cout << "t_type has size of " << sizeof(t_type) << " bytes\n\n";
	count = 0;
	capacity = 2;
	DB = new t_type[capacity];

}

template <class t_type>
TLIST<t_type>::~TLIST()
{
	cout << "The destructor has been called\n";
	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 called\n";
	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.