Solved

STL string, iostream, and cout of class info

Posted on 2002-03-26
2
1,727 Views
Last Modified: 2013-11-20
I'm an STL newbie, and I'm tring to write a simple program to cout a class that has an std::string in it

Here's a program that works but that doesn't use stl:

// *** compile with cl -GX test00.cpp
#include <iostream.h>
#include <string.h>

class CInfo
{
public:
  CInfo(int key, char* ref);

  friend ostream& operator<<(ostream& s, const CInfo &info);

private:
  int   m_key;
  char  m_ref[20];
};

CInfo::CInfo(int key, char* ref) : m_key(key) { strcpy(m_ref, ref); }

ostream& operator<<(ostream& s, const CInfo &info)
{
  return s << '(' << info.m_ref << ')';
}

int main(void)
{
  cout << "Reached main" << endl;
  CInfo myInfo(123456, "myRef");
  cout << myInfo << endl;
  return 0;
}


Here's the (roughly equivalent) program with std::string.  There's some strangeness with std::ostream, and I get the compiler error for the cout << myInfo << endl;

error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class ostream_withassign' (or there is no acceptable conversion)

// *** compile with cl -GX test01.cpp
#include <iostream.h>
#include <string.h>

class CInfo
{
public:
  CInfo(int key, char* ref);

  friend ostream& operator<<(ostream& s, const CInfo &info);

private:
  int   m_key;
  char  m_ref[20];
};

CInfo::CInfo(int key, char* ref) : m_key(key) { strcpy(m_ref, ref); }

ostream& operator<<(ostream& s, const CInfo &info)
{
  return s << '(' << info.m_ref << ')';
}

int main(void)
{
  cout << "Reached main" << endl;
  CInfo myInfo(123456, "myRef");
  cout << myInfo << endl;   // <-- causes compiler error
  return 0;
}

What am I doing incorrectly?  And is the std::outstream necessary?  

TIA
0
Comment
Question by:ee-user
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6897278
Make your code read

// *** compile with cl -GX test01.cpp
#include <iostream>
#include <string>

using namespace std;

// etc...

You were using deprecated STL headers (the correct ones don't have the .h extension), and you haven't selected the 'std' namespace...
0
 

Author Comment

by:ee-user
ID: 6897727
duhhhh ...
thx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question