当我显然在上面声明它时,我在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;
上的错误,因为这是首次检测到问题的时间。 这并不意味着您应该将分号放在那里。
一些编译器会提示您可能缺少类声明中的分号,而其他编译器则不会。 这个错误很常见。 如果您在荒谬的地方看到缺少的分号错误,您应该立即考虑您可能不小心在标题中遗漏了一个分号错误。