Initializing Objects and assign it to no pointer variables - c++

I am having trouble initializing a couple of objects.I am writing a program that will perform frontier based exploration for a mobile robot using Player/Stage simulation 2.0. I have a class called Explorer. The objects I am having difficulty initializing are robot, pp, lp. I look at the reference page online, and I believe it is because there is no assignment operator for this but im hoping there is another way to do it.;
This is my header
#ifndef EXPLORER_H_
#define EXPLORER_H_
#include <libplayerc++/playerc++.h>
#include <iostream>
#include <fstream>
#include <math.h>`
#include <list>
#include "Map.h"
using namespace PlayerCc;
using namespace std;
struct Pose {
double x;
double y;
double theta;
};
struct Frontier {
int startRow;
int startCol;
int endRow;
int endCol;
double score;
};
class Explorer {
public:
Explorer();
void Explore(Map *map);
void performLaserSweep(Map *map);
void detectandgroupFrontiers(Map *map);
Frontier score_pick_Frontier();
void goToFrontier(Frontier f);
private:
PlayerClient robot;
Position2dProxy pp;
LaserProxy *lp;
Pose pose;
list<Frontier> unexploredFrontiers;
};
#endif /* EXPLORER_H_ */
this is my .cc file all that matters is the constructor so that is all i am showing
#include "Explorer.h"
Explorer::Explorer() {
robot = new PlayerClient("127.0.0.1", 6665);
pp = new Position2dProxy(robot, 0);
lp = new LaserProxy(robot, 0);
if (lp == NULL) {
cerr << "Error initializing LASER" << endl;
exit(1);
}
pp.SetMotorEnable(true);
}
Thank you in advance for the help
this is the compiler error
Explorer.cc: In constructor ‘Explorer::Explorer()’:
Explorer.cc:11: error: no matching function for call to ‘PlayerCc::Position2dProxy::Position2dProxy()’
/usr/include/player-2.0/libplayerc++/playerc++.h:1566: note: candidates are: PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint)
/usr/include/player-2.0/libplayerc++/playerc++.h:1553: note: PlayerCc::Position2dProxy::Position2dProxy(const PlayerCc::Position2dProxy&)
Explorer.cc:13: error: base operand of ‘->’ has non-pointer type ‘PlayerCc::PlayerClient’
Explorer.cc:13: error: expected unqualified-id before ‘new’
Explorer.cc:13: error: expected ‘;’ before ‘new’
Explorer.cc:14: error: no matching function for call to ‘PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient&, int)’
/usr/include/player-2.0/libplayerc++/playerc++.h:1566: note: candidates are: PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint)
/usr/include/player-2.0/libplayerc++/playerc++.h:1553: note: PlayerCc::Position2dProxy::Position2dProxy(const PlayerCc::Position2dProxy&)
Explorer.cc:15: error: no matching function for call to ‘PlayerCc::LaserProxy::LaserProxy(PlayerCc::PlayerClient&, int)’
/usr/include/player-2.0/libplayerc++/playerc++.h:900: note: candidates are: PlayerCc::LaserProxy::LaserProxy(PlayerCc::PlayerClient*, uint)
/usr/include/player-2.0/libplayerc++/playerc++.h:881: note: PlayerCc::LaserProxy::LaserProxy(const PlayerCc::LaserProxy&)
make: *** [all] Error 1

robot in the Explorer class is not a pointer, but you are trying to initialize it with the new keyword:
robot = new PlayerClient("127.0.0.1", 6665); // this won't work
Same thing with the variable pp.
One of the notes on an error you're getting: note: candidates are: PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint) also suggest that the constructor needs a PlayerClient pointer.
Try this in the Explorer class:
PlayerClient *robot;
And don't forget do delete it when you are done with it.
An easy way to spot errors like these are looking closely at the error messages. When the error says error: base operand of ‘->’ has non-pointer type it simply means that you are trying to use the pointer operator -> on something that is not a pointer.

Rather than change your class's members to pointers (which comes with its own complications), consider initializing the members rather than assigning to them. Try a Google on "c++ member initializer list" (this one result may be a good place to start: http://www.cplusplus.com/forum/articles/17820/)

From the error it tells candidates are: PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint) but you are passing robot which is not declared as a pointer. You have declared it as PlayerClient robot; However, you are using robot as a pointer to an object.
So change that to PlayerClient *robot; and this error should be taken care off.

Related

Can unity C unittest framework be used to unittest C++ code?

