2

When I try to assign value to my map, it gives me this error. What's wrong with my constructor? This only happens when I declared the key as BigInt. It works if the key is int, or string.

C2678 binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)

#include <iostream>
#include <vector>
#include <map>
using namespace std;

class BigInt {
    private:
        vector<char> v;
    public:
        BigInt();
        BigInt(string s);
        BigInt(int);
        BigInt(const BigInt& b);
        void print();
};
int main()
{
    cout<<"Hello World";
    BigInt b("12345");
    b.print();
    static map<BigInt,BigInt> m;
    m[b] = b;
    return 0;
}

BigInt::BigInt() {
}
BigInt::BigInt(string s) {
    string:: reverse_iterator rit;
    for(rit = s.rbegin(); rit != s.rend(); rit++){
        v.push_back((int)*rit - '0');
    }

}
BigInt::BigInt(int n) {
    while (n > 9) {
        int digit = n % 10;
        v.push_back(digit);
        n = n/10;
    }
    v.push_back(n);

}

BigInt::BigInt(const BigInt& b) {
    copy(b.v.begin(), b.v.end(), back_inserter(v));
}

void BigInt:: print() {

    vector<char>::reverse_iterator rit;
    for (rit = v.rbegin(); rit != v.rend(); rit++) {
        cout << (int)*rit;
    }

}
BaeBae
  • 31
  • 2
  • 4
    Time to implement `operator<` for your class, presumably. See also [Compare Requirements](https://en.cppreference.com/w/cpp/named_req/Compare) for classes used in `std::map`. – tadman May 05 '20 at 21:42
  • 7
    Does this answer your question? [How can I use std::maps with user-defined types as key?](https://stackoverflow.com/questions/1102392/how-can-i-use-stdmaps-with-user-defined-types-as-key) – Nathan Wride May 05 '20 at 21:43
  • 1
    Given that, you really should have implemented all the relational operators `<`, `<=`, `==`, etc. for the `BigInt` class. It is almost useless if you can't compare a `BigInt` to another `BigInt`. You also do not need the `BigInt` copy constructor, as a `vector` will copy just fine by default. – PaulMcKenzie May 05 '20 at 21:46
  • Thank you, It's working just find now. – BaeBae May 05 '20 at 21:53

0 Answers0