Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# error: no match for 'operator=' in 'R = (&B)->Integer::operator+(((Integer&)(&C)))

Posted on 2009-07-12
Medium Priority
888 Views
I am getting this error:
error: no match for 'operator=' in 'R = (&B)->Integer::operator+(((Integer&)(&C)))

in an integer class I created.

I have an = operator as well as a + operator implemented

const Integer& Integer::operator+(Integer &integer01);
const Integerr& Integer::operator=(Integer &integer01);

Can someone point me in the correct direction on what operators I should have implemented to make the code below work correctly.

Integer A = "1200";
Integer B = "1400";
Integer C;

C = A+B;

I also posted the main function which is throwing the error above.
``````int main(int argc, char *argv[])
{
Integer B("123");
B.print();
Integer C = "567";
C.print();

Integer R;
R = B+C;
R.print();
cout << "done" << endl;

}
``````
0
Question by:ethnarch
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 7
• 3
• 2

LVL 84

Expert Comment

ID: 24835809
How did you cerate the class?
what is Integerr, and is it related to Integer?
0

LVL 2

Author Comment

ID: 24835850
Integerr was just a typo sorry about that.

Below is the .cpp and the .h files for the implementation of Integer
``````//Integer.h
#ifndef Integer_H_INCLUDED
#define Integer_H_INCLUDED
#include "Vector.h"
#include <string>
#include <assert.h>

using namespace std;
class Integer
{
public:
//Operators
const Integer& Integer::operator+(Integer &integer01);
const Integer& Integer::operator=(Integer &integer01);

//Constructors
Integer::Integer();
Integer::Integer(const Integer &s);
Integer::Integer(const char *s);
Integer::Integer(const Vector<int> &s);

//Member Functions
void Integer::print();
int Integer::size();
Vector<int>& Integer::getVector();
private:
Vector<int> number;

};
#endif // Integer_H_INCLUDED

//Integer.cpp
#include "Integer.h"
#include "Vector.h"
#include <string>
#include <assert.h>
#include <iostream>

using namespace std;

const Integer& Integer::operator=(Integer &integer01){
number = integer01.number;
}
const Integer& Integer::operator+(Integer &integer01){
Vector<int> n1;
Vector<int> n2;
Vector<int> result;

if(integer01.size()>number.NumberOfItems()){
n1 = integer01.getVector();
n2 = getVector();
}else{
n1 = getVector();
n2 = integer01.getVector();
}

int remainder = 0;
int numOfItems = n2.NumberOfItems();
int i = numOfItems-1;
int tmpIntArray[i];
cout << endl << "numOfItems="<<numOfItems<<endl;
for(i = 0;i<n2.NumberOfItems();i++)
{
cout<< "i=" <<i<<endl;
cout<< endl << "n1[i]="<<n1[i] << " n2[i]=" << n2[i] << " remainder="<<remainder<<endl;
int newInt = n1[i] + n2[i] + remainder;
if(newInt/10>=1){
remainder = newInt/10;
newInt = newInt%10;
}else{
remainder = 0;
}
cout << "newInt=" << newInt <<endl;
}

if(remainder>0){
cout << "added remainder of "<<remainder <<endl;
}

int length = result.NumberOfItems();
int i2;
cout<< "Result=";
for(i2=length-1; i2>=0; i2--)
{
cout<< result[i2];
}
cout<<  endl;

Integer r(result);
return r;
}

Integer::Integer(){
}

Integer::Integer(const Integer &s){
cout << "";
int i;
for(i=0;i<s.number.NumberOfItems();i++){
}
}

Integer::Integer( const char *s){
int i;
for(i = strlen(s)-1; i >=0 ; i--)
{
char chr = s[i];
int c = chr - '0';
cout<<c<<endl;

}
}

Integer::Integer( const Vector<int> &s){
int i;
for(i=0;i<s.NumberOfItems();i++){
}
}

int Integer::size(){
return number.NumberOfItems();
}

Vector<int>& Integer::getVector(){
return number;
}

void Integer::print(){
int length = number.NumberOfItems();
int i;
for(i=0; i<length; i++)
{
cout<< number[i];
}
cout<<endl;
}
``````
0

LVL 84

Expert Comment

ID: 24835970
Is "Vector.h" supposed to be the same as std::vector?
what is NumberOfItems?
0

LVL 2

Author Comment

ID: 24836146
no it's not std::vector

but feel free to substitute that for it.  Number of items is just the number of elements in the Vector.  You can substitute size for numberOfElements
0

LVL 2

Author Comment

ID: 24836365
ok here is an update of the code without Vector, I started using microsofts c++ ide instead hoping the compiler would give me better messages.

