Operator overloading
Jun 28, 2013 at 5:18pm UTC
I am getting an error and I have no idea why, I think it pertains to my cin statement but cannont figure out what is going on exactly. Can anyone help?
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 29 30
#ifndef H_myString
#define H_myString
#include <iostream>
using namespace std;
class newString
{
friend ostream& operator <<(ostream&, const newString&);
friend istream& operator >>(istream&, newString&);
public :
const newString& operator =(const newString&);
newString(const char *); //constructor - conversion from char string
newString(); //default constructor
newString(const newString&); //copy constructor
~newString(); //destructor
char & operator [] (int );
const char & operator [] (int ) const ;
bool operator ==(const newString&) const ;
bool operator !=(const newString&) const ;
bool operator <=(const newString&) const ;
bool operator <(const newString&) const ;
bool operator >=(const newString&) const ;
bool operator >(const newString&) const ;
private :
char *strPtr;
int strLength;
};
#endif
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "myString.h"
using namespace std;
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char [strLength + 1];
strcpy(strPtr, str);
}
newString::newString()
{
strLength = 0;
strPtr = new char [1];
strcpy(strPtr, "" );
}
newString::newString(const newString &other)
{
strLength = other.strLength;
strPtr = new char [strLength + 1];
strcpy(strPtr, other.strPtr);
}
newString::~newString()
{
delete [] strPtr;
}
const newString& newString::operator =(const newString &other)
{
if (this != &other)
{
delete [] strPtr;
strLength = other.strLength;
strPtr = new char [strLength + 1];
strcpy(strPtr, other.strPtr);
}
return *this ;
}
char & newString::operator [](int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char & newString::operator [](int index) const
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
bool newString::operator ==(const newString &other) const
{
return (strcmp(strPtr, other.strPtr) == 0);
}
bool newString::operator <(const newString &other) const
{
return (strcmp(strPtr, other.strPtr) < 0);
}
bool newString::operator <=(const newString &other) const
{
return (strcmp(strPtr, other.strPtr) <= 0);
}
bool newString::operator >(const newString &other) const
{
return (strcmp(strPtr, other.strPtr) > 0);
}
bool newString::operator >=(const newString &other) const
{
return (strcmp(strPtr, other.strPtr) >= 0);
}
bool newString::operator !=(const newString &other) const
{
return (strcmp(strPtr, other.strPtr) != 0);
}
ostream& operator <<(ostream& out, const newString& str)
{
out << str.strPtr;
return out;
}
istream& operator >>(istream in, newString& str)
{
char temp[81];
in >> setw(81) >> temp;
str = temp;
return in;
}
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 29 30 31 32 33 34
#include <iostream>
#include "myString.h"
using namespace std;
int main()
{
newString str1 = "Sunny" ;
const newString str2("Warm" ); //initialize using conversion constructor
newString str3, str4;
cout << str1 << " " << str2 << " ***" << str3 << "###." << endl;
if (str1 <= str2)
cout << str1 << " is less than " << str2 << endl;
else
cout << str2 << " is less then " << str1 << endl;
cout << "Enter a string with a minimum length of 7: " ;
cin >> str1;
cout << endl;
cout << "New value of str1 = " << str1 << endl;
str4 = str3 = "Birth Day" ;
cout << "str3 = " << str3 << ", str4 = " << str4 << endl;
str3 = str1;
cout << "New value of str3 = " << str3 << endl;
str1 = "Birght Sky" ;
str3[1] = str1[5];
cout << "After replacing 2nd character of str3 = " << str3 << endl;
str3[2] = str2[0];
cout << "After replacing 3rd character of str3 = " << str3 << endl;
str3[5] = 'g' ;
cout << "After replacing 6th character of str3 = " << str3 << endl;
system("PAUSE" );
return 0;
}
1>main.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class newString &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVnewString@@@Z) referenced in function _main
1>C:\Users\Joseph\Documents\Visual Studio 2008\Projects\MoreExamples\Debug\MoreExamples.exe : fatal error LNK1120: 1 unresolved externals
Jun 28, 2013 at 5:23pm UTC
Ah nevermind, I figured it out, missed the reference in the cpp file!!
Topic archived. No new replies allowed.