What does this line of code mean in c++? - c++

I cant understand this line of code from source code at github :
using NodePtr = std::shared_ptr<Node>;
I read the cppreference page here, but it didn't have any information regarding similar syntax. As much as I can guess, it is somewhat like #define in that when I use NodePtr from now on, it will replace it internally with std::shared_ptr<Node>. With that, I tried to test code but it didn't work.
Code :
test.h:
#ifndef TEST_H_
#define TEST_H_
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <utility>
#include <typeinfo>
#include <limits>
#include <functional>
namespace nnvm {
class Node;
using NodePtr = std::shared_ptr<Node>;
class Node {
public:
~Node();
inline bool is_variable() const;
inline int num_outputs() const;
inline int num_inputs() const;
};
}
#endif // TEST_H_
test.cpp:
#include "test.h"
#include <iostream>
static graphy::NodePtr Create();
int main(int argc, char const *argv[])
{
/* code */
graphy::Node *node = new graphy::Node();
std::cout << "Hello Graphy!!" << std::endl;
return 0;
}
Here is the error I get :
In file included from /usr/include/c++/5/unordered_map:35:0,
from test.h:7,
from test.cpp:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^
In file included from test.cpp:1:0:
test.h:18:7: error: expected nested-name-specifier before ‘NodePtr’
using NodePtr = std::shared_ptr<Node>;
^
test.cpp:5:14: error: ‘NodePtr’ in namespace ‘graphy’ does not name a type
static graphy::NodePtr Create();
^

At first glance your error is due to namespaces. The using statement is in namespace nnvm and not Graphy.
'using' is similar to 'typedef'. It's an alias allowing 'nnvm::NodePtr' to represent a 'std::shared_ptr'.
update
As #UnholySheep points out, you will also need to add a compiler setting to enable c++11 support as the compiler error states.

