need help with function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
//friends
friend ostream& operator << (ostream& leftOp, const MyString& rightOp);
friend bool operator == (const char* leftOp, const MyString& rightOp);
public:
//methods
MyString();
MyString(const char* s);
bool operator == (const MyString& rightOp) const;
bool operator == (const char* rightOp) const;
char operator [] (int sub) const;
char& operator[](int sub);
bool empty() const;
int size() const;
void clear();
private:
//data members
char stringArray[81]; //stores string
int stringSize; //current size of string
static const int stringCapacity = 80; //max size
};
#endif 


above is my header file. i need help with this function: ostream& operator<<(ostream& leftOp, const MyString& rightOp)

This method should loop through the characters of the string array of the MyString object passed in as rightOp and print them one at a time using the stream object passed in as leftOp

This is all I have and im sure its wrong:

leftOp << rightOp.operator[](sub) <<endl;
Your output operator declared as a friend, so you can just do:
leftOp << rightOp.stringArray <<endl;

wow thank you so much you dont know how much i appreciate it. one more thing (if you know how obviously), why am i getting
'cannot convert 'bool' to 'const chat*' for argument '2' to 'int strcmp(const char*, const char*)
for:
1
2
3
4
bool operator == (const char* leftOp, const MyString& rightOp)
{
return (strcmp(leftOp, rightOp.stringArray == 0));
}
You need to move the '== 0' outside the strcmp statement, like this:
1
2
3
bool operator== (const char* leftOp, const MyString& rightOp) {
    return (strcmp(leftOp, rightOp.stringArray) == 0);
}
thank you so much its been a rough week. i appreciate it greatly. this is my first time using this site, is there any way to give you positive feedback or something for helping?
No, just saying thanks is enough!
Topic archived. No new replies allowed.