I'm using the unity framework to unittest my C code on an AtMega32A. This works great. now I wonder if I can somehow trick unity to also test my C++ code.
I've made a small proof of concept program that exposes all the members of the C++ class via a public function. Now I would like to put a number of assert macro's in that public function. The program can be found here:
https://github.com/cdwijs/cpp_unit_test
In my main I have the following:
int main(void)
{
UNITY_BEGIN();
//RUN_TEST(myPrivate.executeTest); //unity_internals.h(707,65): error: invalid use of non-static member function
//RUN_TEST((void*)myPrivate.executeTest); //error: invalid use of member function (did you forget the '()' ?)
//RUN_TEST((func)))myPrivate.executeTest); //error: 'func' was not declared in this scope
//RUN_TEST(&myPrivate.executeTest); //error: cannot convert 'void (Private::*)()' to 'UnityTestFunction {aka void (*)()}' for argument '1' to 'void UnityDefaultTestRun(UnityTestFunction, const char*, int)'
UNITY_END();
myPrivate.more();
myPrivate.more();
myPrivate.less();
/* Replace with your application code */
while (1)
{
}
}
And in Private_test.cpp I have the following:
#include "private.h"
#include "unity.h"
void Private::executeTest (void)
{
myNumber = 3;
TEST_ASSERT_EQUAL_INT(3,myNumber);
more();
TEST_ASSERT_EQUAL_INT(4,myNumber);
}
I can't figure out howto run the test function. Any Idea's? Am I looking in the correct direction, or should I use a test framework that is specifically aimed at C++?

How do i use an array of a different class in a class?

Town.h
class Town;
Tailor.h
#include "Town.h"
class Bully;
#include "Bully.h"
class Tailor
{
public:
//LookAround is called only in randWalk
void lookAround(Town & town, const short direction, const Bully bully[]);
//THIS is the code inside randwalk that calls bully
if(!bully[0].punch(*this,town))
void randWalk(Town & town, const Bully bully[]);
private:
}
Bully.h
#include "Town.h"
class Tailor;
#include "Tailor.h"
class Bully
{
public:
bool punch(Tailor& tailor, Town & town);
private:
}
main.cpp
#include "Town.h"
#include "Tailor.h"
#include "Bully.h"
int main()
{
const char PLAYER_NAME[] = "Milhouse";
Bully bully_team[5];
//create a town
Town r(10,40,25);
//create a Tailor player called PLAYER_NAME and place him at 2,3
Tailor player(PLAYER_NAME);
player.placeMe(r);
//make the character move 12 times
for(int i = 0; i < 100; i++)
{
player.randWalk(r, bully_team);
}
return 0;
}
The problem is that I need access to an array of type Bully in a Player function, but Bully needs type Player to function. This Bully array needs to be declared in main. I have been trying many different ways of forwarding classes, but i can't seem to get it work.
In file included from Tailor.h:8:0,
from main.cpp:7:
Bully.h:43:17: error: 'MAX_NAME_LENGTH' was not declared in this scope
char m_name[MAX_NAME_LENGTH];
^
Bully.h: In function 'std::ostream& operator<<(std::ostream&, const Bully&)':
Bully.h:31:33: error: 'const class Bully' has no member named 'm_name'
int length = strlen(bully.m_name);
^
Bully.h:35:23: error: 'const class Bully' has no member named 'm_name'
cout << bully.m_name[i];
^
In file included from Tailor.h:8:0,
from Tailor.cpp:1:
Bully.h:43:17: error: 'MAX_NAME_LENGTH' was not declared in this scope
char m_name[MAX_NAME_LENGTH];
^
Bully.h: In function 'std::ostream& operator<<(std::ostream&, const Bully&)':
Bully.h:31:33: error: 'const class Bully' has no member named 'm_name'
int length = strlen(bully.m_name);
^
Bully.h:35:23: error: 'const class Bully' has no member named 'm_name'
cout << bully.m_name[i];
^
Tailor.cpp: In member function 'void Tailor::lookAround(Town&, short int, const Bully*)':
Tailor.cpp:70:34: error: passing 'const Bully' as 'this' argument discards qualifiers [-fpermissive]
if(!bully[0].punch(*this,town))
^
In file included from Tailor.h:8:0,
from Tailor.cpp:1:
Bully.h:27:10: note: in call to 'bool Bully::punch(Tailor&, Town&)'
bool punch(Tailor& tailor, Town & town);
^
Use this order:
Declare Player and Bully
Define Player and Bully
Define the functions of Player and Bully and main
The order within 2. may be significant. Only one of Player and Bully can depend on the definition of the other, and that dependency determines which must be defined after the other. The one that doesn't have a dependency must at most depend on the declaration of the other. Declaring a function that takes a pointer to an array does not require the definition of the pointed type, so Player apparently doesn't depend on the definition of Bully.
The order within 1. and 3. is not significant, and each of the functions may be defined in separate translation unit (source file) if you so wish.

