0

so i need to overload operators (+, -, *, /) to use it with unsigned char arrays; unsigned char array is a number; i wrote this ( only for summ )

#include <iostream>
#include <string>
using namespace std;

class decimal
{
private:
    unsigned char dec[100];
    size_t size;
public:
    decimal(char* get)
    {
        size = strlen(get);
        for (int i = size - 1; i >= 0; i--, get++)
        {
            dec[i] = *get;
            cout << dec[i];
        }       
        cout << endl;
    }
    friend decimal operator + (decimal const &, decimal const &);
};

decimal operator + (decimal const &a, decimal const &b)
{
    int d = atoi((char *)a.dec) + atoi((char *)b.dec);
    string s = to_string(d);
    return decimal(s.c_str);
}

int main()
{
    decimal a("10004");
    decimal b("12");
    decimal c = a + b;
    system("pause");
    return 0;
}

but it gives me errors

error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': non-standard syntax; use '&' to create a pointer to member
error C2512: 'decimal': no appropriate default constructor available

how can i fix this?

Roman
  • 37
  • 5
  • You could add in a default constructor, like: `decimal() : size{0} {}` – Eljay Apr 06 '18 at 18:28
  • yeah,thanks, but it still doesnt fix first error =( – Roman Apr 06 '18 at 18:30
  • 2
    Change `return decimal(s.c_str);` to `return decimal(s.c_str());` – Richard Critten Apr 06 '18 at 18:31
  • https://stackoverflow.com/questions/46570276/vs2017-non-standard-syntax-use-to-create-a-pointer-to-member, https://stackoverflow.com/questions/45440734/non-standard-syntax-use-to-create-a-pointer-to-member-c, https://stackoverflow.com/questions/46845585/c-visual-studio-non-standard-syntax-use-to-create-a-pointer-to-member – melpomene Apr 06 '18 at 18:31
  • 2
    `std::string::c_str()` is a method not a member – Slava Apr 06 '18 at 18:32
  • i tried to use c_str() as you say but then it gives me this: `error C2440: '': cannot convert from 'const char *' to 'decimal'` – Roman Apr 06 '18 at 18:34
  • 2
    Your `decimal` ctor should accept `const char *` instead, or even better `const std::string &` and why do you copy strings backward? – Slava Apr 06 '18 at 18:34

1 Answers1

0

Change the parameter to the constructor to const...

class decimal
{
private:
    unsigned char dec[100];
    size_t size;
public:
    decimal(const char* get)
    {
        size = strlen(get);
        for (int i = size - 1; i >= 0; i--, get++)

Also, change c_str to c_str().

Better by far, change the constructor parameter from const char* get to const std::string &get) and go from there.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
  • 1
    The first part of the answer is good and right on the topic. It is the second part, beginning with 'better far...` – SergeyA Apr 06 '18 at 19:08