2013-07-21 21 views
4

我已經嘗試了很多次來解決這個問題,但我什麼都沒有。錯誤:預期初始表達式''''標記

此代碼的主要目的是將嵌套類NslObject::KeyKNewKeyPair1中的配對密鑰(公共和私有)保存。

.cpp文件

 unsigned long int keyLength = 10; 
     //KeyPair ADD(RSA::GenerateKeyPair(keyLength)); 
     NslObject::KeyK(RSA::GenerateKeyPair(keyLength)); 
     typedef NslObject::KeyK NewKeyPair1; 
     NewKeyPair1(RSA::GenerateKeyPair(keyLength)); 

     //NslObject:: 
     Key OtmanK(NslObject::Get_PublicKey(NewKeyPair1)); 

.h文件中:

#ifndef __NCTUNS_nslobject_h__ 
#define __NCTUNS_nslobject_h__ 

#include <stdio.h> 
#include <event.h> 

//--------------------------------------------------- 
#include <cstdlib>  //srand() 
#include <iostream>  //cout 
#include <ctime>  //time() 
#include <cstring>  //strcmp() 
//#include "test.h"  //testing functions 
#include "RSA.h"  //GenerateKeyPair() 
#include "PrimeGenerator.h"  //Generate() 
//#include <stdio.h> 
#include <stdlib.h> 
#include <sstream> 
#include <string> 


//--------------------------------------------------- 

class MBinder; 

    struct plist { 
u_int8_t  pid; 
struct plist   *next; 
}; 

    struct MBlist { 
u_int8_t portnum; 
MBinder  *sendt; 
struct MBlist *next; 
}; 
/*========================================================================= 
Define Macros 
=========================================================================*/ 
#define DISABLED  0x00 
#define ENABLED   0x01 


/*========================================================================= 
Define Class ProtoType 
=========================================================================*/ 

class NslObject { 

private: 

char  *name_;  /* Instance name */ 
const u_int32_t nodeID_; /* Node Id */ 
const u_int32_t nodeType_; /* Node type, eg: SWITCH, HOST.. */ 
u_int32_t portid_; /* port Id */ 
struct plist *MPlist_; 
    // static KeyPair NewKeyPair; 


    public : 
/* add for new structure engine*/ 
    u_int32_t  pdepth; 
    struct MBlist *BinderList; 
    u_int8_t  PortNum; 

//------------------------------------------------ 
    class KeyK { 
    private : 
     KeyPair Kk; 

    public: 

     KeyK(KeyPair D1): Kk(D1) 
     {} 

    const Key &GetPrivateKey() const 
      { 
        return Kk.GetPrivateKey(); 
      } 

    const Key &GetPublicKey() const 
      { 
        return Kk.GetPublicKey(); 
      } 

     //KeyK NewKeyPair ; 


    }; 

    Key Get_PrivateKey(KeyK &K){ 
     return K.GetPrivateKey(); 
    } 
     Key Get_PublicKey(KeyK &K){ 
     return K.GetPublicKey(); 
    } 


    //static KeyPair *KeyPtr1 ;//= new KeyPair; 
    //------------------------------------------------ 
u_char   s_flowctl;  /* flow control for sending pkt */ 
u_char   r_flowctl;  /* flow control for receiving pkt */ 

MBinder  *recvtarget_; /* to upper component */ 
MBinder  *sendtarget_; /* to lower component */ 


NslObject(u_int32_t, u_int32_t, struct plist*, const char *); 
NslObject(); 
virtual   ~NslObject(); 
virtual int  init(); 
virtual int  recv(ePacket_ *); 
virtual int  send(ePacket_ *); 
virtual int  get(ePacket_ *, MBinder *); 
virtual int  put(ePacket_ *, MBinder *); 
virtual ePacket_ *put1(ePacket_ *, MBinder *); 
virtual int  command(int argc, const char *argv[]); 
virtual int  Debugger(); 


inline void set_port(u_int32_t portid) { 
     portid_ = portid; 
    }; 
inline u_int32_t get_port() const { 
     return(portid_); 
    }; 
inline struct plist* get_portls() const { 
     return(MPlist_); 
    }; 
inline const char * get_name() const { 
     return(name_); 
    } 
inline u_int32_t get_nid() const { 
     return(nodeID_); 
    } 
inline u_int32_t get_type() const { 
     return(nodeType_); 
    } 
    //-------------------------------------------------------- 





    }; 

所以,在這條線在CPP文件中的問題:

Key OtmanK(NslObject::Get_PublicKey(NewKeyPair1)); 

當我試圖編譯這個項目,我得到了這個Erro r留言:

object.cc:87: error: expected primary-expression before ‘)’ token 

請幫幫我。

當我試圖使定向呼叫爲重點班,另一個問題出現了,如:

 unsigned long int keyLength = 10; 
     //KeyPair ADD(RSA::GenerateKeyPair(keyLength)); 
     NslObject::KeyK(RSA::GenerateKeyPair(keyLength)); 
     typedef NslObject::KeyK NewKeyPair1; 
     NewKeyPair1(RSA::GenerateKeyPair(keyLength)); 

     //NslObject:: 
     //Key OtmanK(NewKeyPair1.GetPublicKey()); 
     std::string message = "othman Alkilany"; 
     // NslObject::NewKeyPair.GetPrivateKey() 
     std::string cypherText = RSA::Encrypt( message, NewKeyPair1.GetPublicKey()); 

的錯誤信息是:

error: expected primary-expression before ‘.’ token 
+0

dup? http://stackoverflow.com/questions/17697337/how-can-assign-a-class-value-to-another-nested-classes-in-c/17697417#17697417 – billz

+3

歡迎來到stackoverflow。如果你減少你的問題,你更有可能得到有意義的答案。一個建議可能是有選擇地刪除/註釋部分代碼,直到你到達最小的例子來重現你的問題。 –

回答

2

我猜你的問題是在第二行在這裏:

typedef NslObject::KeyK NewKeyPair1; 
NewKeyPair1(RSA::GenerateKeyPair(keyLength)); 

解決typedef你基本上寫了以下內容:

NslObject::KeyK(RSA::GenerateKeyPair(keyLength)); 

因此,您可以清楚地看到您缺少此行的變量名稱。

爲了解決這個問題,命名變量/對象:

NewKeyPair1 somekeypair(RSA::GenerateKeyPair(keyLength)); 

不過,我猜你濫用了typedef呢?你是否試圖命名NewKeyPair1?類似這樣的:

NslObject::KeyK NewKeyPair1(RSA::GenerateKeyPair(keyLength)); 
+0

是的,這真的很有用。謝謝各位的幫助.... – user2590797

相關問題