Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
cant use a function overload operator with vec2& vec2::operator+=(const vec2& other)
class in vec2.cpp or i get no declaration matches sparky::maths::vec2& sparky::maths::vec2::operator+=(const sparky::maths::vec2&)


[edit]
Added by OP as a solution:
find way how to declare vec2& vec2::operator+=, vec2& vec2::operator-=,
vec2& vec2::operator*=, and vec2& vec2::operator/=

[/edit]

What I have tried:

main.cpp

#include <gl glew.h="">
#include <glfw glfw3.h="">
#include <gl glu.h="">
#include <gl glu.h="">
#include <gl freeglut.h="">
#define GLEW_STATIC
#include <gl gl.h="">
#include <iostream>
#include <math.h>

#include "vec2.h"
#include "window.h"
#define __gl_h_

#define GLFW_DLL


int main()
{




vec2 a(1.0f,2.0f)
vec2 b(5 ,4)
a -= b;


}
#include "vec2.cpp"
namespace sparky {namespace maths {


vec2& vec2::operator+=(const vec2& other)
{
    return add(other);

}
vec2& vec2::operator-=(const vec2& other)
{
    return subtract(other);
}
vec2& vec2::operator*=(const vec2& other)
{
    return multiply(other);
}
vec2& vec2::operator/=(const vec2& other)
{
    return divide(other);
}
    }
}
Posted
Updated 21-Jul-21 20:10pm
v3

1 solution

That should actually work.

A working example
C++
#include <iostream>
#include <vector>
using namespace std;

// this should be inside the header file
namespace sparky
{
  namespace maths
  {
    class vec2
    {
      vector <int> v;

      vec2 & add( const vec2 & other)
      {
        for (size_t n=0; n<v.size() && n<other.v.size(); ++n)
        {
          v[n] += other.v[n];
        }
        return *this;
      }

    public:
      vec2 & operator += ( const vec2 & other );
      void push_back(const int i){ v.push_back(i); }

      friend ostream & operator << ( ostream & os, const vec2 & v2);

    };
  }
}

// this should be inside the source file
namespace sparky
{
  namespace maths
  {

    ostream & operator << (ostream & os, const vec2 & v2)
    {
      for (const auto &  x : v2.v)
        os << x << " ";
      return os;
    }
    vec2& vec2::operator+=(const vec2& other)
    {
      return add(other);
    }
  }
}


// testing the class
int main()
{
  sparky::maths::vec2 v,w;
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  w.push_back(10);
  w.push_back(20);

  cout << "v " << v << ", w " << w  << "\n";
  v += w;
  cout << "v " << v << ", w " << w << "\n";
}
 
Share this answer
 
Comments
jason lyman 22-Jul-21 14:02pm    
i still get vec2& vec2::operator+=(const vec2& other)
{
return add(other)
}
has no declaration matches
and get a new error v has no member name 'v'
CPallini 22-Jul-21 14:47pm    
Did you try to compile my code 'as is'?
See it online: https://onlinegdb.com/2zcitpV1F
jason lyman 22-Jul-21 15:07pm    
yes but codeblocks doesn't seem work still but it will work with link you still gave me
jason lyman 22-Jul-21 15:14pm    
nevermind it worked some when created a new class in code blocks
jason lyman 22-Jul-21 15:15pm    
don't why the hell its acting weird strange

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900