Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 906
  • Last Modified:

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

0
ethnarch
Asked:
ethnarch
  • 7
  • 3
  • 2
2 Solutions
 
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now