i have a piece of code:

struct myKeyword {
                      string name;            
                      int id;            
                };
                 
                  myKeyword keywords[]={{"include",INCLUDE},          {"void",VOID},{"main",MAIN},{"int",INT},{"float",FLOAT},{"cin",CIN},{"cout",COUT},{"if",IF},{"elseif",ELSEIF},{"else",ELSE},{"exit",EXIT},{"for",FOR},{"while",WHILE}};
           
             int j=0;
            cout<<keywords[0].id;    //not able to access keywords.id part


              flag=0;
          
      
              for (j=0;j<11;j++)                       {
                
                if (lexeme==keywords[j].name)
           
            	{                                               
            	            	  
            		temp->assign(lexeme,keywords[j].id);
           
            		flag=1;
            		break;
            	}
              }
              getchar();
            }

why cout<<keywords[0].id is not working?
thanks in advance

Because INCLUDE, VOID, MAIN, etc... mean nothing to the compiler. Consider creating an enum of the words you would like to use then set myKeyword::id to the enum type.

enum bar {THIS, THAT, THE_OTHER_THING};

struct foo {
  bar item;
}

foo baz;
baz.item = THAT;

Because INCLUDE, VOID, MAIN, etc... mean nothing to the compiler. Consider creating an enum of the words you would like to use then set myKeyword::id to the enum type.

actually i have defined these as macros in my header file

You should have mentioned that then...

Have you #included the header in all the relevant files?

It's also likely that the value is not getting into the object. You may need to write a custom constructor.

You should have mentioned that then...

Have you #included the header in all the relevant files?

It's also likely that the value is not getting into the object. You may need to write a custom constructor.

actually i am using this code in a function of a class
but when i used this code without any class in another file it is running fine
what can i do to use this in my function of class

I think you'll have to share the appropriate parts of your class declaration so that I/we can see how the class works.

Make sure the header for this struct is #included in both the class header and the class implementation file. Also, be sure you have header guards in place. If the object that instantiates this struct is a public member of the class, you will need to resolve twice to get to it. If it is a protected/private member, you'll have to access it through a member function/method.

//this is for if you have a public instance inside a class
struct foo {
  int bar;
};

class baz {
 public:
   foo fooObj;
};

int main() {
  baz bazObj;

  bazObj.fooObj.bar = 0; //note the use of (2) "dot" operators...

  return 0;
}
//this is for if you have a non-public instance inside a class
struct foo {
  int bar;
};

class baz {
 private:
   foo fooObj;
 public:
   void setFoo(int newBarVal) {
     fooObj.bar = newBarVal;
   }
};

int main() {
  baz bazObj;

  bazObj.setFoo(0); //note the use of a member function/method

  return 0;
}

this is partial code of my class:

class Lexer:public Token
    {  
              
              FILE *fp;
              char next,ch;
              string lexeme;
              int f,flag,line,i;
              Token *temp;
              
         public: 
          Lexer(char txt[15])
          {
                        fp=fopen(txt,"r");                       
                        ch=getc(fp);
                        line=i=1;
                       damagelexeme();
                       temp=new Token();
                        
          }
...
...
...
         
          Token* analyse()
            {
              cout<<"ssss";
                                 //Read character.
            
                    if(ch=='\n')                   //Compute # of lines in source.txt .
            	  {
            	    line++;
            	    ch=getc(fp);
            	  }
            
                  if(isspace(ch) && ch=='\n' )
                  {
            	  line++;
            	  ch=getc(fp);
                  }
                  if(isspace(ch) && ch!='\n' )          //The character is space.
            	  ch=getc(fp);
            
            
                  if(ch=='/' || ch=='"')    //Function for skipping comments in the file
            	  Skip_Comment();	//and '"' with display statements.
            
            
                  if(isalpha(ch))              //The character is leter.
            	{                    
            	    Read_String();
            	    Is_Keyword_Or_Not();
            	    //Is_Operator_Or_Not();
            	    //Is_Identifier_Or_Not();
            	}
            
.....
.....
.....
              return temp;
            }
          
          void Read_String()
            {
              int j=0;
            
              while(isalpha(ch)&&!feof(fp))
              {
               lexeme=lexeme+ch;
                ch=getc(fp);
              
              } 
            
                fseek(fp,-1,SEEK_CUR);
              
                temp->assign(lexeme,ID);

            }
            
                     
            
            /****************************************************************
            This function check the string is keyword or not.
            *****************************************************************/
            
            void Is_Keyword_Or_Not()
            {
                 		  
                struct myKeyword {
                      string name;            
                      int id;            
                };
                 
                  myKeyword keywords[]={{"include",44},
                  {"void",VOID},{"main",MAIN},{"int",INT},{"float",FLOAT},{"cin",CIN},
                {"cout",COUT},{"if",IF},{"elseif",ELSEIF},{"else",ELSE},{"exit",EXIT},{"for",FOR},{"while",WHILE}};
          
             int j=0;
               cout<<keywords[0].id;
              flag=0;
         ;
      
              for (j=0;j<11;j++)         //search for keyword & # of keywords = 18.
              {
                
                if (lexeme==keywords[j].name)
           
            	{                                               
            	temp->assign(lexeme,keywords[j].id);
            cout<<lexeme;
            		flag=1;
            		break;
            	}
              }
              getchar();
            }
...
...
...
}

Whats the error? How is it not working ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.