简体   繁体   English

为什么在尝试输出字符串时出现此错误?

[英]Why do I get this error when trying to output a string?

I get the error when using cout in main function at end 最后在主函数中使用cout时出现错误

   #include "string"
#include "stdafx.h"
#include"iostream"

using namespace std;

string first[] = { "","one","two","three","four","five","six","seven","eight","nine","ten","eleven",
"twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };

string second[] = { "","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety" };

string lessThan100(int b)
{

    if (b < 20) { return first[b]; }//accounts for numbers that don't follow pattern ie 11 to 19

    return second[b / 10] + first[b % 10]; // dividing by 10 front number modding by 10 gives back number
}

string intToString(int a)
{
    string output = "";
    for (int x = log10(a); x >= 0; x = x - 3)
    {
        if (x >= 9)
        {
            int num = a / 1000000000;//dividing by a billion gives the # of billions
            if (num<99) { return "error number too large"; }

            output = output + lessThan100(num) + " billion ";

        }
        else if (x >= 6)
        {
            int num = a % 1000000000 / 1000000; //modding by a billion leaves the millions dividing by million gives # of millions
            output = output + lessThan100(num) + " million ";

        }
        else if (x >= 3)
        {
            string over100 = "";
            int num = a % 1000000 / 1000;//modding by a million leaves the thousands dividing by a thousand gives # of thousands
            if (num >= 100) { over100 = first[num / 100] + " hundred "; }//we can have more than 99 thousand so we account for that
            output = output + over100 + lessThan100(num) + " thousand ";

        }
        else if (x >= 0)
        {
            string over100 = "";
            int num = a % 1000;//moding by 1000 gives hundreds
            if (num >= 100) { over100 = first[num / 100] + " hundred "; }//accounts for number higher than 99
            output = output + over100 + lessThan100(num);
        }
    }
}

int main()
{
    int number = 19;
    string word = intToString(19);
    cout << word;
    return 0;
}

I get this error binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion). 我收到此错误二进制文件“ <<”:没有找到采用类型为“ std :: string”的右侧操作数的运算符(或者没有可接受的转换)。 The program runs fine when I remove cout << word; 当我删除cout << word时,程序运行正常; I really just want to see if intToString() is working. 我真的只是想看看intToString()是否在工作。

The headers you need are: 您需要的标题是:

#include <string>
#include <iostream>

and they must come after the include of stdafx.h . 并且它们必须位于stdafx.h的包含之后 The MS compiler ignores any lines that are before stdafx.h . MS编译器将忽略stdafx.h之前的任何行。 The reasons why can be found in this answer (to a related question) but they basically boil down to the way MSVC does precompiled headres. 为什么中可以找到原因这个答案 (到一个相关的问题),但他们基本上可以归结为MSVC不预编译headres的方式。

well, you should know that when you use " ", it tells the compiler to search for the head-file in local directory, while if you use <>, it tells the compiler to find head file in system directory. 好吧,您应该知道,当使用“”时,它告诉编译器在本地目录中搜索头文件,而如果使用<>,则告诉编译器在系统目录中查找头文件。 so, I suggest you to try use <> to #inlcude 因此,我建议您尝试使用<>来#inlcude

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将字符串作为文件名而不是char *传递时出现错误? - Why do I get an error when passing a string as a filename, but not a char*? 为什么在尝试访问字符串中的字符时出现段错误 - Why do I get a seg fault when trying to access a character in a string 尝试使用C ++创建二维数组时,为什么会出现此错误? - Why Do I get this error when trying to create a bidimensional array with c++? 为什么在尝试编译示例代码时出现错误“找不到-lcurl”? - Why do I get the error “cannot find -lcurl” when trying to compile example code? 为什么在尝试将我的代码上传到我的 Arduino 时会收到验证错误? - Why do I get a verification error when trying to upload my code to my Arduino? 为什么在尝试制作动态向量数组时会出现此错误 - Why do I get this error when trying to make a dynamic vector array 尝试从模拟返回值时,为什么会出现编译时错误? - Why do I get a compile-time error when trying to return values from a mock? 为什么在 c++ 中尝试实现多线程时出现“Segmentation fault (core dumped)”错误? - Why do I get "Segmentation fault (core dumped)" error when trying to implement multithreading in c++? 输入输入时为什么会出现错误? “表达式:字符串下标超出范围” - Why do I get an error when I enter input? “expression: string subscript out of range” 当我打开文件名在 std::string 中的 fstream 时,为什么会出现“无匹配函数”错误? - Why do I get a "no matching function" error when I open an fstream with the file name in a std::string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM