对unsigned char C ++的赋值无效

时间:2013-02-14 19:12:04

标签: c++ compiler-errors

我写了以下但由于某种原因调用InstructionVal(b)无效。 intellisense正在吐痰:

初始化成员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 个答案:

答案 0 :(得分: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 )
{
}

(当然,这假设是InstructionName std::string代替char const*。这将避免任何 例如,字符串的生命周期问题,并允许轻松比较等。)