subtract 2 Point3d values result into a Point3d inside a function

Hamstrung by this difficulty, which should have been easy. The goal was to subtract 2 Point3d points(Q,R) with a third(P) as the result.


1
2
3
4
5
6
7
8
func()
{
Point3d P;
Point3d Q(2,9,66);
Point3d R(3,5,6);

P = Q - R
}

works fine as long as Point3d is declared in the function.

If I declare the Point3d type outside the function at the top of page and carry the type in function call it doesn't work and the subtraction symbol is underlined as the problem.
1
2
3
4
void find_planar_vecs(std::vector<cv::Point3d>P, std::vector<cv::Point3d>Q, std::vector<cv::Point3d>R, std::vector<cv::Point3d>&QP, std::vector<cv::Point3d>&RP) 
{
	P = Q - R;  //error here posted below
}


Didn't work.
error:Severity Code Description Project File Line Suppression State
Error C2678 binary '-': no operator found which takes a left-hand operand of type 'std::vector<cv::Point3d,std::allocator<_Ty>>' (or there is no acceptable conversion) RayPlaneApp c:\users\clang\desktop\working folder\rayplaneapp\rayplaneapp\rayplaneapp.cpp 176

How do I make P=Q-R in the function but declare it outside of it?
Last edited on
There isn't enough information here to help you. Your second excerpt doesn't make sense <edit: this no longer applies, OP has changed their post>, but your first excerpt is potentially valid.
Last edited on
Thanks for replying. Tell me what further info you need so you are able to help.
Well, you changed/added more information to your post after I wrote that. You are trying to subtract two vectors of cv::Point3d.
So you need some operator overload like:

1
2
3
4
std::vector<cv::Point3d> operator-(const std::vector<cv::Point3d>& lhs, const std::vector<cv::Point3d>& rhs)
{
    // ...
}


Or, for example, perhaps something like:

1
2
3
4
5
6
7
8
std::vector<cv::Point3d> operator-(std::vector<cv::Point3d> lhs, const std::vector<cv::Point3d>& rhs)
{
    for (size_t i = 0; i < rhs.size(); i++)
    {
        lhs[i] -= rhs[i];
    }
    return lhs;
}


Of course, this assumes the vectors are the same size. Otherwise you'll get out-of-bounds access.

Last edited on
1
2
3
4
5
6
7
8
9
std::vector<cv::Point3d> operator-(std::vector<cv::Point3d> lhs, const std::vector<cv::Point3d>& rhs)
{
	lhs[0] -= rhs[0];
	return lhs;
}

main()...
resultVector = operator-(lhs, rhs);
std::cout << "Result :" << resultVector;

The above code works. Thx. Now please tell me in my declarations defining resultVector as type Point3d works instead of resultVectror type std::vector<cv::Point3d>.
1
2
std::vector<cv::Point3d>resultVector; function doesn't work/compile error
Point3d resultVector; works fine  
Last edited on
Your operator- function as it stands right now is quite strange. I might be so bold as to claim it's an abuse of operator overloading. What is so special about the first elements of the vectors? (element [0])

For the latter part of your post... let's back up a bit. What are you trying to accomplish here? Because it feels like you're just randomly trying stuff until it works.

A vector<Point> is different than a Point. If you return a vector<Point>, you can't assign it to a Point, and vice-versa. These are two different things.

A vector<Point> may contain zero or more Points.

Let's say you really wanted to make such an operator overload. You would do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::vector;

struct Point {
    double x;
    double y;
};

Point operator+(const Point& p1, const Point& p2)
{
    return Point { p1.x + p2.x, p1.y + p2.y };   
}

Point& operator+=(Point& point, const Point& other)
{
    point.x += other.x;
    point.y += other.y;
    return point;
}

std::ostream& operator<<(std::ostream& os, const Point& point)
{
    return os << '(' << point.x << ", " << point.y << ')';   
}

// Assumes both v1 and v2 are the same size
// Potentially undefined behavior if they are not
vector<Point> operator+(vector<Point> v1, const vector<Point>& v2)
{
    for (size_t i = 0; i < v2.size(); i++)
    {
        v1[i] += v2[i];
    }
    
    return v1;
}

int main()
{
    vector<Point> v1 = { {3,  4}, {0, 1}, { 2,  3} };
    vector<Point> v2 = { {0, -1}, {1, 0}, {10, 10} };
    
    vector<Point> v3 = v1 + v2;
    for (const Point& point : v3)
    {
        std::cout << point << '\n';   
    }
}

(3, 3)
(1, 1)
(12, 13)
Last edited on

This looks a bit like matrix addition/subtraction.
Matrix can be point2d or point3d or more.
It'll work inside a func() or in the main() module.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68


#include<vector>
#include<iostream>
#include <iomanip>
using namespace std;

#define MATRIX 	vector<vector<double>>

MATRIX operator +(MATRIX m1,MATRIX m2)
{
	MATRIX ans=m1; // dimension the answer here
	for(int r=0;r<m1.size();r++)
	{
		for(int k=0;k<m2[0].size();k++)
		{
			ans[r][k]=m1[r][k]+m2[r][k];
		}	
	}
	return ans;			
}

MATRIX operator -(MATRIX m1,MATRIX m2)
{
	MATRIX ans=m1;  // dimension the answer here
	for(int r=0;r<m1.size();r++)
	{
		for(int k=0;k<m2[0].size();k++)
		{
			ans[r][k]=m1[r][k]-m2[r][k];
		}	
	}
	return ans;			
}

void print(MATRIX m1)
{
 for(int r=0;r<m1.size();r++)
	{
		for(int k=0;k<m1[0].size();k++)
		{
			cout<<setw(3)<<m1[r][k]<<"   ";	
		}
		cout<<endl;
	}
	cout<<endl;
}

int main()
{
	MATRIX v1 = { {3,  4}, {0, 1}, { 2,  3} };
    MATRIX v2 = { {0, -1}, {1, 0}, {10, 10} };
    MATRIX x=v1+v2;
    cout<<" v1 "<<endl;print(v1);
    cout<<" v2 "<<endl;print(v2);
    cout<<" v1+v2 "<<endl;print(x);
    cout<<endl<<endl;
 	 v1 = { {3,  4, -3}, {0, 1, 9}, { 2,  3, 4}, {5, 6, 7} };
     v2 = { {0, -1, -1}, {1, 0, 2}, {10, 10, 10}, {8, -3, 2} };
     x=v1-v2; 
     cout<<" v1 "<<endl;print(v1);
     cout<<" v2 "<<endl;print(v2);
     cout<<" v1-v2 "<<endl;print(x);
system("pause");	
    
    
}
 

Excuse the system("pause"), just for brevity here.
(I haven't done any checks for dimension matching)
oggin, note that instead of #define, you can use 'using' or 'typedef'.
using Matrix = vector<vector<double>>;

Last edited on
Topic archived. No new replies allowed.