3 Replies - 424 Views - Last Post: 27 May 2018 - 07:30 AM Rate Topic: -----

#1 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,310
  • Joined: 22-October 08

[ ] Operator Overloading

Posted 27 May 2018 - 12:36 AM

Hey Guys n Gals,

It's been quite some time since I've posted on DIC. A couple of years IIRC :o/>

I've come to an impasse in brushing up on my C++ skills. I'm overloading [ ] in my class like so:

string & operator[](int);
const string & operator[](int) const;



So we have our non-constant and constant operator overrides for the array subscript overloads.

Now here is where I find it gets interesting. For some reason, the following will work,
	cout << ton->operator[](3) << "\n";



while

cout << ton[3] << "\n";



will not, and I can't work out why. I've spent the last hour searching for a solution, but I cannot find one.

What am I doing wrong here guys?

Note: I haven't much experience with operator overloading, that's why I am experimenting now.

Thanks

Aaron1178

Is This A Good Question/Topic? 0
  • +

Replies To: [ ] Operator Overloading

#2 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: [ ] Operator Overloading

Posted 27 May 2018 - 02:51 AM

You will need to post more content (code) and show us any error/warning messages your compiler is generating. A small complete program that illustrates the problem will probably be best.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,310
  • Joined: 22-October 08

Re: [ ] Operator Overloading

Posted 27 May 2018 - 03:55 AM

Okay, no worries. I'm not on my development machine at the moment, so I'll try to paraphrase it.

Sorry Guys, the error I receive is:

E0349	no operator "<<" matches these operands	
operand types are: std::ostream << MyClass



Which makes me wonder; I am returning a std::string not a MyClass object.

MyClass.h
class MyClass
{
    public:
            //non-constant reference
            string & operator[](int);

            //constant reference
            const string & operator[](int) const;

            //omitted stuff
};



MyClass.cpp
string & MyClass::operator[](int inOffset)
{
    if (inOffset > myVector.size())
    {
	cout << "ERROR: Offset out of range! []\n";
	return string();
    }

    return myVector[inOffset];
}

const string & MyClass::operator[](int inOffset) const
{
    if (inOffset > myVector.size())
    {
	cout << "ERROR: Offset out of range! []\n";
	return string();
    }

    return myVector[inOffset];
}



main.cpp
//omitted for clarity

   MyClass * tmpMyClass = new MyClass(//Input variables ommitted);

   
   cout << tmpMyClass->RandFunc1();
   cout << tmpMyClass->RandFunc2();
   
   //The following acts like intended, achieving the desired goal
   cout << tmpMyClass->operator[](3); //prints what is at offset 3 in the myVector in MyClass

   //The following however, does not work
   cout << tmpMyClass[3]; //Results in an error
//omitted for clarity


This post has been edited by aaron1178: 27 May 2018 - 03:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: [ ] Operator Overloading

Posted 27 May 2018 - 07:30 AM

   MyClass * tmpMyClass = new MyClass(//Input variables ommitted);
...
   //The following however, does not work
   cout << tmpMyClass[3]; //Results in an error


The problem is probably because tmpMyClass is a pointer not a "normal" instance. Why are you using a pointer instead of just using a normal instance.

Note 1: I doubt that that is the only message your compiler is generating. Normally decent compilers give you a "few" hints as to what will fix the issue.

By the way here is a small complete example program (one that doesn't omit anything pertinent to the problem) that illustrates the problem:
#include <string>
#include <iostream>
#include <vector>

class MyClass
{
    public:
        //constant reference
        const std::string & operator[](size_t index) const;

        MyClass(const std::vector<std::string>& t) : test(t) {}
    private:
        std::vector<std::string> test;
};

// Note that the parameter is a size_t because that is the type std::vector.size() returns.
const std::string & MyClass::operator[](size_t index) const  
{
    if (index > test.size())
    {
        std::cout << "ERROR: Offset out of range! []\n";
        return std::string();  // This line should generate a warning about returning a local value.
    }

    return test[index];
}


int main()
{
    using namespace std;
    std::vector<std::string> test{"Hello there big Fellow", "BOO", "George", "Smith"};
   MyClass * tmpMyClass = new MyClass(test);   // Why oh why is this a pointer using horrible manual management instead of just a "normal" instance?

   //The following acts like intended, achieving the desired goal
   cout << tmpMyClass->operator[](3); //prints what is at offset 3 in the myVector in MyClass

   //The following however, does not work
   cout << tmpMyClass[3]; //Results in an error. // SEE NOTE 2.

   delete tmpMyClass;
   // Nothing ommited for clarity!
   return 0;
}



Errors generated by my compiler for the above code.
Spoiler


Note 2: This "error" is, as I suspected, being caused because you are using a pointer instance, not a "normal" instance. Since you used the horrible manual memory management you will need to properly dereference that pointer. Or better yet just use a "normal" instance and avoid the error prone manual memory management.


Jim

This post has been edited by jimblumberg: 27 May 2018 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1