在#define长度的结构中声明一个数组 [英] declaring an array in a structure with a #define length

查看:78
本文介绍了在#define长度的结构中声明一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i我正在尝试制作一个结构


#include" globalVars.h"


struct myStruct {

int offset;

unsigned char uChars [numOfGlobalVars -1];


} saveVars,getVars;

我在globalVars.h中有#define numOfGlobalVars ...它没有


main.cpp: 23:错误:''struct myStruct''没有名为''uChars''的成员,

当我稍后在我的代码中尝试访问这些uChars时。


我应该不能这样做吗?


nass

解决方案

在************** @ gmail.com 写道:


>大家好,
我试图建造一个结构


> #include" globalVars.h"


> struct myStruct {

int offset;

unsigned char uChars [numOfGlobalVars - 1];


>} saveVars,getVars;


>我在globalVars.h中有#define numOfGlobalVars ...它没有编译


它对我有用。我建议你发一个最小但完整的例子。



在************** @ gmail.com 写道:


大家好,

i我试图建立一个结构


#include" globalVars.h"


struct myStruct {

int offset;

unsigned char uChars [numOfGlobalVars -1];


} saveVars,getVars;


我在globalVars.h中有#define numOfGlobalVars ...它没有


main.cpp:23 :错误:''struct myStruct''没有名为''uChars''的成员,

当我稍后在我的代码中尝试访问这些uChars时。


我应该不能这样做吗?


nass



它对我有用。告诉我们更多关于您使用的编译器

的版本和内容。


帮派。


你好再次

g ++ 3.3.6 slackware linux上的编译器但是

i我正在使用QtDesigner的qmake实用程序生成makefile,

然后我只输入make ...

仍然没有在这部分程序中使用qlibraries ...

这里我发送你的fileIO .h和fileIO.cpp以及globalVars.h

和我用来测试fileIO文件的main.cpp:


/ * - ----------- globalVars.h ------------------------------------ ---------------------- * /


#ifndef GLOBALVARS_H

#define GLOBALVARS_H


int appDateTimeOffset;

unsigned char operationalState; // 0 =自动,1 =手动,

2 =预定,3 =记录

unsigned char logSamplingPeriod; // 0 =关闭,1 = 1分钟,2 = 10分钟,

3 = 30分钟,4 = 1小时

unsigned char logType; // 0 =循环,1 =线性

unsigned char changeLog; // 0 = per1min,1 = per1hour,

2 = per1day


#endif // GLOBALVARS_H

/ * - ---------- fileIO.h ------------------------------------- --------------------- * /


#ifndef FILEIO_H

#define FILEIO_H


void writeToFile(const void * buffer,const int& unitSize,const char

* filename =" ../ globalsFile.txt");


int readFromFile(void * buffer,const long bufSize,const char

* filename =" ../ globalsFile.txt");


#endif // FILEIO_H