passing a class member function: type issues

I use the AccelStepper library in my Arduino project, the library has a constructor, with functions as parameters:
AccelStepper(void (*forward)(), void (*backward)());
In the main sketch, this is the code used:
void forwardstep() {
AFstepper->onestep(FORWARD, stepType); //some code to move the motor
}
void backwardstep() {
AFstepper->onestep(BACKWARD, stepType); //some code to move the motor
}
AccelStepper stepper(forwardstep, backwardstep);
as long as this code is in the main sketch, everything works well.
I have created a class that has an AccelStepper object and the forwardstep() and backwardstep() functions as members, but I cannot pass the functions to the constructor of AccelStepper:
.h file:
#define IICADDRESS 0x60
class FilterWheel : public Device
{
public:
FilterWheel();
void forwardstep();
void backwardstep();
void (*fwdstp)(); //function pointer
void (*bckwdstp)(); //function pointer
private:
//Adafruit Motor Shield object
Adafruit_MotorShield AFMS;
//Adafruit Stepper Motor object
Adafruit_StepperMotor *AFstepper;
//AccelStepper wrapper
AccelStepper stepper;
};
.cpp file:
#include "FilterWheel.h"
//constructor
FilterWheel::FilterWheel()
{
fwdstp = &FilterWheel::forwardstep;
bckwdstp = &FilterWheel::backwardstep;
Adafruit_MotorShield AFMS (IICADDRESS);
Adafruit_StepperMotor *AFstepper = AFMS.getStepper(200, 1); //M1 M2
//AccelStepper stepper(forwardstep, backwardstep); //doesn't work
AccelStepper stepper(fwdstp, bckwdstp); //works only if fwdstp = &FilterWheel::forwardstep; and bckwdstp = &FilterWheel::backwardstep; are commented out
}
//go 1 step forward
void FilterWheel::forwardstep() {
AFstepper->onestep(FORWARD, stepType);
}
//go 1 step backward
void FilterWheel::backwardstep() {
AFstepper->onestep(BACKWARD, stepType);
}
when I try to pass the functions directly,
AccelStepper stepper(forwardstep, backwardstep);
the compiler shows the following error:
FilterWheel.cpp:34: error: no matching function for call to 'AccelStepper::AccelStepper(<unresolved overloaded function type>, <unresolved overloaded function type>)'
AccelStepper.h:AccelStepper(void (*)(), void (*)())
AccelStepper.h:AccelStepper(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, bool)
AccelStepper.h:AccelStepper(const AccelStepper&)
Error compiling
when I attach the functions to the function pointers,
fwdstp = &FilterWheel::forwardstep;
bckwdstp = &FilterWheel::backwardstep;
AccelStepper stepper(fwdstp, bckwdstp);
the compiler shows these errors:
FilterWheel.cpp:In constructor 'FilterWheel::FilterWheel()'
FilterWheel.cpp:22: error: cannot convert 'void (FilterWheel::*)()' to 'void (*)()' in assignment
FilterWheel.cpp:23: error: cannot convert 'void (FilterWheel::*)()' to 'void (*)()' in assignment
Error compiling
how can I solve this issue?
forwardstep() and backwardstep() are non-static member functions, so this fwdstp = &FilterWheel::forwardstep; is a pointer to member function, it can't be converted to pointer to function because it needs an object to call it on.
You have to make your functions static or standalone.

Understanding 'using' keyword : C++

