C++ 使用 CodeSynthesis XSD 树映射的类型

发布于 2024-07-06 10:05:04 字数 1122 浏览 7 评论 0原文

我正在使用 CodeSynthesis XSD C++/树映射实用程序将现有的 xsd 转换为我们可以填充值的 C++ 代码。这是我们始终确保遵循架构的原因。

完成转换后,我试图让它工作,以便我可以测试它。 问题是,我不习惯在 C++ 中这样做,这是我第一次使用这个工具。

我从一个名为 ABSTRACTNETWORKMODEL 的类开始,其类型为 versno_typefromtime_type 里面有 typedef。 这是我尝试使用的构造函数以及 typedef,

ABSTRACTNETWORKMODEL(const versno_type&, const fromtime_type&);
typedef ::xml_schema::double_ versno_type;
typedef ::xml_schema::time fromtime_type;

所有这些都在 ABSTRACTNETWORKMODEL 类中, double_ 和 time 的定义是:

typedef ::xsd::cxx::tree::time<char, simple_type> time;
typedef double double_;

其中 time 的定义是具有多个构造函数的类:

template<typename C, typename B>
class time: public B, public time_zone
{
  public:
  time(unsigned short hours, unsigned short minutes, double seconds);
  ...
}

我知道我没有正确创建一个新的抽象网络模型,但我不知道我需要什么来做到这一点。 这就是我此时要做的所有事情:

  ::xml_schema::time t();
  ABSTRACTNETWORKMODEL anm(1234, t);

当然,这会引发有关转换第二个参数的错误,但是有人可以告诉我什么是不正确的吗? 或者至少为我指明了正确的道路,因为我现在想做的事情之一就是学习更多 C++。

I'm using CodeSynthesis XSD C++/Tree Mapping utility to convert an existing xsd into c++ code we can populate the values in. This was we always make sure we follow the schema.

After doing the conversion, I'm trying to get it to work so I can test it. Problem is, I'm not used to doing this in c++ and it's my first time with this tool.

I start with a class called ABSTRACTNETWORKMODEL with types versno_type and fromtime_type
typedef'd inside. Here is the constructor I am trying to use as well as the typedefs

ABSTRACTNETWORKMODEL(const versno_type&, const fromtime_type&);
typedef ::xml_schema::double_ versno_type;
typedef ::xml_schema::time fromtime_type;

all these are in the ABSTRACTNETWORKMODEL class and the definitions for double_ and time are:

typedef ::xsd::cxx::tree::time<char, simple_type> time;
typedef double double_;

where the definition for time is a class with multiple constructors:

template<typename C, typename B>
class time: public B, public time_zone
{
  public:
  time(unsigned short hours, unsigned short minutes, double seconds);
  ...
}

I know I'm not correctly creating a new ABSTRACTNETWORKMODEL but I don't know what I need to do this. Here is all I'm trying to do at this point:

  ::xml_schema::time t();
  ABSTRACTNETWORKMODEL anm(1234, t);

This, of course, throws an error about converting the second parameter, but can somebody tell me what it is that is incorrect? Or at least point me down the right path, as one of the things I'm trying to do right now is learn more c++.

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

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

发布评论

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

评论(2

[旋木] 2024-07-13 10:05:04

我以前也被这个咬过。 如果该行:

::xml_schema::time t();

与代码中显示的完全相同(即带有括号),则问题在于您实际上并未像您想象的那样实例化对象。

要实例化一个对象,您可以使用

::xml_schema::time t;

第一行,相反,声明一个函数 t(),它不带参数并返回 ::xml_schema::time 类型的对象。 由于没有主体,编译器认为您稍后将定义该函数。 它是完全合法的 C++,人们经常这样做(例如,在头文件中),因此编译器会接受它,不会发出警告,因为它无法知道这不是您的意思,并且做了您不想要的事情不期待。

当您将该函数传递给 ABSTRACTNETWORKMODEL 构造函数时,您会收到错误,因为您无法将函数作为参数传递(您可以传递指向该函数的指针,并且可以调用该函数,传递结果临时):

::xml_schema::time t();
ABSTRACTNETWORKMODEL anm(1234, t()); // calls t(), gets a temporary of type ::xml_schema::time, and passes the temporary to the constructor

所以“时间的实例化没有导致错误”的原因是时间对象从未被实例化。 time 类也没有默认构造函数,尝试使用正确的语法实例化 t 会引发您期望的错误。

为了记录在案,在某些情况下需要括号。 例如,当实例化一个临时对象并在同一行中操作该临时对象时:

int hours = time().get_hours(); // assuming that there is now a default constructor

因为删除第一组括号将导致错误:

int hours = time.set_time("12:00:00am"); // error: there is a time class, but no object named "time"

相信我,我真的很喜欢 C++,但有时语法可能很难保持正确。

I've been bitten by this before. If the line:

::xml_schema::time t();

is exactly as it appears in your code (that is, with the parens) then the problem is that you didn't actually instantiate an object like you think.

To instantiate an object you would use

::xml_schema::time t;

The first line, instead, declares a function t() that takes no arguments and returns an object of type ::xml_schema::time. Since there is no body, the compiler thinks you will define the function later. It is perfectly legal C++, and it's something that people do a lot (say, in header files) so the compiler accepts it, does not issue a warning because it has no way of knowing that's not what you meant, and does something you weren't expecting.

And when you pass that function to the ABSTRACTNETWORKMODEL constructor you get an error because you can't pass a function as an argument (you can pass a pointer to the function, and you can call the function, passing the resulting temporary):

::xml_schema::time t();
ABSTRACTNETWORKMODEL anm(1234, t()); // calls t(), gets a temporary of type ::xml_schema::time, and passes the temporary to the constructor

So the reason "the instantiation of time didn't cause an error" is that a time object was never instantiated. The time class doesn't have a default constructor either, and attempting to instantiate t with the correct syntax would have thrown the error you were expecting.

For the record, the parenthesis are required in some cases. For instance, when instantiating a temporary object and manipulating that temporary in the same line:

int hours = time().get_hours(); // assuming that there is now a default constructor

Because dropping the first set of parenthesis will result in an error:

int hours = time.set_time("12:00:00am"); // error: there is a time class, but no object named "time"

Believe me, I really like C++, but the syntax can get really difficult to keep straight some times.

桃扇骨 2024-07-13 10:05:04

在办公室询问,看来我的问题不是创建 ABSTRACTNETWORKMODEL,但实际上是 ::xml_schema::time。

我觉得奇怪的是,时间的实例化没有导致错误,因为它没有任何默认构造函数,或者为什么即使模板和类型正确,它也没有被接受。

Asked around the office, and it appears my problem wasn't creating the ABSTRACTNETWORKMODEL, but it was actually the ::xml_schema::time.

I find it odd that the instantiation of time didn't cause an error, given that it doesn't have any default constructors or why it wasn't accepted even though the template and types were correct.

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