Hi:
I am receiving the following error with this program:
Error 13 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Any ideas or solutions. Thanks in advance.
Ass.cpp
#include <iostream>
#include "Month.h"
using namespace std;
int main()
{
// Create a Month object
Month m("October");
cout << m.getMonthName() << endl; //Error
cout << m.getMonthNumber() << endl << endl;
}
Month.h
# include <iostream>
# include "MyString.h"
class Month;
ostream& operator<<(ostream &strm, Month &obj);
class Month
{
private:
string name;
int monthnumber;
public:
Month ();
~Month();
Month(string);
Month(int);
string getMonthName();
int getMonthNumber();
void setMonthName(string);
void setMonthNumber(int);
friend ostream& operator<<(ostream &strm, Month &obj);
};
Month.cpp
# include "Month.h"
# include <iostream>
#include <string>
# include "MyString.h"
using namespace std;
//operator overloading
ostream& operator<<(ostream &strm, Month &obj)
{
strm<<obj.name;
return strm;
}
Try making things easier: using a const char*. Instead of
1 2 3
|
string getMonthName(){
return someString;
};
|
write:
1 2 3
|
const char* getMonthName(){
return someString.c_str();
};
|
Last edited on
Thanks for your answer. It worked!!!
1 2 3 4 5
|
ostream& operator<<(ostream &strm, Month &obj)
{
strm<<obj.name;
return strm;
}
|
this is already correct (or partially correct), remember we overload
<<
so that we can cout the object like this:
1 2 3 4 5
|
Foo foo(/*initiate the class members here*/);
std::cout << foo << std::endl; // outputs all variables in foo
...
// or using getters
GetFoo(foo);
|
Last edited on