The error messages you're seeing suggest that you're trying to compile C++11 code with an old compiler that defaults to C++98 mode. You probably need a command line switch, something like -std=c++11 (or something similar, depending on exactly which compiler you're using). Or get a new compiler.

Related

Why g++ reported an error in C++ Standard Library in MacOS GDK?

I encountered a weird error when building my code:
Before I show you what the error was, I'd like to show you what the code is doing. This is a node class for my little cmd project, each node is a file/folder. I wrote a function to show the info of the node.
// Node.h
#include <string>
#include <memory>
using namespace std;
typedef bool NODE_TYPE;
#define FILE true
#define DIR false
class Node
{
private:
string _node_name;
NODE_TYPE _node_type;
string _create_date;
string _create_user;
weak_ptr<Node> _parent;
shared_ptr<Node> _sibling;
shared_ptr<Node> _child;
public:
/*Constructors, Destructors and other functions*/
const string toString();
};
// Node.cpp
#include "Node.h"
#include <string>
#include <sstream>
using namespace std;
const string Node::toString()
{
ostringstream buffer;
buffer << ... // the data member of Node
return buffer.str();
}
I wrote a test in main.cpp, and when I say g++ Node.cpp main.cpp -Wall -std=c++11, here's the error:
In file included from Node.cpp:6:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:184:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:163:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:40:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale.h:93:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale/_stdio.h:32:33: error: expected expression
int fprintf_l(FILE * __restrict, locale_t __restrict, const char * __restrict, ...)
Errors like this are 21 more, I guess there was something wrong with SDK, so I reinstalled it, however still no work. Could anyone help me?
I'd like to thank all comments below my question, they are really helpful.
In C standard library, FILE is defined as a structure to which stores the info of a file opened by the program. So my own macro definition leads to wrong macro expansion.
Here I'd like to reference the suggestion by Scott Meyers: For simple constants, prefer const objects or enums to #defines.

c++ error: expected class-name before '{' token

screenshot of Pose.hpp
I am trying to catkin_make a simple package and I am getting the error
.../Pose.hpp:17:1: expected class-name before token...
.../Odometry.cpp:12:3: expected class-name before token...
The responsible Pose header file is sampled here as:
#ifndef POSE_HPP
#define POSE_HPP
#include <string>
#include <vector>
#include <ostream>
#include "ros/serialization.h"
#include "ros/builtin_message_traits.h"
#include "ros/message_operations.h"
#include "ros/message.h"
#include "ros/time.h"
namespace turtle
{//line 17
template <class ContainerAllocator>
struct Pose_ : public ros::Message
{
typedef Pose_<ContainerAllocator> Type;
}; // struct Pose
...
} // namespace turtle
while on the referred header file is referenced in odometry.cpp shown as
#include <geometry_msgs/TwistWithCovarianceStamped.h>
#include <tf/transform_datatypes.h>
#include <robot_localization_demo/odometry.hpp>
namespace robot_localization_demo {
TurtleOdometry::TurtleOdometry(ros::NodeHandle node_handle, double frequency):
node_handle_{node_handle},
turtle_pose_subscriber_{node_handle_.subscribe("turtle1/pose", 16, &TurtleOdometry::turtlePoseCallback, this)},
turtle_twist_publisher_{node_handle_.advertise<geometry_msgs::TwistWithCovarianceStamped>("turtle1/sensors/twist", 16)},
frequency_{frequency},
{//line 12
;
}
and odometry includes Pose as well.
what am I missing here?
The official documentation for ros::Message says:
This base-class is deprecated in favor of a template-based serialization and traits system.
In the official source code it looks like it's still defined, but in a contrib version I found, the whole class definition is removed by #if 0:
namespace ros {
#if 0
class Message {
//
};
#endif
}
So, you most probably need to find a different base class.

C++ undefined reference to class (1 header 2 cpp's)

I am reading a book (C++ for dummies) as well as watching youtube videos to learn how to code. I am currently struggling with very simple class functions.
Main.cpp
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include "Test.h"
using namespace std;
int x;
int main(int nNumberofArgs, char* pszArgs[])
{
combat fight;
cout << x;
fight.dodmg();
cout << x;
return 0;
}
Test.h my header file with the class
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class combat
{
public:
int dodmg();
void zero_out();
private:
int x;
};
#endif // TEST_H_INCLUDED
Test.cpp class functions
#include "Test.h"
int combat::dodmg()
{
x = x - 5;
return x;
}
void combat::zero_out()
{
x = 20
}
I tried to make this very simplistic just to figure out how to work a class.
I included a lot of #includes just to try and make sure it wasn't something stupid like I needed strings.
I am not sure why but the videos I watched simply had the header say
ifndef TEST_H (of their respective code, mine has an _INCLUDE as well, I tried deleting it and it still didn't work.
My unfortunate errors
on line 14 of main.cpp fight.dodmg(); it says
\Beginning_Programming-CPP\Playing_with_class\main.cpp|14|undefined reference to `combat::dodmg()'|
then below that
||error: ld returned 1 exit status|
How are you compiling this? I think this is an issue because you arent compiling your Test.cpp file. If you arent already, try compiling with the command:
g++ main.cpp Test.cpp -o MyProgram
UPDATE:
Few things, you dont have a closing statement to your #ifndef directive in Text.h, you will need a constructor to set the value of x so i added one to the combat class also you were missing a semicolon in the zero_out function. I added comments to all the lines I changed.
Okay try this:
Test.h
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class combat
{
public:
combat(); // added constructor
int dodmg();
void zero_out();
private:
int x;
};
#endif // closed #ifndef
Text.cpp
#include "Test.h"
combat::combat() // implemented constructor
{
x = 20;
}
int combat::dodmg()
{
x = x - 5;
return x;
}
void combat::zero_out()
{
x = 20; // added ';'
}
Hope this helps,
Final edit: I dont think you really need your header guards in this scenario, you could remove the "#ifndef, #define, and the #endif" lines and not see a difference really
It sounds like you provide the wrong arguments for the compiler. Your header file (Test.h) simply provides signatures for the methods, but the implementations are given in the source file (Test.cpp).
This is an important part of writing C++ (or C) code. Your compiler does not automatically search for source files, so you need to tell it where to look, e.g.:
g++ -std=c++11 main.cpp Test.cpp -o main

g++ how to solve warning "used but never defined"? (not static or inline)

I have come across a strange compile error that I cannot make sense of. Firstly the error refers to the function as if it was in an anonymous namespace, however it is in fact inside namespace database. Secondly the "used but never defined" statement suggests that the compile requires me to define the function from within the header. The function is in fact declared in a separate implementation file. However the function is neither static nor inline so I am at a loss as to why it requires a definition in the header. It is a requirement that this piece of code is strictly compliant, because of this I have compiled with both -Wall -Werror. I have also included a shortened version of my source code for clarification.
Note: This question is different from other similar questions asked here in that it does not involve static or inline functions.
Error:
In file included from src/main.cpp:6:0:
include/database.hpp:19:6: error: 'void {anonymous}::SetupSettings()' used but never defined [-Werror]
void SetupSettings();
^
cc1plus.exe: all warnings being treated as errors
main.cpp
#include <iostream>
#include "config.hpp"
#include "database.hpp"
int main() {
database::SetupSettings();
return 0;
}
database.hpp
#ifndef database
#define database
#include <iostream>
#include "config.hpp"
#include "sqlite/sqlite3.h"
namespace database {
extern sqlite3* settings_database;
void SetupSettings();
// ^^ Apparent warning here.
} // namespace database
#endif
database.cpp:
#include <iostream>
#include <vector>
#include "config.hpp"
#include "database.hpp"
#include "sqlite/sqlite3.h"
namespace database {
sqlite3* settings_database;
void SetupSettings() {/*More code here*/}
} // namespace database
The problem is caused by use of:
#ifndef database
#define database
After that,
namespace database { ...
is seen as
namespace { ...
i.e. an anonymous namespace.
You need to use a different include guard macro, such as:
#ifndef database_hpp
#define database_hpp

Clang unknown class name 'exception'

For some odd reason I am having difficulties throwing an exception in C++. I throw without catching std::invalid_argument from the stdexcept header file. I have no real intention of catching as i want the application to fail anyway if the error occurs.
It seemed to be working fine until I #included the function definition class into the namespace of the header declaration. It was added outside of the namespace prior since they are template definitions and I wanted to separate the header from its definition; however, I realized this caused a subtle issue that I did not realized until only recently.
Is their something I am missing? I am using clang btw
Project Compilation
.
.
.
.
.
Compiling CPP file TrieTest.cpp ...
In file included from TrieTest.cpp:4:
In file included from ./Trie.hpp:62:
In file included from ./Trie.cpp:2:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/stdexcept:55:30: error: unknown class name 'exception'; did you mean
'::std::exception'?
class logic_error : public exception
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/exception:60:9: note: '::std::exception' declared here
class exception
^
In file included from TrieTest.cpp:4:
In file included from ./Trie.hpp:62:
In file included from ./Trie.cpp:2:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/stdexcept:112:32: error: expected class name
class runtime_error : public exception
^
2 errors generated.
EDIT: A bit of the src
Also I compile with
clang++ -Wall -Wextra -g -std=c++11 TrieTest.cpp -o TrieTest;
"Trie.h"
#ifndef COM_WORDGAME_UTILITY_TRIE_HPP
#define COM_WORDGAME_UTILITY_TRIE_HPP
#include <string>
using std::string;
namespace wordgame_utility{
template<typename value>
class Trie{
...Trie Function Declarations
};
//The compiler may not complain initialliy however
//Templates cause visibility issues with user code and is normally defined in the header
//this is a work around
#include "Trie.cpp"
}
#endif
"Trie.cpp" head -n 8
#include "Trie.hpp"
#include <stdexcept>
using namespace wordgame_utility;
template<typename value>
using TrieNode = typename Trie<value>::TrieNode;
...Trie Function Definitions
You have a cyclic include in your code.
Trie.hpp includes Trie.cpp which includes Trie.hpp. This is not meant to work in C++, where include is a literal inclusion (think, copy/pasting the included file at the point of the #include directive).
You need to define template methods into the header, or into a third file, that's all there is.
What is the effect of this cycle ? In general, poor error messages, as you can see by yourself.
In your case... let's play preprocessor ourselves:
// begin #include "Trie.hpp"
#define COM_WORDGAME_UTILITY_TRIE_HPP
// begin #include <string>
namespace std {
...
}
// end #include <string>
using std::string;
namespace wordgame_utility{
template<typename value>
class Trie{
...Trie Function Declarations
};
//The compiler may not complain initialliy however
//Templates cause visibility issues with user code
// and is normally defined in the header
//this is a work around
// begin #include "Trie.cpp"
// begin #include "Trie.hpp"
// -- empty because of the include guards --
// end #include "Trie.hpp"
// begin #include <stdexcept>
namespace std {
class logic_exception: public exception { };
}
// end #include <stdexcept>
using namespace wordgame_utility;
template<typename value>
using TrieNode = typename Trie<value>::TrieNode;
...Trie Function Definitions
// end #include "Trie.cpp"
} // namespace wordgame_utility
// end #include "Trie.hpp"
As you can see, it's a mess. The combination of using #include within an open namespace and having cyclic includes is downright ugly.
Fortunately, in the (near ?) future, this will be improved if modules are adopted, which are just a much saner alternative to textual inclusion.