Link to home
Start Free TrialLog in
Avatar of jhson114
jhson114

asked on

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

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;
}
SOLUTION
Avatar of Kalvyn
Kalvyn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jhson114
jhson114

ASKER

thanks alot guys! it works!! :)
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 (https://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
I believe it was my comment that ansered his question.  Points should be awarded to Kalvyn.
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.
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
My problem gone. I removed .h from #include<iostream>. The problem is gone. Thanks all the same.