2013-07-17 38 views
0

這是第一次問我關於Stackoverflow的問題。我qustion是我得到當我試圖運行此代碼此消息:如何在C++中爲另一個嵌套類指派一個類值

object.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_; 
    //const KeyPair newKeyPair; 


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

//------------------------------------------------ 
    static KeyPair newKeyPair ; 
    //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_); 
    } 
    //-------------------------------------------------------- 

    static void Set_KeyPair(void); 

// NslObject(const KeyPair &newKeyPair): 
// newKeyPair(newKeyPair) { 
// } 
//NslObject(KeyPair &other, u_int32_t &N_ID, u_int32_t &N_Type) : newKeyPair(other),nodeID_  (N_ID),nodeType_ (N_Type) {} 



/* Key(const BigInt &modulus, const BigInt &exponent) : 
modulus(modulus), exponent(exponent) { 
}*/ 

}; 


/* Added by CCLin to uniformly format 
* debugging messages. 
*/ 

#define NSLOBJ_DEBUG_STRING_HEAD() do {    \ 
double sec;      \ 
TICK_TO_SEC(sec, GetCurrentTime());   \ 
printf("%11.7f: [%03d]%s::%s: ",   \ 
     sec, get_nid(),    \ 
     getModuleName(this), __FUNCTION__); \ 
} while(0) 

#endif /* __NCTUNS_object_h__ */ 

在Object.cc

void Set_KeyPair() 
{ 
unsigned long int keyLength = 10; 
//static KeyPair ADD(RSA::GenerateKeyPair(keyLength)); 
NslObject::newKeyPair (RSA::GenerateKeyPair(keyLength)); 

//NslObject::KeyPtr 
//NslObject::newKeyPair; 
} 

所以,我的問題是,當編譯器編譯開始它停在此代碼:

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

和出現的消息是:

Error: 
object.cc: In function ‘void Set_KeyPair()’: 
object.cc:53: error: no match for call to ‘(KeyPair) (KeyPair&)’ 

所以,我怎麼能分配一個生成的密鑰對類值到NslObject :: newKeyPair。

您的幫助是高度讚賞&謝謝你。

+1

嘗試聲明一個變量? NslObject :: newKeyPair var(RSA :: GenerateKeyPair(keyLength)); –

+0

什麼是RSA :: GenerateKeyPair的返回類型? –

+1

@DmitrySazonov:不,newKeyPair是一個成員變量,而不是一個類型。 –

回答

0

如果你想用一個新的值賦給它,那麼你需要使用賦值:

NslObject::newKeyPair = RSA::GenerateKeyPair(keyLength); 

你的代碼試圖調用它,而不是一個功能,具有不起作用,因爲它不一個函子。

(同樣,你不應該使用reserved names爲您include防範。)

+0

感謝您的快速回放...... RSA :: GenerateKeyPair的返回類型是KeyPair類。 – user2590797

+0

哎呀!當我試過這段代碼:NslObject :: newKeyPair = RSA :: GenerateKeyPair(keyLength);我得到這個消息:./KeyPair.h:39:錯誤:非靜態const成員'const Key KeyPair :: privateKey',不能使用默認賦值操作符???? – user2590797

+0

@ user2590797:這是指你沒有發佈的代碼,所以我只能猜測;但它看起來像你試圖分配給一個'const'變量。 'const'意味着你不能修改它。 –

相關問題