还有一个比较宽泛的问题,在vc里写纯c++程序要注意些什么?

解决方案 »

  1.   

    下面的代码编译通过!#include <iostream.h>
    class hstring
    {
    public:
    hstring():p(NULL){};friend ostream &operator<<(ostream &stream,hstring &o);
    private:
        char *p;
        int size;
    };
    ostream &operator<<(ostream &stream,hstring &o)
    {
        stream<<o.p;
        return stream;
    }class A : public ostream
    {
    public:
    A():ostream(){};
    };int main()
    {
    A s;
    hstring a;
    s<<a;
    return 0;
    }
      

  2.   

    看不大懂,hstring是什么啊,o.p??p是o的私有成员吧?
      

  3.   

    hstring是我随便起的一个类名字,p的确是o的私有成员,但是友员函数是可以访问私有成员的.
      

  4.   

    于嘉兄,您的程序我怎么编译没出问题啊?(Borland C++ Builder)
      

  5.   

    为什么编译器会报不能访问私有变量?那么我只写一个hstring类的cpp文件的话肯定是编译不通过了是不是?
      

  6.   

    不会吧,我又编译了一遍,的确报错啊!
    d:\vc\test\test.cpp(16) : error C2248: 'p' : cannot access private member declared in class 'hstring'
        d:\vc\test\test.cpp(11): see declaration of 'p'
      

  7.   

    千真万确没有错,Turbo C++ 3.0中也编译通过.
      

  8.   

    不去省略号编译能通过么?不过VC我没试,我只用VC编辑资源,编译还有点手生.
      

  9.   

    VC我试过了,huoxingang的代码,除了unsing namespace std;其他代码是原版拷贝!当然去了省略号^_^
      

  10.   

    开玩笑吧? 去了using namespace std报了15个错啊!
      

  11.   

    按理说应该没错吧?反正Borland的C++编译器从低到高都没问题.
      

  12.   

    问题已经很明显了,就是iostream库的问题,可是这是为什么呢?
    其实就是我问的第二个问题,在vc下编写标准c++函数,库函数需要什么样的定义呢?