I created a quick method in my program to compute the distance between two points using the distance formula, here’s the code:
#include <iostream>
#include <cmath>
using namespace std;
int distanceFormula(int x1, int y1, int x2, int y2) {
double d = sqrt((x1-x2)^2(y1-y2)^2);
return d;
}
it gives me a compiler error on the line where I declare the d variable saying that
error: expression cannot be used as a function.
What does this mean? And what am I doing wrong?
![]()
Gilfoyle
3,2102 gold badges45 silver badges78 bronze badges
asked Feb 7, 2014 at 1:24
0
Be careful, (x1-x2)^2 will not do an exponent of 2 here.
See http://www.cplusplus.com/reference/cmath/pow/.
Second, you probably forgot a + in your expression:
int distanceFormula(int x1, int y1, int x2, int y2) {
double d = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
return d;
}
answered Feb 7, 2014 at 1:27
![]()
Mr_PouetMr_Pouet
3,9328 gold badges36 silver badges47 bronze badges
5
The compiler error is because 2(y1-y2) is invalid syntax.
In this case 2 (or perhaps (x1-x2)^2) is the «expression» and (y1-y2) is taken as a function call argument list; this grammar production is simply not allowed.
Compare the following form where a binary operator (*) is introduced, which in turn makes the parser treat the subsequent (y1-y2) as an expression (bounded by grouping parenthesis) and not a function call. While it won’t do what is desired, as ^ is not exponentiation and the resulting equation is nonsense, it should parse and compile.
sqrt((x1-x2)^2*(y1-y2)^2)
answered Feb 7, 2014 at 1:33
user2864740user2864740
58.8k14 gold badges137 silver badges211 bronze badges
- Forum
- Beginners
- expression cannot be used as a function
expression cannot be used as a function
Hello,
I am exploring Mersenne Twister implementation to use it as a wrapped class that can be reused as a dll for other implementations like C#. I was trying this class and cannot figure out what is wrong. The code listed below and also available @ cpp.sh/4hte. I will appreciate any guidance help with fixing this class.
It gives me error
In member function ‘double Random::GenerateNext()’: 19:19: error: expression cannot be used as a function 20:2: warning: control reaches end of non-void function [-Wreturn-type]
|
|
Well, you have ponters, not objects, remember. You need to dereference pointers first.
Why do you use pointer semantic at all?
|
|
Last edited on
Topic archived. No new replies allowed.
hi,
by running the command: catkin_make -j1 —pkg rtabmap_ros , i got the following error. what is the solution???
Scanning dependencies of target rtabmap_ros
[ 57%] Building CXX object rtabmap_ros/CMakeFiles/rtabmap_ros.dir/src/MsgConversion.cpp.o
/home/masoumeh/catkin_ws4/src/rtabmap_ros/src/MsgConversion.cpp: In function ‘bool rtabmap_ros::convertScanMsg(const LaserScan&, const string&
, const string&, const ros::Time&, rtabmap::LaserScan&, tf::TransformListener&, double, bool)’:
/home/masoumeh/catkin_ws4/src/rtabmap_ros/src/MsgConversion.cpp:2105:81: error: expression cannot be used as a function
data = rtabmap::util3d::laserScan2dFromPointCloud(*pclScan, laserToOdom).data(); // put back in laser frame
^
/home/masoumeh/catkin_ws4/src/rtabmap_ros/src/MsgConversion.cpp:2113:81: error: expression cannot be used as a function
data = rtabmap::util3d::laserScan2dFromPointCloud(*pclScan, laserToOdom).data(); // put back in laser frame
^
/home/masoumeh/catkin_ws4/src/rtabmap_ros/src/MsgConversion.cpp: In function ‘bool rtabmap_ros::convertScan3dMsg(const PointCloud2&, const str
ing&, const string&, const ros::Time&, rtabmap::LaserScan&, tf::TransformListener&, double, int, float)’:
/home/masoumeh/catkin_ws4/src/rtabmap_ros/src/MsgConversion.cpp:2220:121: error: no matching function for call to ‘rtabmap::LaserScan::LaserSc
an(cv::Mat, int&, float&, rtabmap::Transform&)’
scan = rtabmap::LaserScan(rtabmap::util3d::laserScanFromPointCloud(*pclScan), maxPoints, maxRange, scanLocalTransform);
^
In file included from /usr/local/lib/rtabmap-0.20/../../include/rtabmap-0.20/rtabmap/core/SensorData.h:38:0,
from /usr/local/lib/rtabmap-0.20/../../include/rtabmap-0.20/rtabmap/core/Signature.h:42,
from /home/masoumeh/catkin_ws4/src/rtabmap_ros/include/rtabmap_ros/MsgConversion.h:47,
from /home/masoumeh/catkin_ws4/src/rtabmap_ros/src/MsgConversion.cpp:28:
/usr/local/lib/rtabmap-0.20/../../include/rtabmap-0.20/rtabmap/core/LaserScan.h:79:2: note: candidate: rtabmap::LaserScan::LaserScan(const cv:
:Mat&, rtabmap::LaserScan::Format, float, float, float, float, float, const rtabmap::Transform&)
LaserScan(const cv::Mat & data,
^
/usr/local/lib/rtabmap-0.20/../../include/rtabmap-0.20/rtabmap/core/LaserScan.h:79:2: note: candidate expects 8 arguments, 4 provided
/usr/local/lib/rtabmap-0.20/../../include/rtabmap-0.20/rtabmap/core/LaserScan.h:74:2: note: candidate: rtabmap::LaserScan::LaserScan(const cv:
:Mat&, int, float, rtabmap::LaserScan::Format, const rtabmap::Transform&)
LaserScan(const cv::Mat & data,
^
/usr/local/lib/rtabmap-0.20/../../include/rtabmap-0.20/rtabmap/core/LaserScan.h:74:2: note: no known conversion for argument 4 from ‘rtabmap