Issue when attempting to build code

Hello. When attempting to build my project it fails with a number of errors.

C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:35:7: error: redefinition of
‘class Robot’
class Robot : public frc::TimedRobot {
In file included from C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:8:0:
C:\Users\clovi\Desktop\Springof2019\src\main\include/Robot.h:15:7: note: previous definition of ‘class Robot’
class Robot : public frc::IterativeRobot {
C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp: In member function ‘virtual
void Robot::TeleopPeriodic()’:
C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:139:3: error: ‘drive’ was not declared in this scope
drive.MecanumDrive_Cartesian(x, y, z, 0);
C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:139:3: note: suggested alternative: ‘div’
drive.MecanumDrive_Cartesian(x, y, z, 0);
In file included from C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:8:0:
C:\Users\clovi\Desktop\Springof2019\src\main\include/Robot.h: In instantiation of ‘void
frc::impl::RunRobot(wpi::mutex&, Robot**) [with Robot = Robot; wpi::mutex = wpi::priority_mutex]’:
C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/RobotBase.h:55:30: required from ‘frc::StartRobot()::<lambda()> [with Robot = Robot]’
C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/RobotBase.h:53:22: required from ‘struct frc::StartRobot() [with Robot = Robot]::<lambda()>’
C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/RobotBase.h:53:17: required from ‘int frc::StartRobot() [with Robot = Robot]’
C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:201:44: required from here
C:\Users\clovi\Desktop\Springof2019\src\main\include/Robot.h:15:7: warning: ‘frc::IterativeRobot::IterativeRobot()’ is deprecated: Use TimedRobot instead. It’s a drop-in replacement that provides more regular execution periods. [-Wdeprecated-declarations]
class Robot : public frc::IterativeRobot {
In file included from C:\Users\clovi\Desktop\Springof2019\src\main\include/Robot.h:12:0, from C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:8:
C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/IterativeRobot.h:30:3: note: declared here
In file included from C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/IterativeRobotBase.h:13:0,
from C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/IterativeRobot.h:12,
from C:\Users\clovi\Desktop\Springof2019\src\main\include/Robot.h:12,
from C:\Users\clovi\Desktop\Springof2019\src\main\cpp\Robot.cpp:8:
C:\Users\clovi.gradle\caches\transforms-2\files-2.1\f78589b1f173909e18ca9d5419a0a5fa\wpilibc-cpp-2020.2.2-headers/frc/RobotBase.h:30:16: note: synthesized method ‘Robot::Robot()’ first required here
static Robot theRobot;

Does anyone know how to solve these issues? Thanks.

With C++, you should always start with the first error. In this case, it looks like you’re defining the Robot class in both Robot.h and Robot.cpp. You should only define it in the header, and only the function definitions should be in the .cpp file.

It would be very helpful in the future if you can put your code somewhere we can get to it (e.g. GitHub). It’s often hard to figure out error messages unless you can see the code.

Here is the GitHub link:

As for a header file, I have no idea what that is.

The header file is the .h file (in this case Robot.h). The code you posted only has the .cpp file, but is using #include to bring in Robot.h as well.

It seems like you’re not very familiar with C++… while I enjoy coding in it, C++ has a very steep learning curve that’s not going to be easy to teach in this kind of forum. Do you have local mentor resources that might be able to help you?

I am relatively new, and yes we have a mentor but we haven’t been able to solve this.

I think it might be that robot is an already defined class in wpilib. i would change the name from
Robot to RobotTeamNumber, for me that would be Robot2412 (note i have little experience with c++ and wpilib c++)

If you look at Robot.h, you’ll see that file contains a class Robot { ... }; definition as well. You have #include "Robot.h" at the top of the Robot.cpp file, which pulls in the contents of Robot.h into the file as it’s being compiled. The compiler sees the class Robot in Robot.h because of this, but then later in Robot.cpp you also say class Robot { ... };. Thus the compiler complains that you’re trying to define it twice.

There’s a couple of options here. The “standard” way is to keep the class { ... } part in the .h file, and then only define the functions and static variables in the .cpp file. There’s a special syntax that has to be used for the latter… you have to prefix the function name at the definition location with Robot::.

If you’re not familiar with C++, and you’re struggling with this, I urge you to consider using Java instead, which has cleaner syntax and is easier to get started with (e.g. in Java there’s no separation between .h and .cpp files).

1 Like

After removing the second definition, I’m seeing a repeated error about something being marked override, but doesn’t override.

EX: error: ‘void Robot::AutonomousInit()’ marked ‘override’, but does not override
void AutonomousInit() override;

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.