3

我不是很擅长编程,也没有经验,这对于老师帮助微不足道的班级来说是微不足道的。

标题是我得到的错误。这是功能:

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.
   }
 }

这是错误的扩展版本:

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 和 string 都包括在内。我直接从我的书中复制了语法,一切都应该井井有条,但错误​​仍然存​​在。

协助将不胜感激。

4

1 回答 1

3

首先,您Names在代码中使用参数的方式表明它应该是一个数组。但是您将其声明为单个对象。因此出现错误。

该参数显然应该被声明为

void CapFormat (string Names[], int NameCount)

或者

void CapFormat (string *Names, int NameCount)

(这是同一件事。)将此与您的书进行比较。要么是你的错误,要么是书中的错误。

其次,这size条线应该看起来像

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

(注意额外的())。而且绝对没有必要static_castunsigned那里。做就是了

len = Names[c].size();

这是static_cast你的主意吗?如果它在书中,那将是一本相当奇怪的书。

int不适当的变量和适当的变量的不合逻辑的混合string::size_type使我怀疑这实际上是从一本书中摘录的。

于 2012-08-11T04:02:54.507 回答