I don't get the error above anymore but the line below does not seem to function correctly as the number contained in A is empty when it should have 690 in it.
A = B+C;

Here is the new code.  Hopefully this is clearer.
``````//main.cpp
#include <string>
#include "Integer.h"
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
Integer *integer;

Integer B("123");
B.print();
Integer C = "567";
C.print();

Integer A;

cout << "B+C BEGIN" << endl;
A = B + C;
A.print();
cout << "done" << endl;

}

//Integer.h
#ifndef INTEGER_H_INCLUDED
#define INTEGER_H_INCLUDED
#include <vector>
#include <string>
#include <assert.h>

using namespace std;
class Integer
{
public:
//Operators
const Integer& Integer::operator+(Integer &integer01);
const Integer& Integer::operator=(Integer integer01);

//Constructors
Integer::Integer();
Integer::Integer(const Integer &s);
Integer::Integer(const char *s);
Integer::Integer(const vector<int> &s);

//Member Functions
void Integer::print();
int Integer::size();
vector<int>& Integer::getvector();
private:
vector<int> number;

};
#endif // INTEGER_H_INCLUDED

//Integer.cpp
#include "Integer.h"
#include <vector>
#include <string>
#include <assert.h>
#include <iostream>

using namespace std;

const Integer& Integer::operator=(Integer integer01){
number = integer01.number;
return *this;
}
const Integer& Integer::operator+(Integer &integer01){
vector<int> n1;
vector<int> n2;
vector<int> result;

if(integer01.size()>number.size()){
n1 = integer01.getvector();
n2 = getvector();
}else{
n1 = getvector();
n2 = integer01.getvector();
}

int remainder = 0;
int numOfItems = n2.size();
int i = numOfItems-1;
cout << endl << "numOfItems="<<numOfItems<<endl;
for(i = 0;i<n2.size();i++)
{
cout<< "i=" <<i<<endl;
cout<< endl << "n1[i]="<<n1[i] << " n2[i]=" << n2[i] << " remainder="<<remainder<<endl;
int newInt = n1[i] + n2[i] + remainder;
if(newInt/10>=1){
remainder = newInt/10;
newInt = newInt%10;
}else{
remainder = 0;
}
result.push_back(newInt);
cout << "newInt=" << newInt <<endl;
}

if(remainder>0){
result.push_back(remainder);
cout << "added remainder of "<<remainder <<endl;
}

int length = result.size();
int i2;
cout<< "Result=";
for(i2=length-1; i2>=0; i2--)
{
cout<< result[i2];
}
cout<<  endl;

Integer r(result);
return r;
}

Integer::Integer(){
number.push_back(0);
}

Integer::Integer(const Integer &s){
cout << "";
int i;
for(i=0;i<s.number.size();i++){
number.push_back(s.number[i]);
}
}

Integer::Integer( const char *s){
int i;
for(i = strlen(s)-1; i >=0 ; i--)
{
char chr = s[i];
int c = chr - '0';
cout<<c<<endl;

number.push_back(c);
}
}

Integer::Integer( const vector<int> &s){
int i;
for(i=0;i<s.size();i++){
int tmpInt = s.at(i);
number.push_back(tmpInt);
}
}

int Integer::size(){
return number.size();
}

vector<int>& Integer::getvector(){
return number;
}

void Integer::print(){
int length = number.size();
int i;
for(i=0; i<length; i++)
{
cout<< number[i];
}
cout<<endl;
}
``````
0

LVL 40

Expert Comment

ID: 24837600
Apart from a few warnings, the code you posted above compiles fine for me with VS2005.

1>------ Build started: Project: scratchcpp, Configuration: Release Win32 ------
1>Compiling...
1>scratchcpp.cpp
1>.\scratchcpp.cpp(52) : warning C4018: '>' : signed/unsigned mismatch
1>.\scratchcpp.cpp(61) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
1>.\scratchcpp.cpp(64) : warning C4018: '<' : signed/unsigned mismatch
1>.\scratchcpp.cpp(85) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
1>.\scratchcpp.cpp(95) : warning C4172: returning address of local variable or temporary
1>.\scratchcpp.cpp(105) : warning C4018: '<' : signed/unsigned mismatch
1>.\scratchcpp.cpp(112) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
1>.\scratchcpp.cpp(124) : warning C4018: '<' : signed/unsigned mismatch
1>.\scratchcpp.cpp(131) : warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data
1>.\scratchcpp.cpp(139) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
1>.\scratchcpp.cpp(158) : warning C4101: 'integer' : unreferenced local variable
1>Generating code
1>Finished generating code
1>Embedding manifest...
1>Build log was saved at "file://Documents\Visual Studio 2005\Projects\scratchcpp\scratchcpp\Release\BuildLog.htm"
1>scratchcpp - 0 error(s), 11 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
``````
//Integer.h
#ifndef INTEGER_H_INCLUDED
#define INTEGER_H_INCLUDED
#include <vector>
#include <string>
#include <assert.h>

