Log in

View Full Version : C++ NetworkTable code error


CB313
28-06-2016, 19:36
I'm getting an error in my code cannot convert 'std::shared_ptr<NetworkTable>' to 'NetworkTable*' in assignment
Can someone help me explain what this means? I'm rather new to coding, and I don't understand what i did wrong. I copied the code from the FRC Network Tables page (Here (http://wpilib.screenstepslive.com/s/3120/m/7912/l/80205-writing-a-simple-networktables-program-in-c-and-java-with-a-java-client-pc-side)) And the line table = NetworkTable::GetTable("datatable"); is the only one with an error. If it's any help, the whole code is here. (https://github.com/TheBionicZebras/Team313/blob/master/Summer2016Code)

axton900
28-06-2016, 21:25
Hey!
The getTable method returns a shared pointer. So you have to declare your table as a shared pointer by writing:
std::shared_ptr<NetworkTable> table = NetworkTable::GetTable("datatable");

That should fix that issue!


That tutorial is from 2014 and WPILib has been updated and is now in C++11 which introduced Smart Pointers. I would suggest taking a look at the library :Here! (http://http://first.wpi.edu/FRC/roborio/release/docs/cpp/)

alst
28-06-2016, 23:17
(That link should be this (http://http//first.wpi.edu/FRC/roborio/release/docs/cpp/) instead - missing a colon.)

Basically, the error means that the type of your table variable is "NetworkTable*" (a pointer to a NetworkTable), while GetTable() returns a std::shared_ptr<NetworkTable>, which can't be converted to a NetworkTable* directly. The place you actually want to change is currently line 9 in your code, where you declare "table" - it should be "std::shared_ptr<NetworkTable> table;".

Thad House
28-06-2016, 23:32
The things posted above are the proper solution. I'll contact the Screensteps people to get it updated.