注册 登录
编程论坛 C++教室

string在cout的时候出了转换的问题

nubore 发布于 2011-02-06 09:35, 856 次点击
程序代码:
#include <iostream>
#include <cstring>

using namespace std;

...
...
...


void TestType()
    {
        string Type;
        if ( TypeOfTaxer == 's' || TypeOfTaxer == 'S' )
            Type = "Single";
        else if ( TypeOfTaxer == 'm' || TypeOfTaxer == 'M' )
            Type = "Married";
        else if ( TypeOfTaxer == 'w' || TypeOfTaxer == 'W' )
            Type = "Widowed";
        else if ( TypeOfTaxer == 'd' || TypeOfTaxer == 'D' )
            Type = "Divorced";
        else if ( TypeOfTaxer == 'h' || TypeOfTaxer == 'H' )
            Type = "Head of Family";
        else
            Type = "None";

        cout << "\n\n\tFor This Year, You Declared Yourself as : "  << Type << " !\n";
    }

在 << Type 的时候出错, 我的string用法写错了?

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
4 回复
#2
leeco2011-02-08 01:17
你没包含 string的头文件
#3
pangding2011-02-08 09:28
嗯,应该就是 2楼 说的那问题。
#4
lucky5635912011-02-09 09:25
错误信息是什么呢?
#5
gsdgold2011-02-28 23:15
#include <cstring>改成下面这个看看
#include<string>
我也是新手 但愿能帮到你
1