yaml-cpp 出现编译错误

发布于 2024-09-06 20:46:51 字数 8339 浏览 9 评论 0原文

我正在尝试将 yaml-cpp 集成到项目中,但我看到 GCC 出现一些意外错误。举

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 中的一个问题,但经验告诉我,我可能是错的人。有什么想法吗?

更新

文件 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;
        }
}

I'm attempting to integrate yaml-cpp into a project, but I'm seeing some unexpected errors out of GCC. For example:

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

Another example:

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’

My platform is Fedora (2.6.32 kernel), GCC 4.4.1, and yaml-cpp 0.2.5. There are many, many other errors. To the naked eye, this seems like a problem within yaml-cpp, but experience tells me I'm probably the one at fault. Any ideas?

UPDATE

The file traits.h contains the following:

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> {};
}

And nodeutil.h contains:

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;
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-09-13 20:46:51

你似乎在里面包括地狱。您发布的 Traits.h 文件不包含“(”,但 gcc 会抱怨“(”之前的内容。这可能意味着您包含的内容具有与此文件中的内容同名的宏。尝试重新排序包含在您的项目中,看看它是否有什么不同。

You seem to be inside include hell. The traits.h file you posted contains no '(', but gcc complains about what comes before a '('. That might mean that you are including something that has a macro with the same name as something in this file. Try reordering the includes in your project and see if it make any difference.

蓝戈者 2024-09-13 20:46:51

我建议您尝试删除“使用命名空间 X;” - 特别是当 X == YAML 时。这样您的错误消息可能会变得更有洞察力 - 因为看起来您遇到了名称冲突。另一个技巧是查看预处理器输出的内容 - 使用 G++,您可以使用 -E (而不是 -c)运行编译行并将输出通过管道传输到文件。然后您可以看到编译器正在获取什么(而不是原始源代码,或者预处理器正在获取什么)。

I would suggest you try removing "using namespace X;" - particularly where X == YAML. Your error messages may get more insightful that way - as it looks like you're having name clashes. Another trick is to see what the preprocessor is spitting out - with G++ you can run your compile line with -E (instead of -c) and pipe the output to a file. Then you can see what the compiler is getting (rather then the original source, or what the preprocessor is getting).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文