Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the default constructor isn't called?

I want to pass class name oltosmol as an argument so it will create an object with default constructor. It would normally work but not here. And the problem is only with oltosmol.

error: request for member 'creator' in 'smolifier', which is of non-class type 'StatP::CharProcessor\<StatP::oltosmol\>(StatP::oltosmol)'
namespace StatP
{
    class oltosmol
    {
    public:
        oltosmol() {}
        char transform(char c)
        {
            if (c >= 'A' && c <= 'Z')
                return c + 'a' - 'A';
            return c;
        }
    };
    class Cutifier
    {
        unsigned period;
        unsigned counter;
    public:
        Cutifier(unsigned p): period(p), counter(0) {}
        char transform(char c)
        {
            if (counter == period)
            {
                counter = 0;

                if (c == ' ')
                    return '\3';
                else if (c >= 'a' && c <= 'z')
                    return c - 'a' + 'A';
                else if (c >= 'A' && c <= 'Z')
                    return c - 'A' + 'a';
                return c;
            }
            else
            {
                counter++;
                return c;
            }
        }
    };

    template<class Transformer>
    class CharProcessor
    {
        Transformer transformer;
    public:
        CharProcessor(const Transformer &t): transformer(t) {}

        String<char> creator(String<char> &s)
        {
            char *n_data = new char[s.get_length() + 1];
            for (unsigned i = 0; i < s.get_length(); i++)
                n_data[i] = transformer.transform(s[i]);
            n_data[s.get_length()] = '\0';

            return String<char>(n_data);
        }
        void changer(String<char> &s)
        {
            for (unsigned i = 0; i < s.get_length(); i++)
                s[i] = transformer.transform(s[i]);
        }
    };
}

int main()
{
    const char coca_cola[] = "Coca-cola";

    String<char> water_name = coca_cola;
    copy = water_name;

//    ...


    StatP::CharProcessor<StatP::oltosmol> smolifier(StatP::oltosmol);
    std::cout << smolifier.creator(water_name) << "\n\n";

    return 0;
}

I’ve tried to add () and remove namespace, but it’s still doesn’t work. The program would run, if I firstly assign object to a variable, but I don’t want and shouldn’t do that.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your instantiation of smolifier you have to pass an instantiated object. Writing a type in the argument list is invalid syntax.

This is what you need there:

StatP::CharProcessor smolifier(StatP::oltosmol{});

Observe that due to CTAD, you don’t actually have to spell out the type argument to the class template because it can be inferred from the constructor argument.

(live demo, with some types replaced because you didn’t provide them)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading