2013-02-14 29 views
0

我写了以下内容,但由于某种原因调用InstructionVal(b)无效。 智能感知吐出:对无符号字符赋值无效C++

只有()被允许用于初始化成员NPPInstructionDef :: InstructionVal

这里是有问题的代码:

//Single Instruction Definition for Instruction Dictionary 
typedef struct NPPInstructionDef 
{ 
    const char* InstructionName; 
    const unsigned char* InstructionVal[]; 

    NPPInstructionDef(const char* a, const unsigned char* b[]): InstructionName(a), InstructionVal() 
    { 
    } 
}NPPInstruction; 

什么想法?谢谢。

回答

1

首先,我假设你的初始化是InstructionVal( b),而不是你写的InstructionVal()。 但即使如此,你写的不应该编译。

这是通常的问题,因为C风格数组 已损坏,不应使用。你的定义:

unsigned char const* InstructionVal[]; 

定义未知长度的unsigned char*(因此,在一类 确定指标非法的)的阵列。除了()(初始值 )之外,没有办法在初始化列表中初始化 。

你想要的是:

std::vector <unsigned char*> InstructionVal; 

,并构造应该是:

NPPInstructionDef(std::string const& a, 
        std::vector <unsigned char> const& b); 

,或者更可能的:

template <typedef Iterator> 
NPPInstructionDef(std::string const& a, 
        Iterator begin, 
        Iterator end) 
    : InstructionName(a) 
    , InstructionDef(begin, end) 
{ 
} 

(这个假设,当然,那InstructionNamestd::string,而不是char const*。这将避免任何字符串的生命期的问题,例如,并允许容易比较等)