Can someone please explain below output:
#include <iostream>
using namespace std;
namespace A{
int x=1;
int z=2;
}
namespace B{
int y=3;
int z=4;
}
void doSomethingWith(int i) throw()
{
cout << i ;
}
void sample() throw()
{
using namespace A;
using namespace B;
doSomethingWith(x);
doSomethingWith(y);
doSomethingWith(z);
}
int main ()
{
sample();
return 0;
}
Output:
$ g++ -Wall TestCPP.cpp -o TestCPP
TestCPP.cpp: In function `void sample()':
TestCPP.cpp:26: error: `z' undeclared (first use this function)
TestCPP.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
I have another error:
error: reference to 'z' is ambiguous
Which is pretty clear for me: z exists in both namespaces, and compiler don't know, which one should be used. Do you know? Resolve it by specifying namespace, for example:
doSomethingWith(A::z);
using keyword is used to
shortcut the names so you do not need to type things like std::cout
to typedef with templates(c++11), i.e. template<typename T> using VT = std::vector<T>;
In your situation, namespace is used to prevent name pollution, which means two functions/variables accidently shared the same name. If you use the two using together, this will led to ambiguous z. My g++ 4.8.1 reported the error:
abc.cpp: In function ‘void sample()’:
abc.cpp:26:21: error: reference to ‘z’ is ambiguous
doSomethingWith(z);
^
abc.cpp:12:5: note: candidates are: int B::z
int z=4;
^
abc.cpp:7:5: note: int A::z
int z=2;
^
which is expected. I am unsure which gnu compiler you are using, but this is an predictable error.
You get a suboptimal message. A better implementation would still flag error, but say 'z is ambiguous' as that is the problem rather than 'undeclared'.
At the point name z hits multiple things: A::z and B::z, and the rule is that the implementation must not just pick one of them. You must use qualification to resolve the issue.

Unable to pass reference from one class to another

I'm writing a C++ program intended to manage the resources and duties in a flat.
This program is intended to run in Linux Shell.
I created a class call HomePage for users to login. I've also got another class called SelectionPage. Which are the Menus after the user got past the login. In my main functions, i wish to pass some data obtained by an instance of the HomePage class, to an instance of the SelectionPage class.
I've been stuck here for at least 6 hours, Could someone please help?
Here's the error message:
g++ C_Main.cpp C_HomePage.cpp C_SelectionPage.cpp -o Project
C_Main.cpp: In function ‘int main()’:
C_Main.cpp:17:93: error: no match for call to ‘(std::vector<std::basic_string<char> >) ()’
C_Main.cpp:17:122: error: no match for call to ‘(std::vector<std::basic_string<char> >) ()’
C_HomePage.cpp:290:41: error: no ‘std::vector<std::basic_string<char> > HomePage::getDutiesList()’ member function declared in class ‘HomePage’
C_HomePage.cpp:294:44: error: no ‘std::vector<std::basic_string<char> > HomePage::getResourcesList()’ member function declared in class ‘HomePage’
C_SelectionPage.cpp:9:144: error: declaration of ‘SelectionPage::SelectionPage(std::string, int, std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >, std::string)’ outside of class is not definition
C_SelectionPage.cpp:9:146: error: expected unqualified-id before ‘{’ token
make: *** [Project] Error 1
Here is my main()
// Testing Home Page Functionality
#include "H_HomePage.h"
#include "H_SelectionPage.h"
using namespace std;
string initializationFile = "D_initialization.dat";
string flatMemberFile = "D_flatMember.dat";
int main()
{
HomePage frontEnd(initializationFile); //Create Boundary Object
system("clear"); //Clear Terminal Screen
frontEnd.login(flatMemberFile);
SelectionPage Menu(frontEnd.getManager(), frontEnd.getInitPoints(), frontEnd.getDutiesList(), frontEnd.getResourcesList(), frontEnd.getLoginName());
Menu.showManagerMenu();
return 0;
}
Here's my constructor for SelectionPage class, as described in the header file:
...
SelectionPage(string Manager, int Points, vector <string> dutiesList, vector <string> resourceList, string loginName);
...
Here's the implementation of the constructor for SelectionPage class:
SelectionPage::SelectionPage(string newanager, int points, vector <string> newDutiesList, vector <string> newResourceList, string newLoginName);{
manager = newManage;
initPoints = points;
dutiesList = newDutiesList;
resourceList = newResourceList;
loginName = newLoginName;
}
could someone please, please help? I'll be ever so grateful!
Look at your error messages carefully.
C_HomePage.cpp:290:41: error: no ‘std::vector<std::basic_string<char> > HomePage::getDutiesList()’ member function declared in class ‘HomePage’
C_HomePage.cpp:294:44: error: no ‘std::vector<std::basic_string<char> > HomePage::getResourcesList()’ member function declared in class ‘HomePage’
You probably have incorrect declarations for HomePage::getDutiesList() and HomePage::getResourcesList(), or none at all. It's hard to tell what exactly is wrong with them without looking at the class declaration. This is also the reason of compiler errors in your main().
The reason of the other two compiler errors is probably the spurious ; between ) and {' in the definition ofSelectionPage::SelectionPage`. Get rid of it.