Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Error: within this context (http://www.chiefdelphi.com/forums/showthread.php?t=87845)

ehlochbr 09-12-2010 01:42

Error: within this context
 
I'm trying to add the code for the Axis camera and I keep seeing this error when I build the project "error: within this context".

#include "WPILib.h"
#include "Gamepad.h"
#include "Vision/AxisCamera.h"

class RobotDemo : public SimpleRobot
{
RobotDrive myRobot; // robot drive system
Gamepad controller;
AxisCamera camera;

public:
RobotDemo(void):
myRobot(10, 9, 2, 1), // these must be initialized in the same order
controller(1)
{ <------ERROR HERE

All I did was add the AxisCamera header file and the identifier (AxisCamera camera).

What am I missing? Thanks.

Greg McKaskle 09-12-2010 03:16

Re: Error: within this context
 
You've added a data member called camera, of type AxisCamera, no problem there. But the code you've included has a constructor with an initialization list, and you haven't added a value for camera field.

Not being that familiar with the C++ WPI code, the issue is likely that the AxisCamera doesn't have a default constructor or for some other reason, the compiler wants camera to be assigned using the initialization list.

Try putting

camera(???)

after the controller(1) line and before the {. Replace ??? with the parameters required for an AxisCamera object to be initialized.

Gotta love compiler error messages sometimes.

Greg McKaskle

Dave Scheck 09-12-2010 09:01

Re: Error: within this context
 
AxisCamera is a singleton. You shouldn't be calling the constructor directly. Try this.
Code:

AxisCamera& camera;

//In your constructor do this
camera = AxisCamera::GetInstance();


ehlochbr 09-12-2010 09:59

Re: Error: within this context
 
Thanks for the reply. It looks like I have a different problem now. When I try to implement your suggestion and use:

Code:

camera = AxisCamera::GetInstance();
I get an error that says "cannot convert 'AxisCamera' to 'AxisCamera*' in assignment". Can you explain what this means?

Thanks again.

mikets 09-12-2010 10:09

Re: Error: within this context
 
You should declare the camera as:
Code:

AxisCamera &camera;

camera = AxisCamera::GetInstance();


Dave Scheck 09-12-2010 10:15

Re: Error: within this context
 
Thanks for the correction mikets. I misread the header when I wrote my initial post. I updated it to have the reference declaration instead of a pointer.

ehlochbr 09-12-2010 17:54

Re: Error: within this context
 
I really appreciate both of your replies, but making the corrections you suggested has led me to another problem.

Code:

class RobotDemo : public SimpleRobot
{
        RobotDrive myRobot; // robot drive system
        Gamepad controller;
        AxisCamera& camera;
       
public:
        RobotDemo(void):
                myRobot(10, 9, 2, 1),       
                controller(1)
        {                                    <-------------------------ERROR HERE
                GetWatchdog().SetExpiration(0.1);
       
                camera = AxisCamera::GetInstance();  <--------ERROR HERE

The first error says "uninitialized reference". How do I initialize it?
The second error gets me back to where I started. It says "error within this context".

This feels like one step forward then two steps back. Thanks for your help.

byteit101 09-12-2010 18:01

Re: Error: within this context
 
Quote:

Originally Posted by ehlochbr (Post 984586)
I really appreciate both of your replies, but making the corrections you suggested has led me to another problem.

The first error says "uninitialized reference". How do I initialize it?
The second error gets me back to where I started. It says "error within this context".

This feels like one step forward then two steps back. Thanks for your help.

You need to initialize all non pointer members before the body (and especially references, they only have one life)

try this:
Code:

class RobotDemo : public SimpleRobot
{
        RobotDrive myRobot; // robot drive system
        Gamepad controller;
        AxisCamera& camera;
       
public:
        RobotDemo(void):
                myRobot(10, 9, 2, 1),       
                controller(1),
camera(AxisCamera::GetInstance())
        {                                   
                GetWatchdog().SetExpiration(0.1);

Also, "error within this context" is part of a larger error, open the build console and find the red lines to find the rest of the message. many messages are 2 or more parts.
Ex (bold is error in code)
file.cpp: In constructor `ZomBDashboardPacketSender::ZomBDashboardPacketSen der()':
file.cpp:301: error: uninitialized reference member `ZomBDashboardPacketSender::hp'
WPILib/Dashboard.h:58: error: `void Dashboard::operator=(const Dashboard&)' is private
file.cpp:302: error: within this context


All times are GMT -5. The time now is 13:26.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi