我不擅长编程,也没有经验,这适用于教师辅助程度微不足道的课程。
标题是我得到的错误。这是功能:
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。我直接从我的书中复制了语法,一切都应该按顺序排列,但错误仍然存在。
非常感谢协助。
答案 0 :(得分:3)
首先,在代码中使用Names
参数的方式表明它应该是一个数组。但是你将它声明为单个对象。因此错误。
该参数显然应该声明为
void CapFormat (string Names[], int NameCount)
或
void CapFormat (string *Names, int NameCount)
(这是相同的。)将此与您的书进行比较。这是你的错误或书中的错误。
其次,size
行应该看起来像
len = static_cast<unsigned>(Names[c].size());
(注意额外的()
)。那里static_cast
至unsigned
完全没有必要。只是做
len = Names[c].size();
这是static_cast
你的想法吗?如果它在书中,那将是一本相当奇怪的书。
不合适的int
变量与适当的string::size_type
变量的不合逻辑混合使我怀疑这实际上来自于一本书。