我无法理解这行从源代码代码在github上:这行代码在C++中意味着什么?
using NodePtr = std::shared_ptr<Node>;
我读了cppreference页面here,但它没有关于类似的语法的任何信息。尽我所能猜到,它有点像#define
,因为从现在开始我使用NodePtr
时,它将在内部用std::shared_ptr<Node>
代替它。有了这个,我试图测试代码,但它没有奏效。
代码:
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;
}
以下是错误我得到:
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();
^
当然有。这是[底部的第三个链接](http://en.cppreference.com/w/cpp/language/type_alias)。 – Rakete1111
这是一个别名。您可以在代码中使用'NodePtr'而不是'std :: shared_ptr'。 –
Azeem
请阅读第一条错误消息并按照说明进行操作。 – molbdnilo