function-definition error when passing struct into function
Version - ROS melodic System - Ubuntu bionic 18.04
Hi, this may be more of a C++ error than a ROS error but it occurs when I'm running ros_make. The error began when I tried to alter a function to take a struct as one of its inputs - it is listed below:
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp: In function ‘void setPelletColour(Pellet)’:
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp:38:87: error: a function-definition is not allowed here before ‘{’ token
void seenBool(enki_ros_pck::Sight& seen, sensor_msgs::Image msg, struct Pellet){
^
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp:102:1: error: a function-definition is not allowed here before ‘{’ token
{
^
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp:116:1: error: a function-definition is not allowed here before ‘{’ token
{
^
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp:125:1: error: a function-definition is not allowed here before ‘{’ token
{
^
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp:187:1: error: a function-definition is not allowed here before ‘{’ token
{
^
/home/catkin_ws/src/enki_ros_pck/ReversalSignals.cpp:196:1: error: expected ‘}’ at end of input
}
^
enki_ros_pck/CMakeFiles/robot.dir/build.make:134: recipe for target 'enki_ros_pck/CMakeFiles/robot.dir/ReversalSignals.cpp.o' failed
make[2]: *** [enki_ros_pck/CMakeFiles/robot.dir/ReversalSignals.cpp.o] Error 1
Below is the contents of Reversal.h followed by Reversal.cpp where the struct and function are defined - I suspect its something to do with the way I have defined it thats causing the issues.
**ReversalSignals.h**
#ifndef __REVERSAL_SIGNALS_H
#define __REVERSAL_SIGNALS_H
#include "ros/ros.h"
#include "std_msgs/Bool.h"
#include <ros/package.h>
#include "enki_ros_pck/Sight.h"
#include "sensor_msgs/Image.h"
#include <Enki.h>
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <iomanip>
#include "Racer.h"
#include "Geometry.h"
#include "bandpass.h"
#include "parameters.h"
#include <chrono>
#include "PhysicalEngine.h"
enum Colours {BLANK, RED, GREEN, BLUE, PURPLE};
struct Pellet
{
Enki::PhysicalObject* name;
int colour;
double x_coord;
double y_coord;
};
Pellet Pellet;
void setPelletColour(struct Pellet);
void seenBool(enki_ros_pck::Sight& seen, sensor_msgs::Image msg, struct Pellet);
void rewardBool(Enki::Racer* racer, struct Pellet, Enki::PhysicalObject* pellet, std_msgs::Bool& reward, double maxx, double maxy);
void getDistance(Enki::Racer* racer, struct Pellet, Enki::PhysicalObject* pellet, enki_ros_pck::Sight& sight);
void placeBool(std_msgs::Bool& placeBool, Enki::Racer* racer, double circleCentreX, double circleCentreY, double circleRad ,double maxX, double maxY);
#endif
ReversalSignals.cpp
#include "ReversalSignals.h"
using namespace Enki;
using namespace std;
using namespace std::chrono;
void setPelletColour(struct Pellet){
int pellet_colour = Pellet.colour;
switch (pellet_colour){
case RED:
Pellet.name->setColor(Enki::Color(1,0,0));
pellet_colour = RED;
break;
case GREEN:
Pellet.name->setColor(Enki::Color(0,1,0));
pellet_colour = GREEN;
break;
case BLUE:
Pellet.name->setColor(Enki::Color(0,0,1));
pellet_colour = BLUE;
break;
case PURPLE:
Pellet.name->setColor(Enki::Color(1,0,1));
pellet_colour = PURPLE;
break;
default:
Pellet.name->setColor(Enki::Color(0,0,0));
pellet_colour = BLANK;
std::cout << "ERROR setPelletColour: please set pellet colout to RED, GREEN, BLUE, or PURPLE." << std::endl;
break;
}
void seenBool(enki_ros_pck::Sight& seen, sensor_msgs::Image msg, struct Pellet){
static int vision[9];
int pellet_colour = Pellet.colour;
switch(pellet_colour){
case RED:
for (int i=0; i<7; i++)
{
vision[i] = msg.data[((80)+(i*16))]; //creates array of all R values from RGB vision-array (reward is pure red so == (255,0,0) uintRGB - R value every 4 but that makes it lag so 16
}
if( vision[0]==255 || vision[1]==255 || vision[2]==255 || vision[3]==255 || vision[4]==255 || vision[5]==255 || vision[6]==255 || vision[7]==255 || vision[8]==255 || vision[9]==255 || vision[10]==255 || vision[11]==255 || vision[12]==255 || vision[13]==255 || vision[14]==255 || vision[15]==255 || vision[16]==255 || vision[17]==255 || vision[18]==255 || vision[19]==255 || vision[20]==255 || vision[21]==255 || vision[22]==255 || vision[23]==255 || vision[24]==255 || vision [25]==255 || vision[26]==255 || vision[27]==255)
{
seen.sight = true;
}
else
{
seen.sight = false;
}
break;
case GREEN:
for (int i=0; i<7; i++)
{
vision[i] = msg.data[((81)+(i*16))]; //creates array of all R values from RGB vision-array (reward is pure red so == (255,0,0) uintRGB - R value every 4 but that makes it lag so 16
}
if( vision[0]==255 || vision[1]==255 || vision[2]==255 || vision[3]==255 || vision[4]==255 || vision[5]==255 || vision[6]==255 || vision[7]==255 || vision[8]==255 || vision[9]==255 || vision[10]==255 || vision[11]==255 || vision[12]==255 || vision[13]==255 || vision[14]==255 || vision[15]==255 || vision[16]==255 || vision[17]==255 || vision[18]==255 || vision[19]==255 || vision[20]==255 || vision[21]==255 || vision[22]==255 || vision[23]==255 || vision[24]==255 || vision [25]==255 || vision[26]==255 || vision[27]==255)
{
seen.sight = true;
}
else
{
seen.sight = false;
}
break;
case BLUE:
for (int i=0; i<7; i++)
{
vision[i] = msg.data[((82)+(i*16))]; //creates array of all R values from RGB vision-array (reward is pure red so == (255,0,0) uintRGB - R value every 4 but that makes it lag so 16
}
if( vision[0]==255 || vision[1]==255 || vision[2]==255 || vision[3]==255 || vision[4]==255 || vision[5]==255 || vision[6]==255 || vision[7]==255 || vision[8]==255 || vision[9]==255 || vision[10]==255 || vision[11]==255 || vision[12]==255 || vision[13]==255 || vision[14]==255 || vision[15]==255 || vision[16]==255 || vision[17]==255 || vision[18]==255 || vision[19]==255 || vision[20]==255 || vision[21]==255 || vision[22]==255 || vision[23]==255 || vision[24]==255 || vision [25]==255 || vision[26]==255 || vision[27]==255)
{
seen.sight = true;
}
else
{
seen.sight = false;
}
break;
default:
std::cout << "seenBool ERROR: Please set char colour to either RED, GREEN, or BLUE." << std::endl;
seen.sight = false;
break;
}
}
void rewardBool(Enki::Racer* racer, struct Pellet, std_msgs::Bool& reward, double maxx, double maxy) // (racer->pos, pellet->pos)
{
if (sqrt((racer->pos.x - pellet->pos.x)*(racer->pos.x - pellet->pos.x)+(racer->pos.y - pellet->pos.y)*(racer->pos.y - pellet->pos.y))<13.0)
{ //robot half length + food radius = 10+2 = 12
reward.data = true;
racer->pos = Point(maxx/2, maxy/2 -30);
}
else
{
reward.data = false;
}
}
void getDistance(Enki::Racer* racer, struct Pellet, enki_ros_pck::Sight& sight)
{
float xdistance = sqrt((racer->pos.x - pellet->pos.x)*(racer->pos.x - pellet->pos.x));
float ydistance = sqrt((racer->pos.y - pellet->pos.y)*(racer->pos.y- pellet->pos.y));
float direct_distance = sqrt((xdistance*xdistance)+(ydistance*ydistance)) -13; //13 = halflength of robot
sight.distance = direct_distance;
}
void seenBool(enki_ros_pck::Sight& seen, sensor_msgs::Image msg, struct Pellet)
{
static int vision[9];
switch(Pellet.colour){
case RED:
for (int i=0; i<7; i++)
{
vision[i] = msg.data[((80)+(i*16))]; //creates array of all R values from RGB vision-array (reward is pure red so == (255,0,0) uintRGB - R value every 4 but that makes it lag so 16
}
if( vision[0]==255 || vision[1]==255 || vision[2]==255 || vision[3]==255 || vision[4]==255 || vision[5]==255 || vision[6]==255 || vision[7]==255 || vision[8]==255 || vision[9]==255 || vision[10]==255 || vision[11]==255 || vision[12]==255 || vision[13]==255 || vision[14]==255 || vision[15]==255 || vision[16]==255 || vision[17]==255 || vision[18]==255 || vision[19]==255 || vision[20]==255 || vision[21]==255 || vision[22]==255 || vision[23]==255 || vision[24]==255 || vision [25]==255 || vision[26]==255 || vision[27]==255)
{
seen.sight = true;
}
else
{
seen.sight = false;
}
break;
case GREEN:
for (int i=0; i<7; i++)
{
vision[i] = msg.data[((81)+(i*16))]; //creates array of all R values from RGB vision-array (reward is pure red so == (255,0,0) uintRGB - R value every 4 but that makes it lag so 16
}
if( vision[0]==255 || vision[1]==255 || vision[2]==255 || vision[3]==255 || vision[4]==255 || vision[5]==255 || vision[6]==255 || vision[7]==255 || vision[8]==255 || vision[9]==255 || vision[10]==255 || vision[11]==255 || vision[12]==255 || vision[13]==255 || vision[14]==255 || vision[15]==255 || vision[16]==255 || vision[17]==255 || vision[18]==255 || vision[19]==255 || vision[20]==255 || vision[21]==255 || vision[22]==255 || vision[23]==255 || vision[24]==255 || vision [25]==255 || vision[26]==255 || vision[27]==255)
{
seen.sight = true;
}
else
{
seen.sight = false;
}
break;
case BLUE:
for (int i=0; i<7; i++)
{
vision[i] = msg.data[((82)+(i*16))]; //creates array of all R values from RGB vision-array (reward is pure red so == (255,0,0) uintRGB - R value every 4 but that makes it lag so 16
}
if( vision[0]==255 || vision[1]==255 || vision[2]==255 || vision[3]==255 || vision[4]==255 || vision[5]==255 || vision[6]==255 || vision[7]==255 || vision[8]==255 || vision[9]==255 || vision[10]==255 || vision[11]==255 || vision[12]==255 || vision[13]==255 || vision[14]==255 || vision[15]==255 || vision[16]==255 || vision[17]==255 || vision[18]==255 || vision[19]==255 || vision[20]==255 || vision[21]==255 || vision[22]==255 || vision[23]==255 || vision[24]==255 || vision [25]==255 || vision[26]==255 || vision[27]==255)
{
seen.sight = true;
}
else
{
seen.sight = false;
}
break;
default:
std::cout << "seenBool ERROR: Please set char colour to either RED, GREEN, or BLUE." << std::endl;
seen.sight = false;
break;
}
}
void placeBool(std_msgs::Bool& placeBool, Enki::Racer* racer, double circleCentreX, double circleCentreY, double circleRad ,double maxX, double maxY)
{
if ((racer->pos.x >= (circleCentreX - circleRad)) && (racer->pos.x <= (circleCentreX + circleRad)) && (racer->pos.y <= (circleCentreY + circleRad)) && (racer->pos.y >= (circleCentreY - circleRad)))
{
placeBool.data = true;
}
else
{
placeBool.data = false;
}
}
This function (and struct) would later be used in the main.cpp file - does this work? can you use a struct in this way at all? I am very much a beginner with both C++ and ROS so I apologise if the code is hard to follow/ugly/bad practice - any feedback is appreciated.
Thanks.
Asked by 98JMC on 2020-12-01 17:15:17 UTC
Comments
This will be a QA about C++, so if this comment doesn't seem to solve your problem, I suggest you to use another QA site.
The
}
is missing. The}
in front ofvoid seenBool
is forswitch (pellet_colour){
, not the last{
insetPelletColour
.Asked by miura on 2020-12-02 10:31:11 UTC