// cstringclass.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>using namespace std;
class Strings
{
public:
char *p;
int size;
public:
Strings( char *str );
Strings( void );
Strings( const Strings &obj );//copy constructor
~Strings( void ) { delete [] p; }
friend ostream &operator << ( ostream &stream, Strings &obj );
friend ostream &operator >> ( istream &stream, Strings &obj );

Strings operator = ( char *s );
Strings operator + ( Strings &obj );
Strings operator + ( char *s ); friend Strings operator + ( char *s, Strings &obj );

Strings operator - ( Strings &obj );
Strings operator + ( Strings *s ); int operator == ( Strings &obj ) { return !strcmp( p, obj.p ); }
int operator != ( Strings &obj ) { return strcmp( p, obj.p ); }
int operator > ( Strings &obj ) { return strcmp( p, obj.p ) > 0; }
int operator < ( Strings &obj ) { return strcmp( p, obj.p ) < 0; }
int operator >= ( Strings &obj ) { return strcmp( p, obj.p ) >= 0; }
int operator <= ( Strings &obj ) { return strcmp( p, obj.p ) <= 0; } int operator == ( char *s ) { return !strcmp( p, s ); }
int operator != ( char *s ) { return strcmp( p, s ); }
int operator < ( char *s ) { return strcmp( p, s ) < 0; }
int operator > ( char *s ) { return strcmp( p, s ) > 0; }
int operator <= ( char *s ) { return strcmp( p, s ) >= 0; }
int operator >= ( char *s ) { return strcmp( p, s ) <= 0; } int strsize( void ) { return strlen( p ); }
void makestr( char *s ) { strcpy( s, p ); } operator char * ( void ) { return p; }
};
Strings::Strings( void )
{
size = 1;
p = new char[size];
if( !p )
{
cout << "allocation error!" << endl;
exit ( 1 );
}
*p = '\0';
}
Strings::Strings( char *str )
{
size = strlen( str ) + 1;
p = new char[size];
if( !p )
{
cout << "allocation error!" << endl;
exit ( 1 );
}
strcpy( p, str );
}
Strings::Strings( const Strings &obj )
{
size = obj.size;
p = new char[size];
if( !p )
{
cout << "allocation error!" << endl;
exit ( 1 );
}
strcpy( p, obj.p );
}
ostream &operator << ( ostream &stream , Strings &obj )
{
        char* temp;
temp =obj.p;

stream << temp;

return stream;
}
/*----------------------------------------------*/
int main()
{
Strings temp01 ("allocation via a quoted string" );
Strings temp02 = temp01;//= "allocation via a string object" ;
Strings temp03 ;
    cout << temp01 ;  return 0;
}
报错如下
cstringclass.cpp(100) : error C2593: 'operator <<' is ambiguous

解决方案 »

  1.   

    这个问题很不容易发现。原因是由于cout对char*和Strings都有一份定义,它就不知所措了。你可以这样解决它:
    cout<<temp01.operator char *();
    或者干脆把operator<<相关定义删去,这样就行了。但在你这个设计中有一些问题,如operator char *返回一个内部的handle,看看下面的代码:
    Strings temp("Hello, world!");
    char * p = temp;
    strcpy(p, "hello, mom");
    cout<<temp.operator char *();
    会output什么呢?是对全世界的问候,还是仅仅对你的母亲?答案是"hello,mom"!
    这可不是你希望的吧。万一对母亲的问候长了一点,系统就会......
    把:
    operator char * ( void ) { return p; }
    改成这样:
    operator const char * (void) { return static_cast<const char*>(p) );
    就勉强可行了。
    关于memory allocation还有很多问题,你可以看《Effective C++》。或者直接给我来信,我们一起探讨。
    [email protected]
    最后,别忘记给我加分啊。:)
      

  2.   

    多谢二位的回答,我的解決办法是include如下
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <stdio.h>

    ostream &operator << ( ostream &stream , Strings &obj )
    {    stream << obj.p;
    return stream;
    }
    我想主要是stdlib.h与stdio.h包括的reent.h
    解決了namespace问题