When I try to compile the program the re is warning: undefined reference to 'operator<<(std ::basic_ostream <char, std::char_trait s<char> >&, MyString const)'
And there are more similar warnings. Can anyone help me, please
Hi, here is the class definition:
Here is the class implementation:
Here is the main program:
And there are more similar warnings. Can anyone help me, please
Hi, here is the class definition:
Code:
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
//friend functions
friend ostream& operator << (ostream& leftOp, const MyString& rightOp);
friend istream& operator >> (istream& leftOp, 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);
const char* c_str() const;
bool empty() const;
int size() const;
void clear();
private:
//data memebers
char StringArray[81]; //to store string
int StringSize; //current size of the string
static const int StringCapacity = 80; //maximum size
};
#endif
Code:
#include "stdafx.h"
#include "MyString.h"
//no argument constructor
//initializes the string array with null value
//and size to zero
MyString::MyString()
{
strcpy(StringArray, "");
StringSize = 0;
}
//argumented constructor
//initializes string array with passed argument
MyString::MyString(const char* s)
{
int len = strlen(s);
if(len <= StringCapacity) //size <= 80
{
strcpy(StringArray, s);
StringSize = len;
}
else //size > 80
{
int i;
for(i=0;i<=StringCapacity;i++)
StringArray[i] = s[i];
StringArray[i] = '\0';
StringSize = StringCapacity;
}
}
//overloded equal operator with MyString argument
bool MyString::operator==(const MyString& rightOp)const
{
return (strcmp(this->StringArray, rightOp.StringArray) == 0);
}
//overloded equal operator with charcter string
bool MyString::operator==(const char* rightOp)const
{
return (strcmp(this->StringArray, rightOp) == 0);
}
//overloaded indexing operator
//returns character at the specified position in the array
char MyString::operator[](int sub) const
{
if ( sub >= 0 && sub <= StringSize)
return StringArray[sub];
else
return ' ';
}
//overloaded indexing operator
//returns reference of the character at the specified position in the array
char& MyString::operator [] (int sub)
{
return StringArray[sub];
}
//returns StringArray
const char* MyString::c_str()const
{
return StringArray;
}
//returns true if size of StringArray is zero
//otherwise false
bool MyString::empty()const
{
return (StringSize == 0);
}
//returns size of the StringArray
int MyString::size() const
{
return StringSize;
}
//clears the StringArray
void MyString::clear()
{
strcpy(StringArray, "");
StringSize = 0;
}
//friend function definitions
//overloaded extraction operator
ostream& operator <<(ostream& leftOp, const MyString& rightOp)
{
leftOp << rightOp.c_str() <<endl;
return leftOp;
}
//overloaded insertion operator
istream& operator >> (istream& leftOp, MyString& rightOp)
{
char st[81];
leftOp >> st;
MyString s(st);
rightOp = s;
return leftOp;
}
//overloaded equality operator with reference of MyString argument
//and a charcter string
bool operator == (const char* leftOp, const MyString& rightOp)
{
return (strcmp(leftOp, rightOp.c_str()) == 0);
}
Code:
#include <iostream>
#include <cstring>
#include "MyString.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << "Testing default constructor\n\n";
MyString s1;
cout << "s1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
cout << endl;
cout << "Testing second constructor\n\n";
MyString s2 = "some text";
cout << "s2: " << s2 << endl;
cout << "s2 size: " << s2.size() << endl;
cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n");
cout << endl;
cout << "Testing size limit on second constructor\n\n";
MyString s3 = "This is a really long string and not all of it will actually end up in the array, but that is okay";
cout << "s3: " << s3 << endl;
cout << "s3 size: " << s3.size() << endl;
cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
cout << endl;
cout << "Testing write form of subscript operator\n\n";
s2[0] = 'S';
s2[5] = 'T';
cout << "Testing read form of subscript operator\n\n";
cout << "s2: ";
for (int i = 0; i < s2.size(); i++)
cout << s2[i];
cout << endl << endl;
cout << "Testing conversion to C string\n\n";
char ar[81];
strcpy(ar, s2.c_str());
cout << "s2 as C string: " << ar << endl << endl;
cout << "Testing equality operators\n\n";
MyString s4 = "Some Text";
cout << "s2 and s4 are " << ((s2 == s4) ? "equal\n" : "not equal\n");
cout << "s3 and s4 are " << ((s3 == s4) ? "equal\n" : "not equal\n");
cout << "s4 and \"Some Text\" are " << ((s4 == "Some Text") ? "equal\n" : "not equal\n");
cout << "\"More Text\" and s4 are " << (("More Text" == s4) ? "equal\n" : "not equal\n");
cout << endl;
cout << "Testing clear() method\n\n";
s3.clear();
cout << "s3: " << s3 << endl;
cout << "s3 size: " << s3.size() << endl;
cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
cout << endl;
cout << "Testing stream extraction operator\n\n";
cout << "Type a word: ";
cin >> s1;
cout << "\ns1: " << s1 << endl;
cout << "s1 size: " << s1.size() << endl;
cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
return 0;
}
Comment