-4

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();
              ^
Kapil Gupta
  • 7,091
  • 5
  • 16
  • 25
  • 2
    Of course it has. It's the [third link at the bottom](http://en.cppreference.com/w/cpp/language/type_alias). – Rakete1111 Jun 17 '17 at 14:56
  • 4
    It's an alias. You can use `NodePtr` instead of `std::shared_ptr` in your code. – Azeem Jun 17 '17 at 14:57
  • 4
    Read the first error message and do as it says. – molbdnilo Jun 17 '17 at 14:59
  • Related: https://stackoverflow.com/questions/20790932/what-is-the-logic-behind-the-using-keyword-in-c and https://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11 – Cody Gray - on strike Jun 17 '17 at 15:00
  • You've got everything right, except for the fact that this feature (as well as `shared_ptr`) are available starting from C++11 only. Looks like your compiler does not enable this by default, read the first error message to see what option you have to add to enable C++11. – yeputons Jun 17 '17 at 15:06
  • If you don't know what is a std::shared_ptr -> [this part of documentation](https://stackoverflow.com/documentation/c%2b%2b/509/smart-pointers/1672/sharing-ownership-stdshared-ptr#t=201706171501581232628) will help you. –  Jun 17 '17 at 15:08

2 Answers2

2

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.

  • 2
    *"#error This file requires compiler and library support for the ISO C++ 2011 standard."* - That is the first error, it has nothing to do with namespaces – UnholySheep Jun 17 '17 at 15:04
  • @UnholySheep Ah, yes. I stopped with the first error I noticed. But you are correct. There is more that one error here. – Michael Maniscalco Jun 17 '17 at 15:12
0

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.

Ross Smith
  • 3,719
  • 1
  • 25
  • 22