?
Solved

Beginner: Error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::

Posted on 2003-03-06
9
Medium Priority
?
2,087 Views
Last Modified: 2013-12-14
Everytime i try to run the code below, i keep on getting this error message:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator
<char> >' (or there is no acceptable conversion)
whats wrong with the code? i cant seem to figure it out.

heres the code:
---------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <ctime>

using std::cout;
using std::endl;
using std::setw;
using std::string;

const int NUM_GRADES = 10;
const int NUM_STUDENTS = 3;

int findHighest( int * );
int findLowest( int * );
void printDatabase( const int [][NUM_GRADES], const string [NUM_STUDENTS] );

int main()
{
   int student1[ NUM_GRADES ] = { 0 };
   int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104, 91, 71, 85};
   int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72, 76, 79, 99 };
   int database[ NUM_STUDENTS ][ NUM_GRADES ];
   string names[ NUM_STUDENTS ] = {"Bob", "John", "Joe"};
   
   int i=0;

   srand( time( 0 ) );
   
   // initialize student1
   for ( i = 0; i < NUM_GRADES; i++ )
      student1[ NUM_GRADES ] = rand() % 50 + 50;

   // initialize database
   for ( i = 1; i < NUM_GRADES; i++ ) {
      database[ 0 ][ i ] = student1[ i ];
      database[ 1 ][ i ] = student2[ i ];
      database[ 2 ][ i ] = student3[ i ];
   }

   printDatabase( database, names );

   for ( i=0; i < NUM_STUDENTS; i++ )
   {
      cout << names[i] << "'s highest grade is: " << findHighest( student1 ) << endl
           << names[i] << "'s lowest grade is: " << findLowest( database[ i ] ) << endl;
   }

   return 0;
}

int findHighest( int a[] )
{
   int highest = a[ 0 ];
   
   for ( int i = 1; i <= NUM_GRADES; i++ )
      if ( a[ i ] > highest )
         highest = a[ i ];
   
   return highest;
}

int findLowest( int a[] )
{
   int lowest = a[ 0 ];
   
   for ( int i = 1; i < NUM_GRADES; i++ )
      if ( a[ i ] < lowest )
         lowest = a[i];
   
   return lowest;
}

void printDatabase( const int a[][ NUM_GRADES ], const string names[NUM_STUDENTS] )
{
   cout << "Here is the grade database\n\n"
        << setw( 10 ) << "Name";
   
   for ( int n = 1; n <= NUM_GRADES; n++ )
      cout << setw( 4 ) << n;

   cout << endl;
   
   for ( int i = 0; i < NUM_STUDENTS; i++ ) {
      cout << setw( 10 ) << names[i];
     
      for ( int j = 0; j < NUM_GRADES; j++ )
         cout << setw( 4 ) << a[ i, j ];

      cout << endl;
   }
   cout << endl;
}
0
Comment
Question by:jhson114
[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
  • 2
  • 2
  • +2
9 Comments
 

Assisted Solution

by:Kalvyn
Kalvyn earned 100 total points
ID: 8086074
use names[i].c_str()
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 100 total points
ID: 8086120
You forgot #include <string>.  That's where the stream insertion operator lives.  Good luck.
0
 

Author Comment

by:jhson114
ID: 8086176
thanks alot guys! it works!! :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:bcladd
ID: 9889697
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to mnashadka

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
 

Expert Comment

by:Kalvyn
ID: 9889810
I believe it was my comment that ansered his question.  Points should be awarded to Kalvyn.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9891083
Kalvyn, you're response helped show jhson114 how to convert his/her string into a const char *, not how to insert a string into a stream.  For that, you just need to #include <string> which is where the stream insertion operator for a basic_string lives, and then you can use the stream insertion operator on a string (without calling c_str()).  Maybe we should split them.  Of course, ideally jhson114 would award the points.
0
 

Expert Comment

by:ewwe
ID: 13521057
I still have the same problem. Please help. Here is my code:

#include <iostream.h>
#include <string>                              

using namespace std;

int main (void){
    string strOne, strTwo, myStr;
    strOne = "one";
    strTwo = " and two";
    myStr = strOne + strTwo;
    cout << myStr << endl;               //this line generates the error

    system("PAUSE");
    return 0;
}

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

I am using Visual Studio 6 IDE.

Thanks
0
 

Expert Comment

by:ewwe
ID: 13521141
My problem gone. I removed .h from #include<iostream>. The problem is gone. Thanks all the same.
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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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