Class Function Error

Hello,

I am devising a program and at the "rhs.peek(0)" part of the code located in mathvector.cpp, Visual Studio says that there is an error, "class 'mathvector' has no member 'peek.'" My first two code snippets are given by the instructor, and the functions for mathvector.h are also given. I'm just composing the code for these functions and encountered this error as I try to find a way to compute the dot and cross product. Can anyone explain why this is an error? I am including "mathvector.h" which itself includes "point.h" so I believe that a vector should be able to access the "peek" function, which returns a value of x or y.

The following are the files:

point.h,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef POINT_H
#define POINT_H

class point
{
	private:

		double x; // represents the x-coordinate in an ordered pair (x, y).
		double y; // represents the y-coordinate in an ordered pair (x, y).

	public:

		point(); // default constructor. 
		point(double xvalue, double yvalue); // constructor that intializes a parameter of two doubles.
		double peek(int index) const; // member function that returns a coordinate.
		void poke(int index, double value); // member function that sets the value of a coordinate.
};

#endif 


point.cpp

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
#include <iostream>

#include "point.h"

using namespace std; 

// initializes at the origin with point (0,0).
point::point()
{
	x = 0.0;
	y = 0.0;
}

// initializes as a point with xvalue and yvalue.
// xvalue: x-coordinate.
// yvalue: y-coordinate.
point::point(double xvalue, double yvalue)
{
	x = xvalue;
	y = yvalue;
}

// if the index is 0, the point's x-coordinate is returned.
// if the index is 1, the point's y-coordinate is returned.
double point::peek(int index) const
{
	if (index == 0)
		return x;
	else if (index == 1)
		return y;
	else
		return 0;
}

// if the index is 0, set the point's x-coordinate equal to the value.
// if the index is 1, set the point's y-coordinate equal to the value.
// value: some number or variable with a number value.
void point::poke(int index, double value)
{
	if (index == 0)
		x = value;
	else if (index == 1)
		y = value;
}


mathvector.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef MATHVECTOR_H
#define MATHVECTOR_H

#include "point.h"

class mathvector
{
	private:

		point head;

	public:

		mathvector(); // default constructor that sets the vector to the origin
		mathvector(point thetail, point thehead); // constructor that sets the vector to the distance between two points
		double operator,(mathvector) const; // member function that finds the dot product between two vectors
		double operator*(mathvector) const; // member function that finds the cross product between two vectors
};

#endif 


mathvector.cpp

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
#include <iostream>
#include <cmath>

#include "mathvector.h"

using namespace std;

mathvector::mathvector()
{
	head.poke(0, 0.0);
	head.poke(1, 0.0);
}

mathvector::mathvector(point thetail, point thehead)
{
	head = thehead - thetail;
}

// rhs is a vector
// peek(0) returns the x, while peek(1) returns the y
// the dot product is the first vector's x multiply by the second vector's x,
// added up with the second vector's y multiplied by the second's vector's y.
double mathvector::operator,(mathvector rhs) const
{
	double dot = 0.0;

	dot = (head.peek(0)*rhs.peek(0)) + (head.peek(1)*rhs.peek(1));

	return dot;
}

// rhs is a vector
// the cross product is the the first vector's x multiply by the second vector's y,
// subtracted by the second vector's x multiplied by the first's vector's y.
double mathvector::operator*(mathvector rhs) const
{
	double cross = 0.0;

	cross = (head.peek(0)*rhs.peek(1)) - (rhs.peek(0)*head.peek(1));

	return cross;
}
I guess I should clarify a bit.

1
2
3
4
5
6
7
8
double mathvector::operator,(mathvector rhs) const
{
	double dot = 0.0;

	dot = (head.peek(0)*rhs.peek(0)) + (head.peek(1)*rhs.peek(1));

	return dot;
}


This is basically the snippet I am having an issue with. Given a vector (x1, y1) and another vector rhs, (x2, y2), I want to find the dot product between them, which is found by the equation: x1*x2 + y1*y2. Head.peek(0) has no error, but rhs.peek(0) has the error: "class 'mathvector' has no member 'peek.'" It says this, but in the mathvector header, I included the point class, so my mathvector.cpp should have access to the "peek" function, but for some reason it appears not to.

Edit: For this member function, given two mathvectors, mv1 and mv2, then if I write (mv1, mv2) in my main, then that should return the dot product of the vectors mv1 and mv2.
Last edited on
closed account (D80DSL3A)
I think you want rhs.head.peek(0) not rhs.peek(0).
Topic archived. No new replies allowed.