Set with Custom String Class Problem - c++

I have written a custom string class.
I want to use STL set with it. I have overloaded operator <
But still its giving me problem
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const String' (or there is no acceptable conversion)
1> could be 'String &String::operator =(const String &)'
1> 'String &String::operator =(const char *)'
1> 'String &String::operator =(const wchar_t *)'
1> while trying to match the argument list '(const String, const String)'
I guess, It is asking for overloaded operator= (const String , const String)
But its impossible to create such an overloaded function
My String class is this
String ();
String (const char * pStr);
String (const long int pData);
String (const double pData);
String (const int pData);
String (const wchar_t * pStr);
//Copy Constructors
String (const String& rhs);
String (const String& rhs, const int pStartIndex, const int pNumChar);
//Overloaded Operators
String & operator= (const String & rhs);
String & operator= (const char * rhs);
String & operator= (const wchar_t * rhs);
String operator+ (const String& rhs);
//String & operator+= (const char ch);
String & operator+= (const String& rhs);
friend bool operator== (const String& lhs, const String& rhs);
friend bool operator< (const String& lhs, const String& rhs) {
return strcmp(lhs.vStr, rhs.vStr);
}
friend ostream& operator<< (ostream& ostr, String& rhs);
char & operator[] (int pIndex);
char operator[] (int pIndex) const;
const char * String::Buffer () const;
wchar_t * GetTChar();
int String::GetLength () const;
~String ();

"no operator found which takes a left-hand operand of type 'const String'"
it seem you have an expression like
a=b;
where both a and b are const String.
You cannot assign to a const (although the compiler looks desperately seeking for an implementation of such an assignment)

OK, well I can only answer the question you've posed with the information you've given, and the answer is that this works for me.

Related

0xC0000005: Access violation reading location 0x00000000. issues with overloaded == operator

Hi there I'm currently working on a program for a data structures course that I am taking and I'm working on a part of an overloaded extraction operator. I am currently receiving the error Access violation reading location 0x00000000. when I attempt to compare two My String Objects with one another. A MyString object is essentially a c String, here is the class definition
class MyString {
private:
char* str;
public:
MyString();
MyString(const char*);
MyString(const MyString&);
~MyString();
int length() const;
void read(istream&, char);
static const int MAX_INPUT_SIZE = 127;
MyString& operator=(const MyString&);
MyString& operator +=(const MyString&);
friend MyString operator +(const MyString&, const MyString&);
char operator[](int location)const;
char& operator[](int location);
friend ostream& operator<<(ostream&, const MyString&);
friend istream& operator>>(istream&, MyString&);
friend bool operator <(const MyString& left, const MyString& right);
friend bool operator <=(const MyString& left, const MyString& right);
friend bool operator >(const MyString& left, const MyString& right);
friend bool operator >=(const MyString& left, const MyString& right);
friend bool operator ==(const MyString& left, const MyString& right);
friend bool operator !=(const MyString& left, const MyString& right);
};
}
#endif
this is the overloaded == operator throwing the exception
bool operator ==(const MyString& left, const MyString& right) {
return strcmp(left.str, right.str) == 0;
}
this is the context in which i am making the comparison, assume that temp is a valid MyString object.
for (int i = 0; i < sizeof(cs_measure::Measure::unitStrings); i++) {
if (cs_measure::Measure::unitStrings[i] == temp) {
readMe.unit = i;
in >> readMe.unit;
}
}
this is the array that is being referenced in the for loop
const MyString Measure::unitStrings[] =
{ "dram", "tsp", "tbsp", "oz", "cup", "pint",
"qt", "gal", "peck", "bushel", "barrel", "acre_ft" };
This is my first time posting to stack overflow so I have left out any crucial information that may be useful for solving this issue please let me know and I would be happy to help.
As mentioned in the comments, sizeof(cs_measure::Measure::unitStrings) is not the number of items in the array cs_measure::Measure::unitStrings. It is the number of bytes the array occupies in memories.
Since the size in bytes is almost surely larger than the number of elements, you will access the array out-of-bounds in the loop, causing undefined behavior.
You can get the number of items in a built-in array with
std::size(cs_measure::Measure::unitStrings)
since C++17 (may require #include<iterator> if you have not included any container library header).
Or if you can't use C++17, you can define your own version of it, though the C++17 standard version is a bit more powerful. (from cppreference.com):
template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}

overload array operator for mystring class

