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

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;
 
}

Open in new window

LVL 2
ethnarchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
How did you cerate the class?
what is Integerr, and is it related to Integer?
0
ethnarchAuthor Commented:
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;
        }
        result.AddLast(newInt);
        cout << "newInt=" << newInt <<endl;
    }
 
 
    if(remainder>0){
        result.AddLast(remainder);
        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(){
    number.AddLast(0);
 }
 
 Integer::Integer(const Integer &s){
    cout << "";
    int i;
    for(i=0;i<s.number.NumberOfItems();i++){
        number.AddLast(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.AddLast(c);
    }
 }
 
 Integer::Integer( const Vector<int> &s){
    int i;
    for(i=0;i<s.NumberOfItems();i++){
        number.AddLast(s[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;
 }

Open in new window

0
ozoCommented:
Is "Vector.h" supposed to be the same as std::vector?
what is NumberOfItems?
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ethnarchAuthor Commented:
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
ethnarchAuthor Commented:
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;
 }

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
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>Linking...
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;
 
}

Open in new window

0
ethnarchAuthor Commented:
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
ethnarchAuthor Commented:
Does any have any idea why A=B+C is not working properly in the above code?
0
evilrixSenior Software Engineer (Avast)Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
^^^ it should return by value not reference.
0
ethnarchAuthor Commented:
thanks i'll try that when I get home.
0
ethnarchAuthor Commented:
That was it removing the reference solved the problem.  

const Integer Integer::operator+(Integer &integer01){
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.