Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: request for member size in ... which is of non-class type char

Tags:

c++

I'm not very good at programming, nor experienced, and this is for a class where the teacher is of negligible aid.

The title is the error I'm getting. This is the function:

void CapFormat (string Names, int NameCount)
 {
   int Comma;
   int c;
   int d;

    string::size_type len;

   for(c = 0; c < NameCount; c ++)
   {
     len = static_cast<unsigned> (Names[c].size);  //error starts here
     for(d = len; d > 0; d --)
     {
       tolower((Names[c].at(d)));  //supposed to lower cases every letter
     }
     touppper(Names[c].at(0));  //supposed to upper case first letter
     Comma = Names[c].find(",");  //supposed to find a comma between last and first names
     Comma = Comma + 1;
     toupper(Names[c].at(Comma));  //Error here as well.  supposed to then upper case letter after the comma.
   }
 }

Here is the expanded version of the errors:

In function âvoid CapFormat(std::string, int)â:
error: request for member âsizeâ in âNames.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((long unsigned int)c))â, which is of non-class type âcharâ
error: request for member âatâ in âNames.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((long unsigned int)c))â, which is of non-class type âcharâ
error: request for member âatâ in âNames.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = error: âtouppperâ was not declared in this scope
error: request for member âfindâ in âNames.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((long unsigned int)c))â, which is of non-class type âcharâ
error: request for member âatâ in âNames.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((long unsigned int)c))â, which is of non-class type âcharâ

Cstring, cctype, and string are all included. I've copied the syntax directly from my book, everything should be in order, but the errors remain.

Assistance would be greatly appreciated.

like image 463
Lawrence Benecke Avatar asked Dec 13 '25 16:12

Lawrence Benecke


1 Answers

Firstly, the way you use the Names parameter in your code suggest that it is supposed to be an array. But you declared it as a single object. Hence the errors.

The parameter was apparently supposed to be declared as either

void CapFormat (string Names[], int NameCount)

or

void CapFormat (string *Names, int NameCount)

(which is the same thing.) Compare this to your book. It is either your mistake or an error in the book.

Secondly, the size line is supposed to look as

len = static_cast<unsigned>(Names[c].size());

(note the extra ()). And there's absolutely no need for that static_cast to unsigned there. Just do

len = Names[c].size();

Was this static_cast your idea? If it is in the book, that would be a rather strange book.

The illogical mix of inappropriate int variables and proper string::size_type variables makes me doubt that this was actually taken from a book.

like image 126
AnT Avatar answered Dec 16 '25 10:12

AnT