c++/解析:错误:标记之前的预期主表达式'->'如何解决此错误..?
c++/Parsing: error: expected primary-expression before '->' token .how to solve this error..?
我是c++/解析的新手,我将xsd文件转换为自动生成的cpp和头文件。当我尝试访问xml文件(xmlfilename)的元素(成员函数)时,我得到错误"主表达式在'->'标记之前丢失"。
我的代码:
#include "IMACSMsgHeaderType.h"
#include<iostream>
#include<string>
#include <memory>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace SelfTest;
int main()
{
try{
::std::auto_ptr< ::SelfTest::MH > xsd (::SelfTest::MH_ ("bala.xml",::xml_schema::flags::dont_validate));
}
catch(const xml_schema::exception& e)
{
cerr<<e.what()<<endl;
}
cout<<xsd->n();
return 1;
}
编译器输出:
vmware@LSS01:~/Desktop/private> make
g++ -c test_classParsing.cpp
test_classParsing.cpp: In function ‘int main()’:
test_classParsing.cpp:26: error: expected primary-expression before ‘->’ token
make: *** [.o] Error 1
有人能解释一下什么是Primary表达式错误吗?我该如何解决这个问题?
更多详细信息:
#include <memory>
#include <algorithm>
#include <xsd/cxx/tree/exceptions.hxx>
#include <xsd/cxx/tree/elements.hxx>
#include <xsd/cxx/tree/containers.hxx>
#include <xsd/cxx/tree/list.hxx>
#include <xsd/cxx/xml/dom/parsing-header.hxx>
#include "IMACSTypes.h"
#include<iosfwd>
#include<xercesc/dom/DOMDocument.hpp>
#include<xercesc/dom/DOMErrorHandler.hpp>
#include<xercesc/framework/XMLFormatter.hpp>
#include<xsd/cxx/xml/dom/auto-ptr.hxx>
namespace SelfTest
{
class MH: public ::xml_schema::type
{
public:
// hdrSize
//
typedef ::xml_schema::unsigned_int hdrSize_type;
typedef ::xsd::cxx::tree::optional< hdrSize_type > hdrSize_optional;
typedef ::xsd::cxx::tree::traits< hdrSize_type, char > hdrSize_traits;
const hdrSize_optional&
hdrSize () const;
hdrSize_optional&
hdrSize ();
void
hdrSize (const hdrSize_type& x);
void
hdrSize (const hdrSize_optional& x);
// a
//
typedef ::SelfTest::MessageIDType a_type;
typedef ::xsd::cxx::tree::traits< a_type, char > a_traits;
const a_type&
a () const;
a_type&
a ();
void
a (const a_type& x);
void
a (::std::auto_ptr< a_type > p);
// b
//
typedef ::SelfTest::DestinationType b_type;
typedef ::xsd::cxx::tree::traits< b_type, char > b_traits;
const b_type&
b () const;
b_type&
b ();
void
b (const b_type& x);
void
b (::std::auto_ptr< b_type > p);
// n
//
typedef ::xml_schema::string n_type;
typedef ::xsd::cxx::tree::traits< n_type, char > n_traits;
const n_type&
n () const;
n_type&
n ();
void
n (const n_type& x);
void
n (::std::auto_ptr< n_type > p);
// Constructors.
MH (const a_type&,
const b_type&,
const n_type&,
);
MH (const ::xercesc::DOMElement& e,
::xml_schema::flags f = 0,
::xml_schema::container* c = 0);
MH (const MH& x,
::xml_schema::flags f = 0,
::xml_schema::container* c = 0);
virtual MH*
_clone (::xml_schema::flags f = 0,
::xml_schema::container* c = 0) const;
virtual
~MH ();
// Implementation.
//
protected:
void
parse (::xsd::cxx::xml::dom::parser< char >&,
::xml_schema::flags);
protected:
hdrSize_optional hdrSize_;
::xsd::cxx::tree::one< a_type > a_;
::xsd::cxx::tree::one< b_type > b_;
::xsd::cxx::tree::one< n_type > n_;
};
::std::auto_ptr< ::SelfTest::MH >
MH_ (const ::std::string& uri, ::xml_schema::flags f = 0,const ::xml_schema::properties& p = ::xml_schema::properties ());
}
我不知道我错在哪里了,请有人指导我。提前感谢
我敢打赌,这不是你遇到的唯一错误。始终从列表中的第一个错误开始处理编译器错误;随后的错误可能只是先前错误的副作用。
在这种情况下,编译器显然不知道您高亮显示的行上的xsd
是什么。这表明编译器在声明xsd
时也有问题,因此请将注意力集中在为该行报告的编译器错误上。请记住,实际错误可能更早,例如xsd
声明之前的行中忘记了分号。或者编译器可能无法识别您在该行中提到的类型之一。auto_ptr
类来自内存头;你包括那个吗?也许您在提到MH
作为auto_ptr
类型参数时省略了下划线。
当您离开try块时,您的xsd
将超出范围。尝试将cout
移动到这样的尝试:
int main()
{
try
{
std::auto_ptr< SelfTest::MH >
xsd( SelfTest::MH_("bala.xml", xml_schema::flags::dont_validate) );
cout << xsd->n();
}
catch(const xml_schema::exception& e)
{
cerr << e.what() << endl;
}
return 0;
}
auto_ptr的操作保证不会抛出。因此,如果你想限制你的尝试块的范围,你也可以这样做:
int main()
{
std::auto_ptr< SelfTest::MH > xsd;
try
{
xsd.reset( SelfTest::MH_("bala.xml", xml_schema::flags::dont_validate) );
}
catch(const xml_schema::exception& e)
{
cerr << e.what() << endl;
}
if( xsd.get() )
cout << xsd->n();
return 0;
}
注意,如果没有错误,main
应该返回0。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 链接器(MinGW的ld v2.23.2)错误,格式:-L<SOMEDIR>-l<lib1-l<l