Operator(s) too many parameters for this function? - c++

Made my own string class (i.e. for homework obviously) and am getting odd syntax errors on two of my operators. My equality and add operators claim I have too many parameters (i.e. in my .h file), but then claim the method does not even belong to the class in my .cpp file!
I even made the equality operator a friend, but the intellisense still gives me the same error messages.
Does anyone know what I am doing wrong??
friend bool operator==(String const & left, String const & right);
string.h
bool operator==(String const & left, String const & right);
String const operator+(String const & lhs, String const & rhs);
string.cpp
bool String::operator==(String const & left, String const &right)
{
return !strcmp(left.mStr, right.mStr);
}
String const String::operator+(String const & lhs, String const & rhs)
{
//Find the length of the left and right hand sides of the add operator
int lengthLhs = strlen(lhs.mStr);
int lengthRhs = strlen(rhs.mStr);
//Allocate space for the left and right hand sides (i.e. plus the null)
char * buffer = new char[lhs.mStr + rhs.mStr + 1];
//Copy left hand side into buffer
strcpy(buffer, lhs.mStr);
//Concatenate right hand side into buffer
strcat(buffer, rhs.mStr);
//Create new string
String newString(buffer);
//Delete buffer
delete [] buffer;
return newString;
}

You need to define operator== outside the class:
bool String::operator==(String const & left, String const &right)
^^^^^^^^ REMOVE THIS
If operator+ is also a friend, it too needs to be defined as a free function (i.e. outside the class).

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;
}

Correct way to write operator+ using rvalues

I've been writing my own String class and I am not sure how to write operator+ correctly considering I could pass rvalues into it.I think I should have the following 3 non-member functions
String operator+(String &&lhs, String &&rhs);
String operator+(String& lhs,String&&rhs);
String operator+(String&&lhs,String&rhs);
However I am not sure how to implement them. Any help would be appreciated.
First, make sure to define copy and move constructors in your String class:
class String
{
private:
char *m_data;
std::size_t m_length;
...
public:
String();
String(const String &src);
String(String &&src);
~String();
...
};
String::String() :
m_data(nullptr),
m_length(0)
{
}
String(const String &src) :
m_data(new char[src.m_length+1]),
m_length(src.m_length)
{
std::copy_n(src.m_data, m_length, m_data);
m_data[m_length] = 0;
}
String(String &&src) :
m_data(nullptr),
m_length(0)
{
std::swap(m_data, src.m_data);
std::swap(m_length, src.m_length);
}
String::~String()
{
delete[] m_data;
}
Then define operator+ and operator+= for the class:
class String
{
public:
...
String& operator+=(const String &rhs);
...
friend String operator+(String lhs, const String &rhs)
{
lhs += rhs;
return lhs;
}
};
String& String::operator+=(const String &rhs)
{
String tmp;
tmp.m_length = m_length + rhs.m_length;
tmp.m_data = new char[tmp.m_length+1];
std:copy_n(m_data, m_length, tmp.m_data);
std:copy_n(rhs.m_data, rhs.m_length, tmp.m_data + m_length);
tmp.m_data[tmp.m_length] = 0;
std::swap(m_data, tmp.m_data);
std::swap(m_length, tmp.m_length);
return *this;
}
By taking a const String & as input on the right side, that will handle both lvalue and rvalue inputs.
For operator+, the left-hand side is taken by value so the compiler can decide the best constructor to use based on whether the input is an lvalue (copy) or rvalue (move).
Alternatively, you can implement it to take const String & on the left side so it still handles lvalues and rvalues, but then you have to implement it similar to how operator+= is implemented to avoid the extra allocation of copying lhs before concatenating onto it:
friend String operator+(const String &lhs, const String &rhs)
{
/*
String tmp(lhs);
tmp += rhs;
return tmp;
*/
String tmp;
tmp.m_length = lhs.m_length + rhs.m_length;
tmp.m_data = new char[tmp.m_length+1];
std:copy_n(lhs.m_data, lhs.m_length, tmp.m_data);
std:copy_n(rhs.m_data, rhs.m_length, tmp.m_data + lhs.m_length);
tmp.m_data[tmp.m_length] = 0;
return tmp;
}
Either way, you should also define a conversion constructor and operator+ for const char * input as well:
class String
{
public:
...
String(const char *src);
...
friend String operator+(const char *lhs, const String &rhs)
{
return String(lhs) + rhs;
/* or:
std::size_t len = std::strlen(lhs);
String tmp;
tmp.m_length = len + rhs.m_length;
tmp.m_data = new char[tmp.m_length+1];
std:copy_n(lhs, len, tmp.m_data);
std:copy_n(rhs.m_data, rhs.m_length, tmp.m_data + len);
tmp.m_data[tmp.m_length] = 0;
return tmp;
*/
}
...
};
String::String(const char *src) :
m_data(nullptr),
m_length(std::strlen(src))
{
m_data = new char[m_length+1];
std::copy_n(src, m_length, m_data);
m_data[m_length] = 0;
}
This will allow concatenating String objects with string literals (String + "literal", "literal" + String, String += "literal", etc).
See operator overloading on cppreference.com for more details.
The way I usually do it is like this:
class foo
{
...
public:
...
foo&& operator +(foo const & other) &&;
foo&& operator +(foo && other) const &;
foo&& operator +(foo && other) &&;
foo operator +(foo const & other) const &;
};
Not sure if Microsoft supports this but this is a good way to do this in more recent standards. Try clang if msvc wont let you.
The advantages of doing it this way are that you get very fine levels of control over the method used. These 4 operations can also be defined outside of the class if needed. But you'll always want 4 for the 4 possibilities of r-value/l-value combinations.
Also, you generally want to qualify l-values as const to indicate that they are not modified.
Simply defining a copy/move constructor is not usually an efficient solution to this problem. You will need a good understanding of how rvalue references work to implement this efficiently.