using namespace std;
class Integer
{
public:
//Operators
const Integer& Integer::operator+(Integer &integer01);
const Integer& Integer::operator=(Integer integer01);

//Constructors
Integer::Integer();
Integer::Integer(const Integer &s);
Integer::Integer(const char *s);
Integer::Integer(const vector<int> &s);

//Member Functions
void Integer::print();
int Integer::size();
vector<int>& Integer::getvector();
private:
vector<int> number;

};
#endif // INTEGER_H_INCLUDED

//Integer.cpp
//#include "Integer.h"
#include <vector>
#include <string>
#include <assert.h>
#include <iostream>

using namespace std;

const Integer& Integer::operator=(Integer integer01){
number = integer01.number;
return *this;
}
const Integer& Integer::operator+(Integer &integer01){
vector<int> n1;
vector<int> n2;
vector<int> result;

if(integer01.size()>number.size()){
n1 = integer01.getvector();
n2 = getvector();
}else{
n1 = getvector();
n2 = integer01.getvector();
}

int remainder = 0;
int numOfItems = n2.size();
int i = numOfItems-1;
cout << endl << "numOfItems="<<numOfItems<<endl;
for(i = 0;i<n2.size();i++)
{
cout<< "i=" <<i<<endl;
cout<< endl << "n1[i]="<<n1[i] << " n2[i]=" << n2[i] << " remainder="<<remainder<<endl;
int newInt = n1[i] + n2[i] + remainder;
if(newInt/10>=1){
remainder = newInt/10;
newInt = newInt%10;
}else{
remainder = 0;
}
result.push_back(newInt);
cout << "newInt=" << newInt <<endl;
}

if(remainder>0){
result.push_back(remainder);
cout << "added remainder of "<<remainder <<endl;
}

int length = result.size();
int i2;
cout<< "Result=";
for(i2=length-1; i2>=0; i2--)
{
cout<< result[i2];
}
cout<<  endl;

Integer r(result);
return r;
}

Integer::Integer(){
number.push_back(0);
}

Integer::Integer(const Integer &s){
cout << "";
int i;
for(i=0;i<s.number.size();i++){
number.push_back(s.number[i]);
}
}

Integer::Integer( const char *s){
int i;
for(i = strlen(s)-1; i >=0 ; i--)
{
char chr = s[i];
int c = chr - '0';
cout<<c<<endl;

number.push_back(c);
}
}

Integer::Integer( const vector<int> &s){
int i;
for(i=0;i<s.size();i++){
int tmpInt = s.at(i);
number.push_back(tmpInt);
}
}

int Integer::size(){
return number.size();
}

vector<int>& Integer::getvector(){
return number;
}

void Integer::print(){
int length = number.size();
int i;
for(i=0; i<length; i++)
{
cout<< number[i];
}
cout<<endl;
}

//main.cpp
#include <string>
//#include "Integer.h"
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
Integer *integer;

Integer B("123");
B.print();
Integer C = "567";
C.print();

Integer A;

cout << "B+C BEGIN" << endl;
A = B + C;
A.print();
cout << "done" << endl;

}
``````
0

LVL 2

Author Comment

ID: 24838703
evilrix--
The code does compile but I believe the operator's I have set up are wrong somehow.  A=B+C does not work with that code.

When I am debugging inside the operator+ it seems to function correctly and the correct answer of 690 is returned.

In the = operator though the value passed into it is empty for some reason the integer returned from the operator+ is not passed the operator=.
0

LVL 2

Author Comment

ID: 24854407
Does any have any idea why A=B+C is not working properly in the above code?
0

LVL 40

Accepted Solution

evilrix earned 2000 total points
ID: 24854451
operator + is returning a reference to a local object that is going out of scope. I don't know if that is your problem but it is A problem.

const Integer& Integer::operator+(Integer &integer01){

....

Integer r(result);
return r;
}
0

LVL 40

Assisted Solution

evilrix earned 2000 total points
ID: 24854462
^^^ it should return by value not reference.
0

LVL 2

Author Comment

ID: 24854471
thanks i'll try that when I get home.
0

LVL 2

Author Comment

ID: 24855108
That was it removing the reference solved the problem.

const Integer Integer::operator+(Integer &integer01){
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to thâ€¦
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month7 days, 10 hours left to enroll