int is not a class,struct or union type

May 29, 2013 at 3:08pm
hi everyone. i got a bit problem with executing this following codes..when i compile it come out an error say that "int is not a class,struct or union type",can u all help me to solve it?

#include <iostream>
#include <cmath>
using namespace std;
int distance(int,int);
int main()
{
int a=3;
int b=4;
int ret;
ret=distance(a,b);
cout<<ret;
system("pause");
return 0;
}
void distance(int x,int y)
{
int dist=sqrt(x*x+y*y);
return dist;
return 0;
}
May 29, 2013 at 3:17pm
closed account (zb0S216C)
What line does the error occur on?

The prototype:

 
int distance(int,int);

...does not correspond to the definition:

 
void distance(int x,int y)

In fact, "distance( )" is a function defined within the "std" name-space. And, by "using namespace std", you will cause a conflict between "std::distance( )" and "::distance( )". I advise that you change the name of your "distance" function.

Also, in your "distance( )" function, the last "return" statement will never be executed because another "return" shadows it.

Wazzak
May 29, 2013 at 3:18pm
a) Post line where eror occured
b) There is numerous problems in your code:
1) distance() defined as void, but you are trying to return value from it
2) You are trying to return twice. Why?
3) You have probably run into naming collision with std::distance function

c) there is std::hypot() in <cmath> which does exactly what your function does
Last edited on May 29, 2013 at 3:21pm
May 29, 2013 at 3:42pm
sorry because i edit it many times so it got two return and some careless mistake.. oh.. i get it.. thanks..so can i know more about what is the function of distance()?
May 29, 2013 at 4:09pm
Basically it is how many elements is between two iterators.
http://en.cppreference.com/w/cpp/iterator/distance
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
    auto distance = std::distance(v.begin(), v.end());
    std::cout << distance << '\n';
}
3
Last edited on May 29, 2013 at 4:09pm
May 29, 2013 at 4:26pm
1) Conflicting function definition and declaration.
2) Don't return zero from the function.
Topic archived. No new replies allowed.