0

I have void func below:

void ToUppercase(std::string & data)
{
    transform(data.begin(), data.end(), data.begin(), (int(*)(int))toupper);
}

if i use if statement like this:

if (ToUppercase(str).find("AAA") != string::npos) // Do something;

It is showing me error "expression must be a class type". Why cant i use void func directly in if statement. Somehow, it is not feasible for me to return value from ToUppercase func.

RookieDev
  • 43
  • 6

3 Answers3

1

ToUppercase(str) is, as you've declared, of typevoid. You cannot call member access operator (.) on void.

If you want to chain calls like this you could also return the passed reference

std::string & ToUppercase(std::string & data)
{
    transform(data.begin(), data.end(), data.begin(), (int(*)(int))toupper);
    return data;
}

If you insist on returning void for some reason, your only valid option of doing what you're doing is:

ToUUppercase(str);
if(str.find("AAA") != std::string::npos)
{
}
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • **Improvement**: the generic `std::find` will probably be faster than `std::string::find` (see the comment section of https://www.bfilipek.com/2018/07/string-view-perf.html) – Matthias Sep 17 '18 at 15:11
1

find() is a member method for the std::string class, but you are returning void instead. Reform your code to return a std::string so that you can call find() on the return value.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Muhammad Aly
  • 87
  • 1
  • 8
0

If your ToUppercase really cannot return the new string, you might try the following:

ToUppercase(str);
if (str.find("AAA") != string::npos)
{
  ..
}
Robert Kock
  • 5,795
  • 1
  • 12
  • 20