Passing a struct as a parameter - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Passing a struct as a parameter

Posts: 715Member
[b][red]This message was edited by Donotalo at 2006-9-4 2:48:51[/red][/b][hr]
[purple][code]
struct Point {
double x, y;
};

inline double distance(const Point& p1, const Point& p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}

double closest_pair(Point point[10000], int start, int end) {
...
if (start + 1 == end)
[red] return distance(point[start], point[end]);[/red] //line 22
...
}
[/code]
the red line above causes problem. dev c++ gives the following messages:
[green]
/Softworks/Dev C++ 5 Beta 9/include/c++/3.3.1/bits/stl_iterator_base_types.h D:ProgramsUVa10245 The Closest Pair ProblemD In instantiation of `std::iterator_traits':

22 D:ProgramsUVa10245 The Closest Pair Problem10245 The Closest Pair Problem.cpp instantiated from here

126 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `iterator_category' in `struct Point'

127 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `value_type' in `struct Point'

128 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `difference_type' in `struct Point'

129 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `pointer' in `struct Point'

130 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `reference' in `struct Point'[/green]

how to fix this errors? i've no idea. :-(
[/purple]
[hr][purple]~Donotalo()[/purple]

• Posts: 3,711Member
: [b][red]This message was edited by Donotalo at 2006-9-4 2:48:51[/red][/b][hr]
: [purple][code]
: struct Point {
: double x, y;
: };
:
: inline double distance(const Point& p1, const Point& p2) {
: return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
: }
:
: double closest_pair(Point point[10000], int start, int end) {
: ...
: if (start + 1 == end)
: [red] return distance(point[start], point[end]);[/red] //line 22
: ...
: }
: [/code]
: the red line above causes problem. dev c++ gives the following messages:
: [green]
: /Softworks/Dev C++ 5 Beta 9/include/c++/3.3.1/bits/stl_iterator_base_types.h D:ProgramsUVa10245 The Closest Pair ProblemD In instantiation of `std::iterator_traits':
:
: 22 D:ProgramsUVa10245 The Closest Pair Problem10245 The Closest Pair Problem.cpp instantiated from here
:
: 126 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `iterator_category' in `struct Point'
:
: 127 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `value_type' in `struct Point'
:
: 128 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `difference_type' in `struct Point'
:
: 129 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `pointer' in `struct Point'
:
: 130 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `reference' in `struct Point'[/green]
:
: how to fix this errors? i've no idea. :-(
: [/purple]
: [hr][purple]~Donotalo()[/purple]
:
:
:

It compiles fine in Dev C++. Seems the problem is elsewhere. Maybe there is another class called "Point" somewhere?
• Posts: 754Member
: [b][red]This message was edited by Donotalo at 2006-9-4 2:48:51[/red][/b][hr]
: [purple][code]
: struct Point {
: double x, y;
: };
:
: inline double distance(const Point& p1, const Point& p2) {
: return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
: }
:
: double closest_pair(Point point[10000], int start, int end) {
: ...
: if (start + 1 == end)
: [red] return distance(point[start], point[end]);[/red] //line 22
: ...
: }
: [/code]
: the red line above causes problem. dev c++ gives the following messages:
: [green]
: /Softworks/Dev C++ 5 Beta 9/include/c++/3.3.1/bits/stl_iterator_base_types.h D:ProgramsUVa10245 The Closest Pair ProblemD In instantiation of `std::iterator_traits':
:
: 22 D:ProgramsUVa10245 The Closest Pair Problem10245 The Closest Pair Problem.cpp instantiated from here
:
: 126 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `iterator_category' in `struct Point'
:
: 127 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `value_type' in `struct Point'
:
: 128 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `difference_type' in `struct Point'
:
: 129 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `pointer' in `struct Point'
:
: 130 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `reference' in `struct Point'[/green]
:
: how to fix this errors? i've no idea. :-(
: [/purple]
: [hr][purple]~Donotalo()[/purple]
:

The third error gives a nice indication:
[code]
126 D:SoftworksDev C++ 5 Beta 9includec++3.3.1itsstl_iterator_base_types.h no type named `iterator_category' in `struct Point'
[/code]

The function distance (i.e. std::distance) is an STL function taking two iterators as an argument! It gives the distance between two iterators. You probably wrote 'using namespace std' somewhere. You could either not use namespace std, rename your distance function or put your distance function in another namespace.

See ya,
bilderbikkel