Solved

STL string, iostream, and cout of class info

Posted on 2002-03-26
2
1,718 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
Comment Utility
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
Comment Utility
duhhhh ...
thx
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

6 Experts available now in Live!

Get 1:1 Help Now