2011-06-14 66 views
0

我是新來的C++ /解析,我將xsd文件轉換爲自動生成的cpp和頭文件。當我嘗試訪問xml文件(xmlfilename)的元素(成員函數)時,出現錯誤「Primary-expression missing before' - >'token'。C++ /分析:錯誤:預期主要表達式之前' - >'令牌。如何解決此錯誤..?

我的代碼:

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

編譯器輸出:

[email protected]:~/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 

誰能解釋一下什麼是主表達錯誤?我該如何解決這個問題?

更多細節:

#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()); 

} 

我不知道在哪裏我錯了,請一些一個導向me..Thanks提前

+0

哪裏是'n()'聲明? – iammilind 2011-06-14 05:32:39

+0

':: SelftTest :: MH'和':: SelfTest :: MH_'有什麼區別?它不應該是:: :: std :: auto_ptr <:: SelfTest :: MH> xsd(new :: SelfTest :: MH(xmlFilename,xml_schema :: flags :: dont_validate));'? – 2011-06-14 05:32:49

+0

沒有足夠的信息。 – 2011-06-14 05:34:46

回答

1

當您離開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的操作可保證不會拋出。所以,相反,如果你想限制你的try塊的範圍,你可以這樣做,以及:

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。

+0

非常感謝..thats我真的犯了錯誤.. – Balamurugan 2011-06-14 08:01:26

+0

@Bala Don如果它爲你解決,你也不要忘記你也可以接受答案。 – greatwolf 2011-06-14 08:07:00

2

我願意打賭,這不是唯一的錯誤你得到了。始終處理編譯器錯誤,從列表中的第一個錯誤開始;隨後的錯誤可能只是早期錯誤的副作用。

在這種情況下,編譯器顯然不知道xsd在你已經突出顯示的行上。這表明編譯器xsd的聲明有問題,所以請將注意力集中在針對該行報告的編譯器錯誤上。請記住,實際的錯誤可能更早,例如xsd聲明之前的一行中被遺忘的分號。或者,編譯器可能無法識別您在該行上提到的類型之一。 auto_ptr類來自內存標題;你有包括嗎?當您提到MH作爲auto_ptr類型參數時,可能您省略了下劃線。

+0

我添加更多詳細信息請看看 – Balamurugan 2011-06-14 06:45:57