How do you get a + overload to become recognised in another file C++?

Just started learning C++ recently and I'm attempting to make my own string class from scratch. I'm currently working on concatenating strings by overloading += and + operators. After reading this article, basic-rules-of-operator-overloading, I have come up with the following implementation;
String & String::operator+=(const String &o)
{
char * newBuffer = new char[this->size() + o.size() - 1];
//copy over 'this' string to the new buffer
int index = 0;
while (this->at(index) != 0x0)
{
*(newBuffer + index) = this->at(index);
index++;
}
//copy over the param string into the buffer with the offset
//of the length of the string that's allready in the buffer
int secondIndex = 0;
while (o.at(secondIndex) != 0x0)
{
*(newBuffer + index + secondIndex) = o.at(secondIndex);
secondIndex++;
}
//include the trailing null
*(newBuffer + index + secondIndex) = 0x0;
//de-allocate the current string buffer and replace it with newBuffer
delete[] this->s;
this->s = newBuffer;
this->n = index + secondIndex;
return *this;
}
inline String operator+(String lhs, const String &rhs)
{
lhs += rhs;
return lhs;
}
However, the compiler will not recognise the + overload! It does work if I place the function in the main test file (where I am calling the method) but not if I place it in my String.cpp file where all my other methods are located.
Here is my String.h file if you need it;
#include <iostream>
class String
{
public:
String(const char * s);
String(const String &o);
int size() const;
char at(int i) const;
String &operator+=(const String &o);
private:
char * s;
int n;
//needs to be a friend function defined OUTSIDE of the class as when using
//ostream << String you do not have access to the ostream so they can't be
//member operators
friend std::ostream & operator<<(std::ostream &os, const String &o);
};
Thanks for any help!
(also, anything you think I can improve on in regards to my implementation would be graciously received)
Well everyone already explained, so it should be as simple as just adding the forward declaration to the end of your .h file like this:
#include <iostream>
class String
{
public:
String(const char * s);
String(const String &o);
int size() const;
char at(int i) const;
String &operator+=(const String &o);
private:
char * s;
int n;
//needs to be a friend function defined OUTSIDE of the class as when using
//ostream << String you do not have access to the ostream so they can't be
//member operators
friend std::ostream & operator<<(std::ostream &os, const String &o);
};
//forward declaration
String operator+(String lhs, const String &rhs);
The forward declaration just tells the compiler to look for a function with that signature. When it doesn't find it in your current .cpp file it looks up on the other .cpp files. I hope this helps!

String decorator class leads to numerous build errors

I've created a decorator class for std::string that, when included in the project, causes thousands of build errors originating from files such as cmath.h, cstring.h, xstring.h and I cant figure out why. All files in the project that need string operations use this class instead of std::string for consistency.
I've tried to slowly comment out parts of the decorator class to try to make sense of what is actually happening, but the errors only started to make sense once the entire class had been commented out. There are too many errors to list them all here, but a small sample of the errors include:
Error C2733 'abs': second C linkage of overloaded function not allowed
Error C2065 'allocator': undeclared identifier
Error C2974 'std::basic_string': invalid template argument for '_Alloc'
Error C2873 'strxfrm': symbol cannot be used in a using-declaration
Error C2535 'void std::basic_string<_Elem,_Traits,_Alloc>::_Construct(_Iter,_Iter)': member function already defined or declared
There are thousands of these errors across multiple standard library files. this is the header file for the string decorator:
#pragma once
#include <string>
#include <vector>
namespace Framework
{
class String
{
private:
std::string Data;
public:
String();
String(const char*Init);
String(int Init);
friend String operator+(String &LHS, String &RHS);
friend String operator+(String &LHS, const char *RHS);
friend String operator+(String &LHS, const char RHS);
String& operator+=(String &RHS);
String& operator+=(const char *RHS);
String& operator+=(const char RHS);
friend bool operator==(String &LHS, String &RHS);
friend bool operator==(const String &LHS, const String &RHS);
friend bool operator==(String &LHS, const char *RHS);
friend bool operator==(const String &LHS, const char *RHS);
friend bool operator!=(String &LHS, String &RHS);
friend bool operator!=(const String &LHS, const String &RHS);
friend bool operator!=(String &LHS, const char *RHS);
String& operator=(const char * RHS);
char operator[](int Index);
size_t Length();
size_t IndexOf(String SubString, size_t Offset = 0);
bool Contains(String SubString, size_t Offset = 0);
String SubString(size_t Start, size_t Count = 0);
std::vector<String> Split(char Delimeter, bool KeepEmpty = false);
const char *ToCharString();
void Insert(int Position, String Text);
void RemoveAt(int Index);
int ToInt();
double ToDouble();
bool ToBoolean();
unsigned __int8 ToByte();
};
}
What makes this more baffling is the fact that this facade worked perfectly in another project, its only in this new project that it seems to be failing.
I'm not entirely sure what caused this issue, but the resolution of it was moving the String.h and String.cpp files to a sub folder in the project instead of letting it sit at the root of the project. Once I had done that and reverted everything back to using Framework::String, the project built fine.
A clue as to why this worked may be in the fact that if I right clicked on a #include "String.h" line when String.h and String.cpp were still in the root directory and selected Open Document, it opened the standard library string.h file instead of my String.h file.

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.