### Help! I am almost there but stuck with some problem.

I am writing a code which gets three coordinates for a triangle from a user and calculates its perimeter.
I am pretty sure everything works but calling the function 'distance' in my main function is wrong. Tried my best to fix it, but it is no use. Any idea how to fix it?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include #include using namespace std; struct Point { int x; int y; }; struct Triangle { Point point1; Point point2; Point point3; }; double distance (Point firstPoint, Point secondPoint) { double a = 0, b = 0; double distance = 0; a = firstPoint.x + secondPoint.x; b = firstPoint.y + secondPoint.y; distance = sqrt((a * a) + (b * b)); return distance; } int main() { const int NUMTRIANGLE = 3; Triangle triangles[NUMTRIANGLE]; for (int a = 0; a < NUMTRIANGLE; a++) { double perimeter = 0; cout << "Enter the first position x of triangle " << a + 1 << ": "; cin >> triangles[a].point1.x; cout << "Enter the first position y of triangle " << a + 1 << ": "; cin >> triangles[a].point1.y; cout << "Enter the second position x of triangle " << a + 1 << ": "; cin >> triangles[a].point2.x; cout << "Enter the second position y of triangle " << a + 1 << ": "; cin >> triangles[a].point2.y; cout << "Enter the third position x of triangle " << a + 1 << ": "; cin >> triangles[a].point3.x; cout << "Enter the thrid position y of triangle " << a + 1 << ": "; cin >> triangles[a].point3.y; perimeter += distance (triangles[a].point1, triangles[a].point2); perimeter += distance (triangles[a].point1, triangles[a].point3); perimeter += distance (triangles[a].point2, triangles[a].point3); cout << fixed << showpoint << setprecision(2); cout << "The perimeter of triangle " << a + 1 << " is: " << perimeter << endl; } } ``````
Last edited on
 calling the function 'distance' in my main function is wrong

Please define "wrong". What is the error? (Compiler, linker, runtime, or unexpected output.)

Lets assume the last. I have two points on the x-axis P1 (1,0) and P2 (3,0). Intuitively, the distance is 2.
Lets compute: a = 1 + 3; b = 0; distance = sqrt( a * a ) = 4

Lines 24 and 25. The `+`

When I tried to compile without
 perimeter += distance (triangles[a].point1, triangles[a].point2);
, it runs perfectly.

As I tried to compile with it, it shows compile error.
Here is the error message that I got.

1>------ Build started: Project: Assignment 5 part 3_1, Configuration: Debug Win32 ------
1>Build started 2014-07-14 오전 1:51:31.
1>InitializeBuildStatus:
1> Touching "Debug\Assignment 5 part 3_1.unsuccessfulbuild".
1>ClCompile:
1> problem3.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(373): error C2039: 'iterator_category' : is not a member of 'Point'
1> c:\users\jin\documents\visual studio 2010\projects\assignment 5 part 3_1\assignment 5 part 3_1\problem3.cpp(9) : see declaration of 'Point'
1> c:\users\jin\documents\visual studio 2010\projects\assignment 5 part 3_1\assignment 5 part 3_1\problem3.cpp(58) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(373): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(373): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(373): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(373) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(373): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(374): error C2039: 'value_type' : is not a member of 'Point'
1> c:\users\jin\documents\visual studio 2010\projects\assignment 5 part 3_1\assignment 5 part 3_1\problem3.cpp(9) : see declaration of 'Point'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(374): error C2146: syntax error : missing ';' before identifier 'value_type'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(374): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(374): error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(374) : see declaration of 'std::iterator_traits<_Iter>::value_type'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(374): error C2868: 'std::iterator_traits<_Iter>::value_type' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(375): error C2039: 'difference_type' : is not a member of 'Point'
1> c:\users\jin\documents\visual studio 2010\projects\assignment 5 part 3_1\assignment 5 part 3_1\problem3.cpp(9) : see declaration of 'Point'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(375): error C2146: syntax error : missing ';' before identifier 'difference_type'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(375): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(375): error C2602: 'std::iterator_traits<_Iter>::difference_type' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(375) : see declaration of 'std::iterator_traits<_Iter>::difference_type'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(375): error C2868: 'std::iterator_traits<_Iter>::difference_type' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(377): error C2039: 'pointer' : is not a member of 'Point'
1> c:\users\jin\documents\visual studio 2010\projects\assignment 5 part 3_1\assignment 5 part 3_1\problem3.cpp(9) : see declaration of 'Point'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(377): error C2146: syntax error : missing ';' before identifier 'pointer'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(377): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(377): error C2602: 'std::iterator_traits<_Iter>::pointer' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(377) : see declaration of 'std::iterator_traits<_Iter>::pointer'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(377): error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(378): error C2039: 'reference' : is not a member of 'Point'
1> c:\users\jin\documents\visual studio 2010\projects\assignment 5 part 3_1\assignment 5 part 3_1\problem3.cpp(9) : see declaration of 'Point'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(378): error C2146: syntax error : missing ';' before identifier 'reference'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(378): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(378): error C2602: 'std::iterator_traits<_Iter>::reference' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=Point
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(378) : see declaration of 'std::iterator_traits<_Iter>::reference'
1> with
1> [
1> _Iter=Point
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(378): error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=Point
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.78
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
`using namespace std;`
There is a good reason why that is not recommended.

One of the included headers makes `std::distance` visible.
Your using makes it visible as `distance`.

Now you have two functions with name `distance` in global scope and the overload resolution goes haywire.

Replace the using on line 4 with:
 ``12`` ``````using std::cin; using::cout;``````

and add the `std::` to remaining fixed, endl, etc.
Wow. Thanks, now I got it.
So, I see why
 using namespace std;
is not recommended, but
my textbook uses this line in every program. Is it because I am still at a starting level?
Topic archived. No new replies allowed.