none
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class cString' (or there is no acceptable conversion)

    Question

  • Please anybody can address me where my code is wrong or how I can resolve this compiler error.  I would like to thank to whoever will into my thread. Please excuse me for my poor English.

     I am new to C++ and I want to learn C++ so I am trying to understand constructor functionality so I am wrote a small class and I want to check what would be happen if i dint defined copy constructor or assignment operator but my class declaration seems to ok compiler pint of view. I know that this class has logical error.
        While compile time I have got the following error on VC++ 6.0 environment.


    Here is my code:

    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>

    #include <string>

    using namespace std;

    class cString{
    public:
     cString(const char *value);
     ~cString();
    private:
     char *data;
    };

    cString::cString(const char *value)
    {
      if(*value)
      {
        data = new char [strlen(value)+1];
     strcpy(data, value);
      }
      else
      {
       data = new char [1];
       *data = '\0';
      }
    }
    inline cString::~cString(){}

    int main(int argc, char* argv[])
    {
        cString a("Hello");
     cString b("World");
    // a=b;
      cout<<"String a="<<a<<endl;
       getch();
       return 0;
    }

    Thursday, April 28, 2011 10:05 AM

Answers

  • << is an overloaded operator that that can understand many different data types.

    But it doesn't understand a parameter of type cString which is a class written by you.

    In such situations you can overload a cast operator and use that cast in the line with cout.

     

    In your class, create an operator like this -

     

     operator char*()
     {
    	 return data;
     }
    

    Now you can use cout like this -

    cout<<"String a="<< (char*)a << endl;
    
    



    «_Superman_»
    Microsoft MVP (Visual C++)

    Polymorphism in C
    • Marked as answer by DattaB Friday, April 29, 2011 6:15 AM
    Thursday, April 28, 2011 10:21 AM

All replies

  • Please anybody can address me where my code is wrong or how I can resolve this compiler error.  I would like to thank to whoever will into my thread. Please excuse me for my poor English.

     I am new to C++ and I want to learn C++ so I am trying to understand constructor functionality so I am wrote a small class and I want to check what would be happen if i dint defined copy constructor or assignment operator but my class declaration seems to ok compiler pint of view. I know that this class has logical error.
        While compile time I have got the following error on VC++ 6.0 environment.


    Here is my code:

    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>

    #include <string>

    using namespace std;

    class cString{
    public:
     cString(const char *value);
     ~cString();
    private:
     char *data;
    };

    cString::cString(const char *value)
    {
      if(*value)
      {
        data = new char [strlen(value)+1];
     strcpy(data, value);
      }
      else
      {
       data = new char [1];
       *data = '\0';
      }
    }
    inline cString::~cString(){}

    int main(int argc, char* argv[])
    {
        cString a("Hello");
     cString b("World");
    // a=b;
      cout<<"String a="<<a<<endl;
       getch();
       return 0;
    }

    • Merged by Calvin_Gao Monday, May 02, 2011 2:23 AM Merge them to keep in the same topic
    Thursday, April 28, 2011 10:01 AM
  • << is an overloaded operator that that can understand many different data types.

    But it doesn't understand a parameter of type cString which is a class written by you.

    In such situations you can overload a cast operator and use that cast in the line with cout.

     

    In your class, create an operator like this -

     

     operator char*()
     {
    	 return data;
     }
    

    Now you can use cout like this -

    cout<<"String a="<< (char*)a << endl;
    
    



    «_Superman_»
    Microsoft MVP (Visual C++)

    Polymorphism in C
    • Marked as answer by DattaB Friday, April 29, 2011 6:15 AM
    Thursday, April 28, 2011 10:21 AM
  • #include <iostream>
    #include <conio.h>
    #include <string>
    using namespace std;

    class cString
    {
     friend ostream& operator<<(ostream& os, const cString& str);
    public:
     cString(const char *value);
     ~cString();
    private:
     char *data;
    };

    cString::cString(const char *value)
    {
     if(*value)
     {
      data = new char [strlen(value)+1];
      strcpy(data, value);
     }
     else
     {
      data = new char [1];
      *data = '\0';
     }
    }
    inline cString::~cString(){}

    ostream& operator<<(ostream& os, const cString& str)
    {
     os<<str.data;
     return os;
    }

    int main(int argc, char* argv[])
    {
     cString a("Hello");
     cString b("World");
     // a=b;
     cout<<"String a="<<a;
        getch();
        return 0;
    }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Thursday, April 28, 2011 11:18 AM
  • You need to post your question to a Visual C++ forum (this is a Visual Basic .NET forum), or perhaps a moderator can move this question there:

    http://social.msdn.microsoft.com/Forums/en/vcgeneral/threads

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, April 28, 2011 12:17 PM
  • This is a VB.NET Forum. Please repost at here or have a moderator move it to the relevant forum.

    Samuel


    Sorry if my English is poor, it's not my first language. :) Programming FTW! Samuel
    Thursday, April 28, 2011 12:17 PM
  • Thank you. Your solution is wokring.

    Thursday, April 28, 2011 12:29 PM
  • Thank you Visual Eleven for answered to my question.  I have gave tried to run your given solution code, and old my error has resolved, however I got another error as         error C2248: 'data' : cannot access private member declared in class 'cString'
    : see declaration of 'data'
     error C2593: 'operator <<' is ambiguous

    For same question's answared by _Superman_  and his solution is working.

    Thank you.

    Thursday, April 28, 2011 12:39 PM