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

编译器好像是吧“<<”这个符号认错了

丶浅唱 发布于 2014-08-22 20:05, 696 次点击
以下是原码:
#include<iostream>
#include<string.h>
using namespace std;
class CBuliding
{
    string name;
public:
    void set(string strName);
    void display();
};
void CBuliding::display()
{
    cout<<"建筑是"<<name<<endl;//  就是这里,书上说这里是内联函数。
}
void CBuliding::set(string strName)
{
    name=strName;
}
class CBridge:public CBuliding
{
    float length;
public:
    void setLength(float l)
    {
        length=l;
    }
    void display()
    {
        CBuliding::display();
        cout<<"其长度是"<<length<<"米"<<endl;
    }
};
void main()
{
    CBuliding buliding;
    CBridge bridge;
    buliding.set("中国古建筑");
    buliding.display ();
    bridge.set("中国赵州桥");
    bridge.setLength(static_cast<float>(60.40));
    bridge.display();
    return;
}


【cout<<"建筑是"<<name<<endl;】
这里如果是把name去掉的话就能编译通过。如果加进去就会提示:
binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable co
nversion)
实在是想不通是哪里错了:
7 回复
#2
apull2014-08-22 22:50
#include<string>
#3
stop12042014-08-23 07:32
回复 2 楼 apull
有些编译器同时支持 c/c++编译  所以这个没错
#4
stop12042014-08-23 07:35
void main()  请不要再返回了  return .  这里void的意思就是没返回值. 但你是 又 return.
而且  return 要加返回值.    return 1;  return a;之类的   不能直接return;
通常是return 0;
#5
天使梦魔2014-08-25 09:56
不知道你是用的是什么编译器。
假设编译器不能按某种约定识别名字空间就加
cout<<"建筑是"<<this->name<<endl;
试试

name可能被识别成外部变量。
#6
码珠砂华2014-08-27 10:03
name是私有成员  怎么能在公有函数里调用呢
#7
l34562014-08-27 10:42
#include<string.h>//c++为什么还要加.h????而且下面有 using namespace std了!!!去掉.h就可以了
 using namespace std;
#8
fl89622014-09-11 12:36
回复 6 楼 码珠砂华
Name 是私有成员怎么能在公有函数里调用呢?咦
1