Log in

View Full Version : define a new class(C++)


nadavsen2
28-01-2009, 11:36
hello, i'v been trying to define a new class of my own in c++
and i dont know why but the Windriver isnt recognizing my class..

where i need to put the H and the Cpp files?

do i need to define the class in the Windriver somehow?


tnanks alot

gvarndell
28-01-2009, 15:37
hello, i'v been trying to define a new class of my own in c++ and i dont know why but the Windriver isnt recognizing my class..
where i need to put the H and the Cpp files?
do i need to define the class in the Windriver somehow?
tnanks alot

Please elaborate on what you mean when you say Windriver isn't recognizing your class.
Have you created a downloadable kernel module project?
If so, have added the .h and .cpp file(s) to that project?
If so, have you built the containing project?
You haven't given enough information about what you've tried and how you went about things.

nadavsen2
28-01-2009, 17:44
My code is working , i dont have any problems while downloading the code to
the robot ,

When you say to add the H and Cpp files , you mean copy them to the directory? or that i need to make another proccess in the WindRiver to add them?

and yes , i can build my project , the Windriver isnt recognize the class
unlike the Jaguar or the PWM classes for example...

hope you understand... thanks anyway..

gvarndell
28-01-2009, 17:57
add the H and Cpp files , you mean copy them to the directory?

OK, what you need to do is drag the files from a Windows explorer window and drop them right onto the project in the Workbench Project Navigator view.
They will then become part of your project and will be compiled and linked with it.
Make sure to drop the files on the top of the project -- not somewhere in the middle. The best thing to do is collapse the project so you can't see any of what's inside. That way, you can't miss with dragging and dropping.

Here's a snippet from the Eclipse documentation (which should be avaliable to you by using Help->Help Contents in Workbench)

Importing files

Files can be imported into the Workbench either by :

* dragging and dropping from the file system, or
* copying and pasting from the file system, or
* Using the Import wizard.

Using drag and drop or copy/paste to import files relies on operating system support that is not necessarily available on all platforms. If the platform you are using does not have this support, you can always use the Import wizard.

Dave Scheck
28-01-2009, 18:02
Your question is still a little confusing. Do you see the files that you created in the tree on the left? If not, follow the instructions below.

If you're creating files from scratch, right click on the project and go to add new file (don't remember the exact command). This will add an empty file to your project directory.

If you already created the files, you can copy them into your project directory just like you would any other file (via Windows Explorer, shell, etc). You will then need to right click on the project and go to refresh in order for the files to show up in the tree.