/*------------fileIO.cpp-------- -------------------------------------------------- * /


#include" fileIO.h"

#include< stdio.h>


void writeToFile(const void * buffer,const int& unitSize,const char

* filename)

{

FILE * pFile;

pFile = fopen(filename," wt");

fwrite(buffer,unitSize,1,pFile);

fcl ose(pFile);


}


int readFromFile(void * buffer,const long bufSize,const char * filename)

{

FILE * pFile;

pFile = fopen(filename," rt");


if (pFile == NULL)return(0);


fread(buffer,bufSize,1,pFile);


fclose(pFile) ;


返回(1);

}


/ * --------- --- main.cpp中-------------------------------------------- -------------- * /


#include" fileIO.h"

#include" .. /globalVars.h"

#include< iostream.h>


struct myStruct {

int offset;

// unsigned char uChars [numOfGlobalVars-1];

unsigned char uChar1;

unsigned char uChar2;

unsigned char uChar3;

unsigned char uChar4;

} saveVars,getVars;


int main()

{

//初始化全局变量

appDateTime Offset = 99;

operationalState = 96;

logSamplingPeriod = 101;

logType = 75;

changeLog = 69;


//用globalVariables值填充结构

saveVars.offset = appDateTimeOffset;

saveVars.uChar1 = operationalState;

saveVars.uChar2 = logSamplingPeriod;

saveVars.uChar3 = logType;

saveVars.uChar4 = changeLog;


writeToFile(& saveVars,sizeof(saveVars));


//重新启动globalVars

appDateTimeOffset = 0;

operationalState = 0;

logSamplingPeriod = 0;

logType = 0;

changeLog = 0;


if(readFromFile(& getVars,sizeof(getVars)))

cout<<" all OK"<< endl;


//用readFromFile值重新填充globalVars

appDateTimeOffset = getVars.offset;

operationalState = getVars.uChar1;

logSamplingPeriod = getVars.uChar2;

logType = getVars.uChar3;

changeLog = getVars.uChar4;


cout<< appDateTimeOffset<< endl;

cout< ;< operationalState<< endl;

cout<< logSamplingPeriod<< endl;

cout<< logType<< endl;

cout<< changeLog<< endl;


} // main


/ * ------- - - - - - - - - - - - 结束 - - - - - - - - - - - - - - ---------------------------------- * /


i已切换在

中为4个独立的条目(uCha​​r#声明)命令使它工作...但那就是它..如果你想要

编译它只是改变uChar#到uChars [#] ..

感谢您的帮助。

nass

gangs写道:

在************** @ gmail.com 写道:


大家好,

i我试图建立一个结构


#include" globalVars.h"

struct myStruct {

int offset;

unsigned char uChars [numOfGlobalVars -1];


} saveVars,getVars;

我在globalVars.h中有#define numOfGlobalVars ...它没有

编译说


main.cpp:23:错误:''struct myStruct''没有名为''uChars''的成员,

,当我稍后在我的代码中尝试访问这些uChars时。


我应该不能这样做吗?


nass



它对我有用。告诉我们更多关于你用过的编译器

的版本和内容。


帮派。


Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: ''struct myStruct'' has no member named ''uChars'',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass

解决方案

at**************@gmail.com writes:

>Hello everyone,
i am attempting to make a structure

>#include "globalVars.h"

>struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

>} saveVars, getVars;

>and i have #define numOfGlobalVars in globalVars.h... it does not
compile

It works for me. I suggest you post a minimal but complete example.



at**************@gmail.com wrote:

Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: ''struct myStruct'' has no member named ''uChars'',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass

It worked for me. Tell us a little more about the compiler you''ve used
the version and stuff.

gangs.


hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
/*------------fileIO.h----------------------------------------------------------*/

#ifndef FILEIO_H
#define FILEIO_H

void writeToFile(const void *buffer,const int &unitSize,const char
*filename="../globalsFile.txt");

int readFromFile(void *buffer,const long bufSize,const char
*filename="../globalsFile.txt");

#endif //FILEIO_H

/*------------fileIO.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include <stdio.h>

void writeToFile(const void *buffer,const int &unitSize,const char
*filename)
{
FILE * pFile;
pFile = fopen (filename,"wt");
fwrite(buffer,unitSize,1,pFile);
fclose(pFile);

}

int readFromFile(void *buffer,const long bufSize,const char *filename)
{
FILE * pFile;
pFile = fopen (filename,"rt");

if (pFile==NULL) return (0);

fread (buffer,bufSize,1,pFile);

fclose (pFile);

return (1);
}

/*------------main.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include "../globalVars.h"
#include <iostream.h>

struct myStruct{
int offset;
//unsigned char uChars[numOfGlobalVars-1];
unsigned char uChar1;
unsigned char uChar2;
unsigned char uChar3;
unsigned char uChar4;
} saveVars, getVars;

int main()
{
//init the global variables
appDateTimeOffset=99;
operationalState=96;
logSamplingPeriod=101;
logType=75;
changeLog=69;

//fill up the structure with the globalVariables values
saveVars.offset=appDateTimeOffset;
saveVars.uChar1=operationalState;
saveVars.uChar2=logSamplingPeriod;
saveVars.uChar3=logType;
saveVars.uChar4=changeLog;

writeToFile(&saveVars,sizeof(saveVars));

//reinit the globalVars
appDateTimeOffset=0;
operationalState=0;
logSamplingPeriod=0;
logType=0;
changeLog=0;

if (readFromFile(&getVars,sizeof(getVars)))
cout<<"all OK"<<endl;

//refill the globalVars with the readFromFile values
appDateTimeOffset=getVars.offset;
operationalState=getVars.uChar1;
logSamplingPeriod=getVars.uChar2;
logType=getVars.uChar3;
changeLog=getVars.uChar4;

cout<<appDateTimeOffset<<endl;
cout<<operationalState<<endl;
cout<<logSamplingPeriod<<endl;
cout<<logType<<endl;
cout<<changeLog<<endl;

}//main

/*-----------------------------END-------------------------------------------------------------*/

i have switched to 4 independent entries (the uChar# declarations) in
order to make it work... but thats pretty much it.. if you want to
compile it just change the uChar# to uChars[#]..
Thank you for your help.
nass
gangs wrote:

at**************@gmail.com wrote:

Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: ''struct myStruct'' has no member named ''uChars'',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass


It worked for me. Tell us a little more about the compiler you''ve used
the version and stuff.

gangs.


这篇关于在#define长度的结构中声明一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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