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.
CodePudding user response:
This constructor passes pointer to a function which is not a thing you intend to pass:
... smolifier(StatP::oltosmol)
To do the thing you intended you require instantiate your class and than pass it to the CharProcessor
:
StatP::oltosmol smol{};
... smolifier(smol);
But in your case better solution to remove reference from constructor and pass it by value and move it inside CharProcessor
completely;
CharProcessor(Transformer t): transformer(std::move(t)) {}
...
// And now you can just put braces
CharProcessor<oltsmol> smolifer({});
CodePudding user response:
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)