I need help figuring out how to overload the array operator for a MyString class that I have to create. I already have everything else figured out, but the arrays are giving me trouble, for some reason.
Here is my header file:
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <cstring> // For string library functions
#include <cstdlib> // For exit() function
using namespace std;
// MyString class: An abstract data type for handling strings
class MyString
{
private:
char *str;
int len;
public:
// Default constructor.
MyString()
{
str = 0;
len = 0;
}
// Convert and copy constructors.
MyString(char *);
MyString(MyString &);
// Destructor.
~MyString()
{
if (len != 0)
delete [] str;
str = 0;
len = 0;
}
// Various member functions and operators.
int length() { return len; }
char *getValue() { return str; };
MyString operator+=(MyString &);
MyString operator+=(const char *);
MyString operator=(MyString &);
MyString operator=(const char *);
bool operator==(MyString &);
bool operator==(const char *);
bool operator!=(MyString &);
bool operator!=(const char *);
bool operator>(MyString &);
bool operator>(const char *);
bool operator<(MyString &);
bool operator<(const char *);
bool operator>=(MyString &);
bool operator>=(const char*);
bool operator<=(MyString &);
bool operator<=(const char *);
MyString operator [](MyString *);
// Overload insertion and extraction operators.
friend ostream &operator<<(ostream &, MyString &);
friend istream &operator>>(istream &, MyString &);
};
#endif
What would the body look like for MyString::operator []?
MyString MyString::operator [](MyString *)
{
... what goes here
}
The syntax for using the array operator with an object of the given class is:
MyString s("Test");
char c = s[0];
The argument to the function is an integral value.
Hence, the operator needs to be declared as:
// The non-const version allows you to change the
// content using the array operator.
char& operator [](size_t index);
// The nconst version allows you to just get the
// content using the array operator.
char operator [](size_t index) const;
MyString MyString::operator [](MyString *)
That's not how you should typically use a subscript operator.
What do you expect when you are using the [] operator? By the way you declared it, you are using a string pointer as argument, and receiving a string as return.
Usually, you pass an index type (commonly an unsigned-integer like size_t) and return the character at that position. If that's what you want, you should do something along these lines:
char& MyString::operator [](size_t position)
{
// some error handling
return str[position];
}
char MyString::operator [](size_t position) const { /* ... */ }
For overall guidelines on overloading operators, take a look at What are the basic rules and idioms for operator overloading?.
Also, I would point out that your destructor is a bit odd:
if (len != 0)
delete [] str;
str = 0;
len = 0;
Your indentation level suggests that you expect everything to happen inside the if statement, but only the first one will. That is not particularly dangerous in this case, because only the delete would suffice.
There is no problem in deleteing a null pointer, and str and len will be destroyed shortly after, so you don't have to bother resetting them.

Breakpoint on delete[] when destroying char *

