Ich kann diese Codezeile aus dem Quellcode auf Github verstehen:Was bedeutet diese Codezeile in C++?
using NodePtr = std::shared_ptr<Node>;
ich die cppreference Seite here, lesen, aber es hat keine Informationen über ähnliche Syntax. So viel wie ich erraten kann, ist es etwa wie #define
in dem, wenn ich von jetzt an NodePtr
verwende, wird es es intern mit std::shared_ptr<Node>
ersetzen. Damit habe ich versucht, Code zu testen, aber es hat nicht funktioniert.
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;
}
Hier ist der Fehler, den ich bekommen:
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();
^
Natürlich hat es. Es ist der [dritte Link am Ende] (http://en.cppreference.com/w/cpp/language/type_alias). – Rakete1111
Es ist ein Alias. Sie können 'NodePtr' anstelle von' std :: shared_ptr 'in Ihrem Code verwenden. –
Azeem
Lesen Sie die erste Fehlermeldung und tun Sie, was sie sagt. – molbdnilo