Solved

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

Posted on 2009-07-12
12
856 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:ethnarch
  • 7
  • 3
  • 2
12 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
How did you cerate the class?
what is Integerr, and is it related to Integer?
0
 
LVL 2

Author Comment

by:ethnarch
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Is "Vector.h" supposed to be the same as std::vector?
what is NumberOfItems?
0
 
LVL 2

Author Comment

by:ethnarch
Comment Utility
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

by:ethnarch
Comment Utility
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
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:ethnarch
Comment Utility
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

by:ethnarch
Comment Utility
Does any have any idea why A=B+C is not working properly in the above code?
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
Comment Utility
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

by:evilrix
evilrix earned 500 total points
Comment Utility
^^^ it should return by value not reference.
0
 
LVL 2

Author Comment

by:ethnarch
Comment Utility
thanks i'll try that when I get home.
0
 
LVL 2

Author Comment

by:ethnarch
Comment Utility
That was it removing the reference solved the problem.  

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now