Solved

STL string, iostream, and cout of class info

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Smart Camera scanning and reading information 3 126
NotAlone Challenge 20 91
unix example issues 18 112
Authenticate using sesu from script 7 152
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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 shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

740 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