C++ 使用 CodeSynthesis XSD 树映射的类型
我正在使用 CodeSynthesis XSD C++/树映射实用程序将现有的 xsd 转换为我们可以填充值的 C++ 代码。这是我们始终确保遵循架构的原因。
完成转换后,我试图让它工作,以便我可以测试它。 问题是,我不习惯在 C++ 中这样做,这是我第一次使用这个工具。
我从一个名为 ABSTRACTNETWORKMODEL 的类开始,其类型为 versno_type
和 fromtime_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前也被这个咬过。 如果该行:
与代码中显示的完全相同(即带有括号),则问题在于您实际上并未像您想象的那样实例化对象。
要实例化一个对象,您可以使用
第一行,相反,声明一个函数 t(),它不带参数并返回
::xml_schema::time
类型的对象。 由于没有主体,编译器认为您稍后将定义该函数。 它是完全合法的 C++,人们经常这样做(例如,在头文件中),因此编译器会接受它,不会发出警告,因为它无法知道这不是您的意思,并且做了您不想要的事情不期待。当您将该函数传递给
ABSTRACTNETWORKMODEL
构造函数时,您会收到错误,因为您无法将函数作为参数传递(您可以传递指向该函数的指针,并且可以调用该函数,传递结果临时):所以“时间的实例化没有导致错误”的原因是时间对象从未被实例化。 time 类也没有默认构造函数,尝试使用正确的语法实例化 t 会引发您期望的错误。
为了记录在案,在某些情况下需要括号。 例如,当实例化一个临时对象并在同一行中操作该临时对象时:
因为删除第一组括号将导致错误:
相信我,我真的很喜欢 C++,但有时语法可能很难保持正确。
I've been bitten by this before. If the line:
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
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):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:
Because dropping the first set of parenthesis will result in an error:
Believe me, I really like C++, but the syntax can get really difficult to keep straight some times.
在办公室询问,看来我的问题不是创建 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.