If the files are in the source tree, does the compiler attempt to build them? If you intentionally add a syntax error does the compiler catch it? If not, check the build target in the tree (I think it's green) and check to see if it is picking up all files in your project.

Are you adding a #include or forward declaration in the files that use the classes?

If there are compiler errors that you are getting, it would be helpful for you to post them here. That way people will be able to help you pinpoint your problem.

nadavsen2
29-01-2009, 09:33
I tried to drugg the files to the project and right , my class suddenly is being recognize by the code, but now the class i'v belt is not recognize the other classes like PWM or DigitalInput and so on (thats the errors that i get)

gvarndell
29-01-2009, 09:49
I tried to drugg the files to the project and right , my class suddenly is being recognize by the code, but now the class i'v belt is not recognize the other classes like PWM or DigitalInput and so on (thats the errors that i get)

You have transitioned from Workbench problem to basic C++ programming problem. I'll have to let others help you with that but I would suggest providing more information than you have. People tend to not help when it's too much work.

Pancake
29-01-2009, 14:40
Typically, there's two files, the header file and the CPP file.

Example Header

// Camera.h
#ifndef CAMERA_H
#define CAMERA_H

class Camera
{
public:
void doSometing();
};

#endif


Then make a .cpp file with the same name as the header file:

// Camera.cpp

#include "Camera.h"

void Camera::doSomething()
{
printf("Doing something...");
}


Now when you get to the robot code, all you need to do is simply include the "Camera.h" file:


//MyRobot.cpp
#include "Camera.h"

class MyRobot : public SimpleRobot
{
/* Code ommited for simplicity */
void Autonomous()
{
Camera c();
c.doSomething();
}
};

START_ROBOT_CLASS(MyRobot);


Hope this example helps...

Mike Mahar
29-01-2009, 16:01
Make sure that you put in the proper include files at the top of your class source files. For example, look your example robot code as provided by WPI and look at the top of the file. There are #include files there. Make sure that you have enough includes to recognize the wpi library. The simplest is to #include "WPILib.h"

nadavsen2
29-01-2009, 17:46
i didnt put those lines

#ifndef CAMERA_H
#define CAMERA_H

and the #endif..

what is this macro? or its not macro?

and why in the OperatorControl you access you class by typing
Camera c();

and not
Camera *c;
c = new Camera();

?

wt200999
29-01-2009, 18:08
http://www.cplusplus.com/doc/tutorial/

(or any other C++ tutorial/book)

Dave Scheck
29-01-2009, 18:14
i didnt put those lines

#ifndef CAMERA_H
#define CAMERA_H

and the #endif..

what is this macro? or its not macro?It's called a header guard. You can read about it here (http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/) or here (http://www.cs.niu.edu/~mcmahon/cs241/c241man/node90.html)

why in the OperatorControl you access you class by typing
Camera c();

and not
Camera *c;
c = new Camera();In the end both of those do the same thing, instantiate a Camera called c. Which one you use depends on how you plan to use it. If you're only using it in that location and you don't need to pass it around, you can use the non-pointer version. If you plan to be passing the object around, or it is an attribute of a class you'll want to use the pointer approach.

ehlochbr
01-11-2010, 23:05
I'm having a similar problem as described above. I'm trying to get the Logitech Gamepad to work with our robot. I've found the .h and .cpp files I need. I've included the .h file in the WPILib and included the reference (#include) at the top of my source program. Do I need to do something with the .cpp file? If so, what?

Windriver will build my project just fine and download it to the robot. The problem is that the robot doesn't recognize the code. It acts like there isn't anything there. The only thing I can think of is that I must be doing something wrong with the gamepad files. Please help!

Thanks,
Eric

Dave Scheck
02-11-2010, 09:32
I'm having a similar problem as described above. I'm trying to get the Logitech Gamepad to work with our robot. I've found the .h and .cpp files I need. I've included the .h file in the WPILibI'll need more information to confirm, but I think your problem comes with the above statement.

I'm assuming that you took the gamepad .cpp and .h and put it in the WPILib directory, included it in your robot cpp file, built it and it failed at runtime. This is actually what I would expect because when you built, the compiler was able to find the gamepad.h file in the WPILib directory so it appeared to build fine. When the robot tried to run one of the gamepad specific members/methods, it failed because the gamepad class was never compile. Why? Because when you build your robot project, WPILib doesn't get rebuilt. Want proof? Go into your cpp file, take out a semicolon, and rebuild. You won't get an error.

In general I suggest not modifying WPILib directly or adding to it because if they come out with a new release, you will need to go in and make your change again.

The easy way to approach this problem is to take your gamepad files (both cpp and h) and put them directly in your project directory (you may need to right click on your project name and refresh the file list if you add it through Windows). When you rebuild your project, it will build up the gamepad files since it knows to build all cpp files.

Give that a shot and let us know how it goes

ehlochbr
02-11-2010, 10:17
When you say to include the files in my project directory, I'm assuming you mean import the files. For example, now I have MyRobot.cpp, Gamepad.h, & Gamepad.cpp (along with other folders and stuff) underneath my main project in windriver.

If this isn't what you meant then can you please elaborate on how to include the files in my directory without saving them in the WPILib folder?

Also, I see your point about changing the WPILib folder and took the files out.

I really appreciate your help. I just started helping out this year and I'm trying to catch up.

Mike Soukup
02-11-2010, 13:21
What he meant is to put Gamepad.h & Gamepad.cpp in the same folder as your robot code, which I believe is what you did. I believe you also have to refresh the file tree on the left by right clicking and selecting Refresh (going from memory here) so the files appear there and Windriver knows to include them in the build. As Dave suggested before, add a syntax error to Gamepad.ccp and build the project. If the build fails because of that error, you know it's getting compiled in. Once you know that, you can go ahead and use the class.