What is wrong with this function?

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
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <sstream>

#include <vector>
#include <string>

#include <iomanip>
#include <cstdlib>
#include <cmath>

using namespace std;


typedef double numeric_t;
typedef   long   index_t;

class point_t {
	numeric_t x, y;
public:
	point_t() {x=0, y=0;}
	point_t(const numeric_t x1, const numeric_t x2) {x=x1; y=x2;}

	inline void setX(const numeric_t val) {x=val;}
	inline void setY(const numeric_t val) {y=val;}

	inline numeric_t X() {return x;}
	inline numeric_t Y() {return y;}

	inline point_t offset(const numeric_t dx, const numeric_t dy) {return point_t(x+dx, y+dy);}

	inline bool operator == (const point_t & otherP) {return (x==otherP.x) && (y==otherP.y);}
	inline bool operator != (const point_t & otherP) {return (x!=otherP.x) || (y!=otherP.y);}

	friend ostream & operator << (ostream & stm, const point_t & pt) {return stm <<"("<<pt.x<<", "<<pt.y<<")";}

	friend istream & operator >> (istream & stm, point_t & pt);

};


istream & operator >> (istream & stm, point_t & pt) {
    numeric_t xx, yy;
    string a, b, c;
    if(stm >> a >> xx >> b >> yy >> c && a == "(" && b == "," && c == ")" )
    {
        pt=point_t(xx, yy) ;
    }

    return stm ;
}


inline numeric_t distance(point_t & p1, point_t & p2) {return sqrt((p1.X()-p2.X())*(p1.X()-p2.X())+(p1.Y()-p2.Y())*(p1.Y()-p2.Y()));}//this is the one with problems, 


int main() {
	cout<<"Enter two points:"<<endl;
			point_t p1, p2, p3;
	cin>>p1>>p2;
	cout<<p1.X()<<p1.Y()<<p2.X()<<p2.Y()<<endl;
	cout<<p1<<endl<<p2<<endl;
	p3=p2.offset(1,-7);
	cout<<p3;
	bool eee;
	eee=(p1==p3);
	cout<<eee<<endl;
	numeric_t thevalue, v1, v2;
	cin>>v1>>v2;
	thevalue=v1+v2;
//	thevalue=distance(p1, p2); /* if I use this function, then there will be compiling problems like: point_t has no member of difference_type, something like that, I don't understand*/
	cout<<thevalue<<endl;


	return 0;
}


Please give me some clue, thanks!
Which function? There are two functions and one operator defined in the code you've posted.

EDIT: And why do you think there's something wrong? Are you getting an error message? Are you seeing behaviour which you believe to be incorrect?

The more information you withhold from us, the less able we are to help you.
Last edited on
http://www.cplusplus.com/reference/iterator/distance/

By changing your distance function to distance_t, I can get it to compile. We should also note that this would serve as an excellent example of why using namespace std; is bad.

Hope that helps.
@Toledo, the purpose of a namespace is to prevent that kind of overlaps and you are right that overriding them can cause subtle bugs. I would never say though that overriding a namespace was a bad idea. If you know what you are doing it can save you a lot of writing; talking about well known namespaces such as std repeating its name does not even bring readability supposing you are not using other streams all mixed up. So as my opinion, overriding a namespace is not generally bad, sometimes totally worthwhile, it is just one of those things you can easily go wrong with if you are not experienced.
Last edited on
Thanks! So, you are suggesting me to use namespace, and then every function used will need to be written as

1
2
namespace1::foo();
namespace2::foo();


?So I need to change my programming habit? and then my code will be much longer than right now.
Last edited on
Toledo did not suggest you would add every function of yours to a namespace but more of that you would not override the std namespace, so you would use std::cout and std::cin instead of the bare function names. This would have prevented your mistake of using distance which referred to std::distance.
Last edited on
OK, Thanks!!!
Personally, I'd say that putting your code into its own namespace is a good habit to get into. It'll help when you start working on larger projects.
DO I need to put everything in the namespace?

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
namespace mycode
{
class point_t {
	numeric_t x, y;
public:
	point_t() {x=0, y=0;}
	point_t(const numeric_t x1, const numeric_t x2) {x=x1; y=x2;}

	inline void setX(const numeric_t val) {x=val;}
	inline void setY(const numeric_t val) {y=val;}

	inline numeric_t X() {return x;}
	inline numeric_t Y() {return y;}

	inline point_t offset(const numeric_t dx, const numeric_t dy) {return point_t(x+dx, y+dy);}

	inline bool operator == (const point_t & otherP) {return (x==otherP.x) && (y==otherP.y);}
	inline bool operator != (const point_t & otherP) {return (x!=otherP.x) || (y!=otherP.y);}

	friend ostream & operator << (ostream & stm, const point_t & pt) {return stm <<"("<<pt.x<<", "<<pt.y<<")";}

	friend istream & operator >> (istream & stm, point_t & pt);

};


istream & operator >> (istream & stm, point_t & pt) {
    numeric_t xx, yy;
    string a, b, c;
    if(stm >> a >> xx >> b >> yy >> c && a == "(" && b == "," && c == ")" )
    {
        pt=point_t(xx, yy) ;
    }

    return stm ;
}


inline numeric_t distance(point_t & p1, point_t & p2) {return sqrt((p1.X()-p2.X())*(p1.X()-p2.X())+(p1.Y()-p2.Y())*(p1.Y()-p2.Y()));}


If I got several files and I want to put all in the same namespace, can I just repeat doing this for all files? Thanks.
}
Last edited on
Yes, except you'll need to close the namespace with a closing-brace too.

You can use the same namespace for as many definitions as you like, across as many files as you like.

As you start to become more comfortable designing bigger systems, you might decide to start splitting it up between several namespaces. One common way that larger projects are organised is to split it up between different libraries, and put the code for each library in its own directory and in its own namespace, using the library name for the directory and the namespace.
Thanks!
You're welcome :)
Topic archived. No new replies allowed.