error C2676: binary '++': 'Vector' doesn’t define

error C2676: binary '-': 'Vector' doesn’t define

include <iostream> #include <cmath> #include <iostream> #include <iomanip> using namespace std; class Vector { private: int* first; int* second; public: Vector(int f = 0, int s = 0); //Constructor Vector(const Vector& other); //Copy Constructor ~Vector(); //Destructor //SETTERS, GETTERS int* getFirst(); int* getSecond(); void setFirst(int* f); void setSecond(int* s); Vector operator ++ (); // Overloaded ++ operator Vector operator -- (); // Overloaded -- operator }; Vector Vector :: operator ++ () // Overloaded ++ operator { first++; second++; return *this; } Vector Vector :: operator -- () // Overloaded -- operator { --first; --second; return *this; } int main() { int first, second; cout << "Please enter the first vector (first , second ) : "; cin >> first >> second; Vector v1(first, second); cout << "Please enter the second vector (first , second ) : "; cin >> first >> second; Vector v2(first, second); cout << "The vector you've entered is: "; v1.print(); cout << " "; v2.print(); v1++; v2++; v1.print(); cout << " "; v2.print(); return 0; } 

    2 answers 2

    It's simple - prefix ++ is defined as

     operator ++ (); 

    and postfix - like

     operator ++ (int); 

    Correct either the definition of the operator or replace the v++ call with ++v .

    • Thank you very much for the explanation - Poli
    • @Poli Well, if you are satisfied with the answer - mark with a bird :) - Harry

    Just giving my vector class, once did - you can take the whole class, but you can remake it by analogy:

    .N

     typedef struct Vector2 { public: Vector2(); Vector2(float value); Vector2(float x, float y); Vector2 operator+(const Vector2& ROperand); Vector2 operator-(const Vector2& ROperand); Vector2 operator*(const Vector2& ROperand); Vector2 operator/(const Vector2& ROperand); Vector2 operator++(); Vector2 operator++(int); Vector2 operator+=(float var); Vector2 operator--(); Vector2 operator--(int); Vector2 operator-=(float var); bool operator>(const Vector2& ROperand); bool operator>=(const Vector2& ROperand); bool operator<(const Vector2& ROperand); bool operator<=(const Vector2& ROperand); bool operator==(const Vector2& ROperand); bool operator!=(const Vector2& ROperand); bool operator!=(float var); float x, y; //V4 vec4XY(); //V4 vec4WH(); }V2; 

    .srr

     #include "Vector2.h" Vector2::Vector2() { x = 0; y = 0; } Vector2::Vector2(float value) { x = value; y = value; } Vector2::Vector2(float x, float y) { this->x = x; this->y = y; } //V4 Vector2::vec4XY() //{ // return V4(x,y,0,0); //} // //V4 Vector2::vec4WH() //{ // return V4(0,0,x,y); //} Vector2 Vector2::operator+(const Vector2& ROperand) { Vector2 Result; Result.x = x + ROperand.x; Result.y = y + ROperand.y; return Result; } Vector2 Vector2::operator-(const Vector2& ROperand) { Vector2 Result; Result.x = x - ROperand.x; Result.y = y - ROperand.y; return Result; } Vector2 Vector2::operator*(const Vector2& ROperand) { Vector2 Result; Result.x = x * ROperand.x; Result.y = y * ROperand.y; return Result; } Vector2 Vector2::operator/(const Vector2& ROperand) { Vector2 Result; Result.x = x / ROperand.x; Result.y = y / ROperand.y; return Result; } Vector2 Vector2::operator++() { Vector2 Result; Result.x = ++x, Result.y = ++y; return Result; } Vector2 Vector2::operator++(int) { Vector2 Result; Result.x = x++, Result.y = y++; return Result; } Vector2 Vector2::operator--() { Vector2 Result; Result.x = --x, Result.y = --y; return Result; } Vector2 Vector2::operator--(int) { Vector2 Result; Result.x = x--, Result.y = y--; return Result; } bool Vector2::operator>(const Vector2& ROperand) { if ((x > ROperand.x) , (y > ROperand.y)) return 1; return 0; } bool Vector2::operator>=(const Vector2& ROperand) { if ((x >= ROperand.x) , (y >= ROperand.y)) return 1; return 0; } bool Vector2::operator<(const Vector2& ROperand) { if ((x < ROperand.x) , (y < ROperand.y)) return 1; return 0; } bool Vector2::operator<=(const Vector2& ROperand) { if (x <= ROperand.x , (y <= ROperand.y)) return 1; return 0; } bool Vector2::operator==(const Vector2& ROperand) { if ((x == ROperand.x) , (y == ROperand.y)) return 1; return 0; } bool Vector2::operator!=(const Vector2& ROperand) { if ((x != ROperand.x) , (y != ROperand.y)) return 1; return 0; } bool Vector2::operator!=(float var) { if (!((x == var) , (y == var))) return 1; return 0; } Vector2 Vector2::operator+=(float var) { Vector2 Result; Result.x = x += var; Result.y = y += var; return Result; } Vector2 Vector2::operator-=(float var) { Vector2 Result; Result.x = x -= var; Result.y = y -= var; return Result; }