2

I am trying to run a code that defines objects that are a collection of English letters. I dont know why it does not compile.

I have tried to change from int to const int but it is not the case,

and also added the disable 4996 message but it didnt help.

#include <iostream>

using namespace std;


class CharSet
{
    int size;
    char* pSet;
public:
    // -----------------------------------
    CharSet(int const size, char* set)
    {
        this->size = size;
        pSet = new char[strlen(set) + 1];
        strcpy(pSet, set);
    }
    // -----------------------------------

    ~CharSet()
    {
        delete[] pSet;
    }
    // -----------------------------------

    CharSet operator*(const CharSet & other)
    {
        int maxSize = 0;
        if (this->size >= other.size)
            maxSize = this->size;
        else
            maxSize = other.size;

        char * ret = new char[maxSize + 1];
        char temp;
        int index = 0;
        for (int i = 0; i < this->size; i++)
        {
            temp = this->pSet[i];
            for (int j = 0; j < other.size; j++)
            {
                if (other.pSet[j] == temp)
                {
                    ret[index] = temp;
                    index++;
                }
            }
        }
        ret[index] = '\0';

        return CharSet(maxSize, ret);
    }

    // -----------------------------------

    bool operator()(char check)
    {
        bool flag = false;
        for (int i = 0; i < this->size; i++)
        {
            if (pSet[i] == check)
                flag = true;
        }
        return flag;
    }

    // -----------------------------------

    friend ostream& operator<<(ostream& os, const CharSet& s)
    {
        os << s.pSet;
        return os;
    }

    // -----------------------------------
};

int main()
{
    CharSet s1(4, "DAQP"), s2(3, "AXZ");
    cout << s1 * s2 << endl;
    if (s1('Q') == true)
        cout << "The element is member of the set" << endl;
    else
        cout << "The element is not member of the set" << endl;
    return 0;
}

errors:

  1. E0289 no instance of constructor "CharSet::CharSet" matches the argument
  2. E0289 no instance of constructor "CharSet::CharSet" matches the argument list
  3. C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
  4. C2664 'CharSet::CharSet(const CharSet &)': cannot convert argument 2 from
  5. C2664 'CharSet::CharSet(const CharSet &)': cannot convert argument 2 from 'const char [4]' to 'char *'

1 Answers1

2

you need a const char* in your constructor:

CharSet(int const size, const char* set)

Thanks to @holy black cat "DAQP" is a const char[] which you didn't provide a constructor for that(the array will implicitly convert to pointer).


A better way is using std::string:

class CharSet
{
 std::string pSet;
public:
// -----------------------------------
CharSet(std::string set) : pSet(set)
{
}
// -----------------------------------

~CharSet()
{
}
// -----------------------------------

CharSet operator*(const CharSet & other)
{
    int maxSize = 0;

    std::string ret;
    char temp;
    int index = 0;
    for (int i = 0; i < pSet.size(); i++)
    {
        temp = pSet[i];
        for (int j = 0; j < other.pSet.size(); j++)
        {
            if (other.pSet[j] == temp)
            {
                ret += temp;
                index++;
            }
        }
    }
    return CharSet(ret);
}
// the rest of members ...
//
};

full code at godblot

Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 1
    thank you!! the other error is with the strcpy. the visual studio tell me to use srtcpy_s, but it is still not work. – Lorin Sherry Jul 11 '19 at 18:58
  • @LORINECHERRY the remaining error is Visiual Studio warning you that `strcpy` has a number of fabulously deadly and often utterly silent failure cases. `srtcpy_s` does nothing to fix these failure cases, but makes the failures extremely loud (default behaviour is to crash the program). It also has a different set of arguments, [so look it up](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019) and make sure it is the right solution for you. – user4581301 Jul 11 '19 at 19:12
  • 3
    Also strongly consider using `std::string` in place of character arrays as they make most of the string manipulation problems starting programmers have next to impossible. – user4581301 Jul 11 '19 at 19:12
  • Ive got little bit confusing.. can you show me exmaple please? i am new to c++ – Lorin Sherry Jul 11 '19 at 19:47
  • @LORINECHERRY I'll add a string example – Oblivion Jul 11 '19 at 19:50
  • @LORINECHERRY I adapted your class to use string – Oblivion Jul 11 '19 at 20:11
  • Pardon my nitpicking, but `"DAQP"` is `const char [5]`. – HolyBlackCat Jul 11 '19 at 20:30
  • @HolyBlackCat Could you please elaborate. I always thought it is a const char*. https://stackoverflow.com/questions/7903551/when-to-use-const-char-and-when-to-use-const-char – Oblivion Jul 11 '19 at 20:44
  • @Oblivion i am trying to compile your code at VS and it not compiling :( – Lorin Sherry Jul 11 '19 at 20:46
  • @LORINECHERRY did you take it from godbolt? – Oblivion Jul 11 '19 at 20:47
  • yes yes yes yes (sorry the character couting thing i didnt had nothing else to say hehe) – Lorin Sherry Jul 11 '19 at 21:03
  • @Oblivion Since arrays can be implicitly converted to pointers, the difference is subtle. It presents itself only in a few cases. E.g. `const char (&ref)[5] = "DAQP";` wouldn't compile if it was a pointer. Also `sizeof("DAQP")` is `5`, but for a pointer it would be equal to `sizeof(const char *)`. – HolyBlackCat Jul 11 '19 at 21:03
  • yes its working thank you a lot!! there is a way here to talk in private if i have more questions? and it you want to answer of course. – Lorin Sherry Jul 12 '19 at 08:10
  • @Oblivion I forgot that the function and the c'tor needs to apply the thing that each letter can only appear once. so string like 'Hello' cant be accept – Lorin Sherry Jul 12 '19 at 08:19