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.

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

AxisCamera is a singleton. You shouldn’t be calling the constructor directly. Try this.

AxisCamera& camera;

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

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

camera = AxisCamera::GetInstance();

I get an error that says “cannot convert ‘AxisCamera’ to ‘AxisCamera*’ in assignment”. Can you explain what this means?

Thanks again.

You should declare the camera as:


AxisCamera &camera;

camera = AxisCamera::GetInstance();

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.

I really appreciate both of your replies, but making the corrections you suggested has led me to another problem.

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.

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

try this:

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::ZomBDashboardPacketSender()':** 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