Меню

Expression cannot be used as a function ошибка

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's user avatar

Gilfoyle

3,2102 gold badges45 silver badges78 bronze badges

asked Feb 7, 2014 at 1:24

LoreleiRS's user avatar

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_Pouet's user avatar

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

user2864740's user avatar

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]

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
#include <random> 
#include <iostream>
#include <iomanip>
#include <string>
#include <map>


class Random {
    std::mt19937* gen;
    std::uniform_real_distribution<>* distr;
  public:
	Random (int seed, double min, double max)
	{
		gen = new std::mt19937(seed);
		distr = new std::uniform_real_distribution<>(min,max);
	};
    double GenerateNext() 
	{
		return distr(gen);
	};
};

int main()
{
    double a = 1.0;
    double b = 2147483647.0;
	int seed = 999999999;
    Random x(seed, a, b);
	std::cout << std::fixed << std::setprecision(10) << x.GenerateNext() << std::endl;
	std::cout << std::fixed << std::setprecision(10) << x.GenerateNext() << std::endl;
	std::cout << std::fixed << std::setprecision(10) << x.GenerateNext() << std::endl;
}

Well, you have ponters, not objects, remember. You need to dereference pointers first.

Why do you use pointer semantic at all?

1
2
3
4
5
6
7
8
9
10
11
class Random 
{
    std::mt19937 gen;
    std::uniform_real_distribution<> distr;
  public:
    Random (int seed, double min, double max) : gen(seed), distr(min, max) {}
    double GenerateNext() 
    {
        return distr(gen);
    }
};

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

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Explorer exe произошла ошибка
  • Explorer exe ошибка что значит