Solved

STL string, iostream, and cout of class info

Posted on 2002-03-26
2
1,720 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 49
repeatSeparator  java  challenge 13 56
notReplace  challenge 53 116
twoTwo  challenge 35 96
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now