重做字符是未声明的,但它被声明了
Redo char is undeclared yet it is declared
当我显然在上面声明它时,我在main上遇到重做字符(粗体)的错误.cpp。我还想知道为什么它要求我在使用命名空间 std 前面放一个分号,因为我以前从未这样做过。
//ReverseString.h
#include <iostream>
#include <string>
using namespace std;
class StringClass
{
public:
string string;
int GetStringLength (char*);
void Reverse(char*);
void OutputString(char*);
void UserInputString (char*);
StringClass();
private:
int Length;
}
//StringClass.cpp
#include <iostream>
#include <string>
#include "ReverseString.h"
;using namespace std;
void StringClass::UserInputString(char *string)
{
cout << "Input a string you would like to be reversed.n";
cin >> string;
cout << "The string you entered: " << string << endl;
}
int StringClass::GetStringLength (char *string)
{
Length = strlen(string);
return Length;
}
void StringClass::Reverse(char *string)
{
int c;
char *front, *rear, temp;
front = string;
rear = string;
GetStringLength(string);
for ( c = 0 ; c < ( Length - 1 ) ; c++ )
rear++;
for ( c = 0 ; c < Length/2 ; c++ )
{
temp = *rear;
*rear = *front;
*front = temp;
front++;
rear--;
}
}
void StringClass::OutputString(char *string)
{
cout << "Your string reversed is: " << string << ".";
}
//Main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "ReverseString.h"
;using namespace std;
const int MaxSize = 100;
int main()
{
do
{
char string[MaxSize];
**char redo;**
StringClass str;
str.UserInputString(string);
str.Reverse(string);
str.OutputString(string);
//Asks user if they want redo the program
cout << "Would you like to redo the program?n";
cout << "Please enter Y or N: n";
**cin >> redo;**
}while(redo == 'Y' || redo == 'y');
}
它为什么声明它但给出一个错误,它没有声明,这真的很令人困惑。
redo
在循环中声明为局部变量。它的范围从声明点开始,到关键字之前的右大括号结束while
。redo
的名称在while
条件下是未知的。
您在 ReverseString.h
中的类声明后缺少分号。
编译器正在拾取行using namespace std;
上的错误,因为这是首次检测到问题的时间。 这并不意味着您应该将分号放在那里。
一些编译器会提示您可能缺少类声明中的分号,而其他编译器则不会。 这个错误很常见。 如果您在荒谬的地方看到缺少的分号错误,您应该立即考虑您可能不小心在标题中遗漏了一个分号错误。
相关文章:
- 按顺序声明的字符数组重叠
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 字符类型声明和验证
- C++,字符串声明中的其他字符
- Cpp 什么是字符 a 的声明 [10][100]
- 带有字符的字符串的 cpp 声明
- 声明多字符字符的警告和错误
- C++ - "(常量字符*)"未在范围内声明?
- 使用无符号字符和字符串作为函数声明
- 如何在标头中声明后初始化 2D 字符数组
- 声明一个包含 N 个指向函数的指针的数组,该函数返回指向字符的指针
- 声明并分配给 2D 字符指针
- 在堆栈 C++ 上声明固定大小的字符数组
- 声明一个接受预处理器宏的字符
- 主 () 内外的字符指针声明
- 重做字符是未声明的,但它被声明了
- 如何在声明后设置常量字符*
- 在c++ 11中声明字符十六进制常量
- 在类中声明字符数组的正确方法是什么?