c++ - Visual Studio,Xutility错误

标签 c++ visual-studio compiler-errors

我正在尝试使用std编写一个简单的程序,但是每当我执行Visual Studio时,都会在Xutility文件中给出一个错误。

该文件是基本的,但是,我想测试一些功能,这是代码:

#include <iostream>

std::string text = "racecar", temp, revText;
bool yes;

void init(){
 //std::cin >> text  ;
 reverseFunction(text);}

void reverseFunction(std::string userWord){
 std::reverse(userWord, revText);}

int main(int argc, char *argv[]){
 init();}

我正在使用VS 2015,但我在新笔记本电脑上安装了2017,但都遇到相同的问题...这些是我得到的错误

C2794 'iterator_category': is not a member of any direct or indirect base class of 'std::iterator_traits<_InIt>' Project1 c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 967 Error
C2938 '_Iter_cat_t' : Failed to specialize alias template Project1 c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 967 Error
C2062 type 'unknown-type' unexpected Project1 c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 967 Error
C2675 unary '--': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator Project1 c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 3547
Error
C2675 unary '++': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator Project1 c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 3547



我有两台机器,运行不同的版本,并且都有相同的问题,并且我无法编译错误,因为它有任何解决方法

最佳答案

此版本的代码是否可以解决?

#include <iostream>
#include <string>
#include <algorithm>

std::string text = "racecar", temp, revText;
bool yes;

void reverseFunction(std::string userWord)
{
    revText = userWord;
    std::reverse(revText.begin(), revText.end());
}

void init()
{
    //std::cin >> text  ;
    reverseFunction(text);
}

int main(int argc, char *argv[])
{
    init();
}

有一些错误:
  • stringalgorithm包括缺少的
  • reverseFunction()是在init()之后定义的,因此编译器无法找到它
  • 根据the docsstd::reversebeginend迭代器作为参数,而不是要反转的字符串以及要将其存储的位置。原因有:

    1-数据转换就地完成,

    2-STL算法旨在实现通用且与容器无关:它们应可用于任何可迭代的集合。为了说服自己,您可以尝试以下代码:
    #include <algorithm>
    #include <iostream>
    
    int main()
    {
        int t[] = { 0, 1, 2, 3, 4, 5 };
        int len = sizeof t / sizeof(int);
    
        std::cout << "Before: ";
        for (int i = 0; i < len; ++i) std::cout << t[i] << " ";
        std::cout << std::endl;
    
        std::reverse(t, t + len);
        std::cout << "After:  ";
        for (int i = 0; i < len; ++i) std::cout << t[i] << " ";
        std::cout << std::endl;
    
        return 0;
    }
    

  • 这应该为您提供以下输出:
    Before: 0 1 2 3 4 5 
    After:  5 4 3 2 1 0 
    

    如您所见,即使我们不使用STL容器,该算法仍然有效,我们仅提供了类似迭代器的输入。

    关于c++ - Visual Studio,Xutility错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43340693/

    相关文章:

    c++ - 有没有办法恢复嵌入在 boost mpl 引用中的原始模板模板类?

    c++ - 如何使用 CMake?

    c# - 不正确的语法 ASP.NET

    Java 编译器错误。找不到标志

    c++ - g++ 中的奇怪编译错误(clang++ 编译正常)

    c++ - OpenCV:如何创建多边形的 mask ?

    c++ - 尝试使用分配器构造 std::list 时失败

    python - 使用 openssl/bn.h 将 c++ 痛饮到 python

    c++ - CEdit不显示特殊字符

    c++ - .cpp 错误 : no match for 'operator<' in 'std::cerr < "Converting file \""'