我正在嘗試將yaml-cpp集成到一個項目中,但我看到一些GCC出現的意外錯誤。例如:編譯器錯誤出現yaml-cpp
g++ -c -ggdb3 -ansi -Wall -Werror -pedantic-errors src/commands-tz.cpp -o obj/commands-tz.o
In file included from /usr/local/include/yaml-cpp/conversion.h:9,
from /usr/local/include/yaml-cpp/node.h:8,
from /usr/local/include/yaml-cpp/parser.h:8,
from /usr/local/include/yaml-cpp/yaml.h:8,
from src/note.h:26,
from src/commands-tz.cpp:297:
/usr/local/include/yaml-cpp/traits.h:26: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:26: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:26: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:26: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:31: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:31: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:31: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:31: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:34: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:34: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:34: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:34: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:37: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:37: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:37: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:37: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:42: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:42: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:42: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:42: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:45: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:45: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:45: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:45: error: expected unqualified-id before ‘)’ token
又如:
In file included from /usr/local/include/yaml-cpp/nodeimpl.h:8,
from /usr/local/include/yaml-cpp/node.h:139,
from /usr/local/include/yaml-cpp/parser.h:8,
from /usr/local/include/yaml-cpp/yaml.h:8,
from src/note.h:26,
from src/commands-tz.cpp:297:
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected nested-name-specifier before ‘(’ token
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected nested-name-specifier before ‘(’ token
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected nested-name-specifier before ‘(’ token
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/nodeutil.h:24: error: ‘is_index_type_with_check’ is not a template
/usr/local/include/yaml-cpp/nodeutil.h:24: error: explicit specialization of non-template ‘YAML::is_index_type_with_check’
我的平臺是Fedora的(2.6.32內核),GCC 4.4.1,和YAML-CPP 0.2.5。還有很多很多其他的錯誤。肉眼看來,這似乎是yaml-cpp中的一個問題,但經驗告訴我,我可能是有過錯的人。有任何想法嗎?
UPDATE
文件traits.h包含以下內容:
namespace YAML
{
template <typename>
struct is_numeric { enum { value = false }; };
template <> struct is_numeric <char> { enum { value = true }; };
template <> struct is_numeric <unsigned char> { enum { value = true }; };
template <> struct is_numeric <int> { enum { value = true }; };
template <> struct is_numeric <unsigned int> { enum { value = true }; };
template <> struct is_numeric <long int> { enum { value = true }; };
template <> struct is_numeric <unsigned long int> { enum { value = true }; };
template <> struct is_numeric <short int> { enum { value = true }; };
template <> struct is_numeric <unsigned short int> { enum { value = true }; };
template <> struct is_numeric <long long> { enum { value = true }; };
template <> struct is_numeric <unsigned long long> { enum { value = true }; };
template <> struct is_numeric <float> { enum { value = true }; };
template <> struct is_numeric <double> { enum { value = true }; };
template <> struct is_numeric <long double> { enum { value = true }; };
template <bool, class T = void>
struct enable_if_c {
typedef T type;
};
template <class T>
struct enable_if_c<false, T> {};
template <class Cond, class T = void>
struct enable_if : public enable_if_c<Cond::value, T> {};
template <bool, class T = void>
struct disable_if_c {
typedef T type;
};
template <class T>
struct disable_if_c<true, T> {};
template <class Cond, class T = void>
struct disable_if : public disable_if_c<Cond::value, T> {};
}
而且nodeutil.h包含:
namespace YAML
{
template <typename T, typename U>
struct is_same_type {
enum { value = false };
};
template <typename T>
struct is_same_type<T, T> {
enum { value = true };
};
template <typename T, bool check>
struct is_index_type_with_check {
enum { value = false };
};
template <> struct is_index_type_with_check<std::size_t, false> { enum { value = true }; };
#define MAKE_INDEX_TYPE(Type) \
template <> struct is_index_type_with_check<Type, is_same_type<Type, std::size_t>::value> { enum { value = true }; }
MAKE_INDEX_TYPE(int);
MAKE_INDEX_TYPE(unsigned);
MAKE_INDEX_TYPE(short);
MAKE_INDEX_TYPE(unsigned short);
MAKE_INDEX_TYPE(long);
MAKE_INDEX_TYPE(unsigned long);
#undef MAKE_INDEX_TYPE
template <typename T>
struct is_index_type: public is_index_type_with_check<T, false> {};
// messing around with template stuff to get the right overload for operator [] for a sequence
template <typename T, bool b>
struct _FindFromNodeAtIndex {
const Node *pRet;
_FindFromNodeAtIndex(const Node&, const T&): pRet(0) {}
};
template <typename T>
struct _FindFromNodeAtIndex<T, true> {
const Node *pRet;
_FindFromNodeAtIndex(const Node& node, const T& key): pRet(node.FindAtIndex(static_cast<std::size_t>(key))) {}
};
template <typename T>
inline const Node *FindFromNodeAtIndex(const Node& node, const T& key) {
return _FindFromNodeAtIndex<T, is_index_type<T>::value>(node, key).pRet;
}
}
導致錯誤的行包含哪些代碼?(圍繞traits.h中的第26行和nodeutil.h中的第9行) – Kleist 2010-06-27 21:10:24
你似乎在內部包含地獄。你發佈的traits.h文件包含* no *'(',但是gcc抱怨在'('之前發生了什麼,這可能意味着你包含一些與這個文件中的名字相同的宏。在您的項目中重新排序包含,看看它是否有任何區別 – Gianni 2010-06-27 23:40:21
@Gianni:這是包括地獄好吧,我不知道這是如何工作,但文件「note.h」包括。在一個實現中tation文件[src/commands-tz.cpp]我#include「note.h」和GCC死了。如果我添加(包含*之前的*)一個#include ,那麼一切工作都會變得很花哨。我無法弄清楚,但感謝您的建議! (轉貼爲答覆,所以我可以標記它?) –
2010-08-02 10:41:24