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

时间:2015-11-16 03:48:16

标签: c++

在结尾的主要功能中使用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;
}

我得到这个错误二进制'&lt;&lt;':没有找到运算符,它采用'std :: string'类型的右手操作数(或者没有可接受的转换)。当我删除cout&lt;&lt;&lt;&lt;&lt;&lt;&lt;字;我真的只想看看intToString()是否有效。

2 个答案:

答案 0 :(得分:1)

您需要的标题是:

#include <string>
#include <iostream>

他们必须在包含stdafx.h之后来。 MS编译器忽略stdafx.h之前的任何行。 为什么的原因可以在this answer(相关问题)中找到,但它们基本上归结为MSVC预编译headres的方式。

答案 1 :(得分:0)

嗯,你应该知道当你使用&#34; &#34;,它告诉编译器在本地目录中搜索头文件,而如果使用&lt;&gt;,它告诉编译器在系统目录中查找头文件。 所以,我建议你尝试使用&lt;&gt;到#inlcude