I'm doing an assignment for class in which I can't use the string class. I need to use char* as arrays and doing arithmetic with them.
My code being executed in main is the following: I create 2 myString objects and I add them. Then this is done correctly. Both strings get concatenated. but, there's a breakpoint at delete[] str. Can you tell me where I do wrong exactly? I'd like to understand what happens.
myString& myString :: operator+ (const myString& s)
{
myString tmp; //myString temporal
strcpy_s(tmp.str, strlen(str)+1,str); //STR is copied to tmp.str
Alloc(strlen(s.str)+size+1); //Then memory is allocated for both values
strcpy_s(str, strlen(tmp.str)+1, tmp.str); //WE COPY TMP.STR INTO STR NOW WITH ENOUGH SIZE FOR THE NEXT...
strcat_s(str, strlen(s.str) + size+1, s.str); //..ARGUMENT WE CONCATENATE 2 MYSTRING.STR
return (*this);
}
This is the class myString
class myString
{
public:
//Propietats
int size;
char* str;
//CONSTRUCTORS
myString();
myString(const myString&);
//myString(myString&);
myString(const char*, ...);
//Utilities
int Len(char*);
const void Clear();
const void Alloc(const int);
//Operators
bool operator== (const myString&) const;
bool operator== (const char* s) const;
const myString& operator= (myString&);
const myString& operator= (const char* s);
bool operator!= (const myString&) const;
bool operator!= (const char* s) const;
myString& operator+ (const myString&);
myString& operator+ (const char*);
//Metodes
~myString()
{
delete[] str; // **ERROR** THERE'S A BREAKPOINT HERE
}
};
#endif
My error is that there's a breakpoint in delete[] str; And I don't know what to do. It means there's an overflow? How do I solve it?
I'm quite new to pointer arithmetic so don't be harsh.
myString& myString :: operator+ (const myString& s)
{
myString tmp; //myString temporal
tmp.Alloc(strlen(str)+1); // Add this line
strcpy_s(tmp.str, strlen(str)+1,str); //STR is copied to tmp.str
...
You are not allocating room in tmp string. Once allocated, it should work fine.
You are probably corrupting the heap.
myString tmp;
What is tmp.str after this instruction ? Is tmp.str a NULL pointer ? A pointer to a buffer with some default size ?
strcpy_s(tmp.str, strlen(str)+1,str);
Did you make sure tmp.str has room for strlen(str)+1 char ?
You probably should call tmp.Alloc(strlen(str)+1) before this instruction.

Object as array index

I have the following class:
class MyInteger
{
private:
__int64 numero;
static __int64 int64Pow(__int64, __int64);
public:
// It doesn't matter how these methods are implemented
friend class MyInteger;
MyInteger(void);
MyInteger(const MyInteger&);
MyInteger(const __int64&);
~MyInteger(void);
static MyInteger const minValue;
static MyInteger const maxValue;
MyInteger& operator = (const MyInteger&);
MyInteger operator + (const MyInteger&) const;
MyInteger operator - (const MyInteger&) const;
MyInteger operator * (const MyInteger&) const;
MyInteger operator / (const MyInteger&) const;
MyInteger& operator += (const MyInteger&);
MyInteger& operator -= (const MyInteger&);
MyInteger& operator *= (const MyInteger&);
MyInteger& operator /= (const MyInteger&);
MyInteger operator % (const MyInteger&) const;
MyInteger& operator %= (const MyInteger&);
MyInteger& operator ++ ();
MyInteger operator ++ (int);
MyInteger& operator -- ();
MyInteger operator -- (int);
bool operator == (const MyInteger&) const;
bool operator != (const MyInteger&) const;
bool operator > (const MyInteger&) const;
bool operator < (const MyInteger&) const;
bool operator >= (const MyInteger&) const;
bool operator <= (const MyInteger&) const;
int toStdInt() const
{
return (int)numero;
}
float toStdFloat() const;
double toStdDouble() const;
char toStdChar() const;
short toStdShortInt() const;
long toStdLong() const;
long long toStdLongLong() const;
unsigned int toStdUInt() const;
__int64 toStdInt64() const;
unsigned __int64 toStdUInt64() const;
unsigned long long int toStdULongLong() const;
long double toStdULongDouble() const;
template<class Type>
Type& operator[](Type* sz)
{
return sz[toStdULongLong()];
}
};
template<class Type>
Type* operator+(const Type* o1, const MyInteger& o2)
{
return ((o1) + (o2.toStdInt()));
}
I'd like to use this class to access array elements like this:
MyInteger myInt(1);
int* intPtr = (int*)malloc(sizeof(int) * N);
intPtr[myInt] = 1;
I thought that the function
template<class Type>
Type* operator+(const Type* o1, const MyInteger& o2)
{
return ((o1) + (o2.toStdInt()));
}
could solve my problem, because as this post reports (Type of array index in C++) "The expression E1[E2] is identical (by definition) to *((E1)+(E2))", but I get the C2677 error ('[' operator: no global operator found which takes type 'MyInteger' (or there is no acceptable conversion))
Can someone clarify me this situation?
Thanks
You may be able to do that by overriding the cast to int of your MyInteger class in a way similar to:
class MyInteger {
...
operator int() const
{
return toStdInt(); /** Your class as an array index (int) */
}
...
}

error C2593; Program compiles under VS6 but not under VS10 [duplicate]

I get the following error:
1>c:\documents and settings\krzys\desktop\desktop icons\ollydbg\plugins\odbgscript\OllyLangCommands.cpp(3602): error C2593: 'operator =' is ambiguous
1> c:\documents and settings\krzys\desktop\desktop icons\ollydbg\plugins\odbgscript\var.h(45): could be 'var &var::operator =(const long double &)'
1> c:\documents and settings\krzys\desktop\desktop icons\ollydbg\plugins\odbgscript\var.h(42): or 'var &var::operator =(const int &)'
1> c:\documents and settings\krzys\desktop\desktop icons\ollydbg\plugins\odbgscript\var.h(41): or 'var &var::operator =(const ulong &)'
1> while trying to match the argument list '(var, std::streamsize)'
At this code:
map<string, var> variables;
streamsize sum = 0;
if (sum) {
variables["$RESULT"] = sum; // Error 1
return true;
}
and
class var
{
public:
ulong dw;
string str;
long double flt;
vtype vt;
bool isbuf;
int size;
var();
var(const var& rhs);
var(string& rhs);
var(ulong rhs);
var(int rhs);
var(long double rhs);
// less than zero this < rhs
// zero this == rhs
// greater than zero this > rhs
int compare(const var& rhs) const;
int compare(const string& rhs) const;
int compare(const ulong& rhs) const;
int compare(const int& rhs) const;
int compare(const long double& rhs) const;
string strclean(void);
string strbuffhex(void);
string strbuff(void);
var& operator=(const var& rhs);
var& operator=(const string& rhs);
var& operator=(const ulong& rhs); // Error 4
var& operator=(const int& rhs); // Error 3
var& operator=(unsigned short& rhs);
var& operator=(unsigned char& rhs);
var& operator=(const long double& rhs); // Error 2
var& operator+=(const var& rhs);
var& operator+=(const string& rhs);
var& operator+=(const ulong& rhs);
var& operator+=(const int& rhs);
var& operator+=(const long double& rhs);
void resize(ulong newsize);
};
Compiled perfectly with VS6, but now I'm getting this error with VS10.
Check sum's type and variables's methods. There's a chance they've added new operator= overloads in the switch from VS6 to VS2010.
Your variables map holds values of type var. sum isn't a var, so a conversion needs to be made.
The streamsize type in VC6 is a simple int and the var class will convert that to a var type implicitly.
In VS10, streamsize is an __int64, which you have no implicit conversion to a var for.