C++ 反向字符串无循环

C++ 反向字符串无循环,c++,string,reverse,C++,String,Reverse,好的,这就是我得到的,但我得到一个错误,上面写着“complexReverseString”:找不到标识符???我哪里做错了?我仔细检查了一下,没有用 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream input; string forward; cout << "Plea

好的,这就是我得到的,但我得到一个错误,上面写着“complexReverseString”:找不到标识符???我哪里做错了?我仔细检查了一下,没有用

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream input;
    string forward;

    cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
    input.open("information.txt");
    cin >> forward;
    //reverseString(forward, findStringSize(forward)); a function I'm not actually calling
    input >> forward;
    complexReverseString();

    input.close();


}
int complexReverseString(string userInput)
{
    string source(userInput);
    string target( source.rbegin(), source.rend() );
    cout << "The reversed string is " << target << endl;

    return 0;
}
在调用complexReverseString之前,必须先对其进行原型化

在主入口点之前的源文件中执行此操作,如下所示:

int complexReverseString(string userInput);
int main() { ... }
或者在单独的标题中执行此操作:

#ifndef COMPLEX_STR_HPP
#define COMPLEX_STR_HPP

int complexReverseString(string userInput);

#endif /* COMPLEX_STR_HPP */

/* int main.cpp */
#include "complex_str.hpp"

int main() { ... }
其次,您忘记将参数传递给函数:

 complexReverseString( /* parameter here */ );

 e.g
 complexReverseString(forward);
第三,由于您没有在complexReverseString中更改字符串,因此我建议使用常量字符串,或者引用字符串:

int complexReverseString(const string& userInput);

您需要将参数传递给方法吗?由于方法需要字符串类型的参数,否则需要重载它。。它目前正在失败,因为它正在寻找函数定义complexReverseString

将代码更改为

complexReverseString(forward);

<>在没有C++中显式循环的情况下,简单的反转字符串或任何其他容器的方法是使用反向迭代器:

string input;
cin >> input;
// Here is the "magic" line:
// the pair of iterators rbegin/rend represent a reversed string
string output(input.rbegin(), input.rend());
cout << output << endl;

在C/C++中,编译器从上到下读取源代码。如果在编译器读取某个方法之前使用该方法,例如,如果在底部定义了该方法,并且在main中使用该方法,并且main位于顶部,编译器将不会运行项目/文件夹中的所有源/头文件,直到找到该方法或已遍历所有文件

您需要做的是对您的方法进行原型化,或者在顶部定义它

函数原型基本上是一个方法头,用于通知编译器该方法将在源代码中的其他地方找到

原型:

#include <iostream>    
#include <fstream>    
#include <string>

using namespace std;

// DEFINE PROTOTYPES BEFORE IT'S CALLED (before main())
int complexReverseString(string);

int main()    
{    
    // your main code
}    

int complexReverseString(string userInput)    
{    
    // your reverse string code
}
这两种样式中的任何一种都会产生相同的结果,因此由您决定使用哪种样式


除此之外,当您在main中调用complexReverseString时,您忘记了传递它的参数,这些参数应该是向前的。因此,它不是complexReverseString,而是complexReverseStringforward

请用它所涉及的语言标记您的问题。如果您看不到循环,并不意味着没有循环。@KcNorth:理解编译错误是学习任何编译语言的重要部分。看看你今天发布的其他问题,现在绝对是你阅读的时候了。StackOverflow社区无法教您语言或编程,这不是本网站的工作方式,抱歉!仅供参考,@Baz1nga供参考,原型设计是一种良好的实践。并且应该在现代代码中始终使用。再次很高兴知道。。但是这里的问题是另外一个问题,你已经更新了你的答案以解决主要问题。在这个类中还没有学会如何使用标题:使用complexReverseStringforward;所以它要求用户输入,而不是有一个constsweet我不知道这篇文章写得这么低谢谢你所有的快速回复这是最好的景象我感谢所有的帮助
#include <iostream>    
#include <fstream>    
#include <string>

using namespace std;

// DEFINE METHOD BEFORE IT'S CALLED (before main())
int complexReverseString(string userInput)    
{    
    // your reverse string code
}

int main()    
{    
    // your main code
}