错误C2099:初始化不是一个常数 [英] error C2099: initializer is not a constant

查看:349
本文介绍了错误C2099:初始化不是一个常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct sDevice_d
{
    char name[24];
    signed int (*Send)(unsigned char*, unsigned short);
    signed int (*Recv)(unsigned char*, unsigned short);
} sDevice_d, *psDevice_d;

Device.c

#include "Protocol.h"

sDevice_d sDevice = { "ten", I2c_Send };
psDevice_d psDevice = &sDevice;

static signed int I2c_Send(unsigned char* buf, unsigned short len)
{
    return 0;
}

在code上面我收到以下错误:

In the code above I am getting the following Error:

错误C2099:初始化不是一个常数

error C2099: initializer is not a constant

请帮我解决这个。

我使用Visual Studio的Win32应用程序。

I am using Visual studio Win32 application.

推荐答案

被定义I2c_Send需要的功能和可视
尝试创建和初始化使用前结构
在其初始化列表。我已经包括您的code改编
下面,说明这两个文件:

The function I2c_Send needs to be defined and visible before attempting to create and initializing a struct using it in its initializer list. I have included an adaptation of your code below that illustrates this in two files:

protocol.h

protocol.h

typedef struct
{
    char name[24];
    signed int (*Send)(unsigned char*, unsigned short);
    signed int (*Recv)(unsigned char*, unsigned short);
} S_DEVICE;

//prototype here
static signed int I2c_Send(unsigned char* buf, unsigned short len); 

device.c

device.c

#include "protcol.h"

S_DEVICE sDevice_d = {"ten", I2c_Send, I2c_Send}; 

int main(void)
{
   return 0;
}

//define here
static signed int I2c_Send(unsigned char* buf, unsigned short len)
{
    return 0;
}

这个源代码编译和(使用C99扩展),并应得到使用ANCI C编译器内置
类似的结果在你的视觉工作室,Win32环境下。

This source compiled and built using an ANCI C compiler (using C99 extensions) and should yield similar results in your Visual Studios, Win32 environment.

这篇关于错误C2099:初始化不是一个常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