user1149554
user1149554

Reputation: 9

Overload the ++ operator to add 1 to the values stored in an array?

Well the title pretty much says it. I have posted a part of my code, I have no idea how to add 1 to the sides of the triangle (a, b and c)... Any help would be greatly appreciated!

class Triangle
{
public:
    Triangle();
    Triangle(double a, double b, double c);
    ~Triangle();
    double get_side_a() const;
    double get_side_b() const;
    double get_side_c() const;
    Triangle& operator++();
    void operator++(int dummy);
private:
    double* sides;
};

Triangle::Triangle()
{
    sides = new double[3];
    sides[0] = 0;
    sides[1] = 0;
    sides[2] = 0;
} // Constructor

Triangle::Triangle(double a, double b, double c)
{
    sides = new double[3];
    sides[0] = a;
    sides[1] = b;
    sides[2] = c;
} // Constructor with parameters

Triangle::~Triangle()
{
    if(sides)
    {
        delete[] sides;
        sides = 0;
    }
} // Destructor

double Triangle::get_side_a() const
{
    return sides[0];
} // get_side_a

double Triangle::get_side_b() const
{
    return sides[1];
} // get_side_b

double Triangle::get_side_c() const
{
    return sides[2];
} // get_side_c

Triangle& Triangle::operator++()
{
    *this = *this + 1;
    return *this;
} // Oprator ++ (prefix)

void Triangle::operator++(int dummy)
{
    ++(*this);
} // Operator ++ (postfix)

int main()
{
    Triangle tri1, tri2;

    cout << "Enter side legths for triangle 1:" << endl;
    cin >> tri1;
    cout << endl << "Enter side legths for triangle 2:" << endl;
    cin >> tri2;

    cout << endl;
    cout << tri1 << endl;
    cout << tri2 << endl;

    return 0;
}

I get a error: no match for 'operator+' in '*(Triangle*)this + 1' in this particular writing of Triangle& Triangle::operator++()...

Edit: It's been known for C++ to fry brains, but it something else having to find out first hand. 10x everyone.

Upvotes: 0

Views: 232

Answers (4)

Jonathan Wakely
Jonathan Wakely

Reputation: 171393

Others already gave good answers showing how to fix it and indicating other problems in your code, this is a more general answer about the error message.

The error message points to this line:

*this = *this + 1;

It tells you that there is no operator+ defined to allow that addition operation.

As a commenter said, you need to actually read the error message and think about what it says.

The compiler is correct, you haven't defined that operator, so of course that line won't compile. Did you expect it to? Do you also expect to be able to multiply your type by 10? Or divide it by 2? What would those operations mean for your type? How would the compiler know what to do?

If the compiler tells you there's no operator that allows that expression it means you haven't written an operator that matches the argument types.

Upvotes: 1

Void - Othman
Void - Othman

Reputation: 3481

There are a few problems with your code:

  • Your postfix increment doesn't return the old value. Code should adhere to the expected behavior of a given operator.
  • You'll need to implement copying to correctly implement the postfix increment semantics.
  • You have no Triangle stream insertion/extractions operators, which is why your code won't compile.

Try something like this:

#include <iostream>
#include <vector>

class Triangle
{
public:
    Triangle() : sides(3) {}
    Triangle(double a, double b, double c) : sides({a, b, c }) {}
    Triangle(Triangle const & other) : sides(other.sides) {}
    ~Triangle() {}
    void operator=(Triangle other) { sides.swap(other.sides); }
    double get_side_a() const { return sides[0]; }
    double get_side_b() const { return sides[1]; }
    double get_side_c() const { return sides[2]; }

    Triangle & operator++()
    {
        for (auto & side : sides)
            ++side;

        return *this;
    }

    Triangle operator++(int)
    {
        Triangle tmp(*this);
        ++(*this);
        return tmp;
    }

private:
    std::vector<double> sides;
};

std::ostream & operator<< (std::ostream & o, Triangle const & t)
{
    o << '('
      << t.get_side_a() << ", "
      << t.get_side_b() << ", "
      << t.get_side_c() << ')';

    return o;
}

int main()
{
    using namespace std;

    Triangle t(1, 2, 3);
    cout << t << endl;

    Triangle t1 = t++;  // (1, 2, 3) - old value returned
    cout << t1 << endl;

    Triangle t2 = ++t;  // (2, 3, 4) + (1, 1, 1) = (3, 4, 5)
    cout << t2 << endl;
}

Compiling and running with g++:

$ g++ -std=c++0x foo.cpp
$ ./a.out
(1, 2, 3)
(1, 2, 3)
(3, 4, 5)

HTH!

Upvotes: 1

firexfighterx
firexfighterx

Reputation: 679

You dont need the overloaded function that takes a parameter. The compiler will look at that code as Triangle object ++ Triangle object. In your one that takes no parameters add the code above to change each side. Once you do the compiler will see the ++ operator with a triangle object on the left side to execute your code. Hope this helps.

Upvotes: -1

Attila
Attila

Reputation: 28772

You will either need to overload operator+ with Triangle and int or change the operator++ logic to work directly on the sides of the *this object

In either case the logic is something like this (for ++):

sides[0]++;
sides[1]++;
sides[2]++;

Upvotes: 4

Related Questions