Link to home
Start Free TrialLog in
Avatar of wasawas
wasawasFlag for Japan

asked on

Howto apply __alignof it doesn't work with undeclare error

Hi,Experts

I have a problem with using __alignof function in VC++ 6.0  I apply it to "Win32 Application console" just a basic one using pattern.

Eg.
if(__alignof(char)==1)cout<< "OK";

The error say __alignof does not defined.
Which library of header that I should declare or How to set the project to use the __alignof function.

Best regard.


Avatar of DanRollins
DanRollins
Flag of United States of America image

I just tried that operator in VC++ 6.0 and it compiled without error.  Perhaps the error message is referring to something else on that same line.  Try this:

      int x=1;
      if( __alignof( char ) == 1 ) {
            x=2;
      }

to see if you still get an error on that line.
Avatar of wasawas

ASKER

Hi, DanRollin.

I copied your code and run it again but same error shown:  

error C2065: '__alignof' : š©UŒfDjDX%PgY
(I copy error here to show that it still error with C2065 which mean declaration problem
sorry for using Japanese language cause I don't know exactly word in English version.
it's meaning=  '__alignof' does not defined)

I think it maybe about declaration or setting project. I just create the new project I tried all type that I can eg. Win32 Application, Win32 Console, MFC  and just put the line that using  __alignof() but it doesn't work. Can you tell me which library __alignof() come from. Even MSDN doesnot tell anything about which library include the declaration of __alignof().

Thank you so much.
It is not a library or header file -- it is in internal operator -- like sizeof()
The compiler recognizes it automatically.  

My first thought was that it might be a "Micrsosoft-specific" extension... but I tried turning off the extensions

   Project > Settings > C/C++ > Customize > Disable language extensions

however, whatever setting I use gets the same result:  No compiler error.  Anyway, you can try changing that yourself to see if it makes any difference for you.

I was able to get that error if I "misspell" the operator.  For instance, If I use just one leading underline or three leading underline characters or uppercase "o"...

   _alignof(char)  
 or
   ___alignof(char)
 or
   __alignOf(char)

I get something like...
E:\MyProj\console19.cpp(10) : error C2065: '_alignof' : undeclared identifier
E:\MyProj\console19.cpp(10) : error C2144: syntax error : missing ')' before type 'char'
E:\MyProj\console19.cpp(10) : error C2059: syntax error : ')'
E:\MyProj\console19.cpp(10) : error C2143: syntax error : missing ';' before '{'

So just for fun, you could try changing the number of underline characters (try 0,1,2,3, or 4 of them) to see if it makes a difference.

-- Dan
BTW, I am using VC++ 6.0 with SP 5.  You can get that here:
     http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp5/default.aspx
Avatar of wasawas

ASKER

Hi,

This is the that doesn't work I think my spelling is OK.
And I tried as you adviced me about Project Setting but it still doesn't work.
I don't know why. I'm not sure but I think it can be thinking in simple way.
Some thing environment term or Compiler version or .....

My project have 3 files here:


/////////////////////////////////////////////////// StdAfx.h /////////////////////////////////////////////////////////////
// stdafx.h : –n·¹Æà ¤ó¯ëüÉ Õ¡¤ë
//            ~_oÂgÞpLOKdB~Š      ôUŒjD
//            ×í¸§¯È(n¤ó¯ëüÉ Õ¡¤ë’ðW~Y
//

#if !defined(AFX_STDAFX_H__DB61E1D9_2E61_4FA9_8194_5AAA253E26A3__INCLUDED_)
#define AFX_STDAFX_H__DB61E1D9_2E61_4FA9_8194_5AAA253E26A3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN // Windows ØÃÀüK‰†i(UŒjD¹¿ÃÕ’dW~Y

#include <stdio.h>

// TODO: ×í°éàgŁjØÃÀüÂg’ý WfO`UD

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ oMLnôMký n£’?eW~Y

#endif // !defined(AFX_STDAFX_H__DB61E1D9_2E61_4FA9_8194_5AAA253E26A3__INCLUDED_)

/////////////////////////////////////////////////// StdAfx.cpp /////////////////////////////////////////////////////////////
// stdafx.cpp : –¤ó¯ëüÉÕ¡¤ë’+€½ü¹ Õ¡¤ë
//              Test10.pch UŒ‹×ê³óѤëØÃÀü
//              stdafx.obj UŒ‹×ê³óѤ뿤×Å1

#include "stdafx.h"

// TODO: STDAFX.H k+~ŒfDfSnÕ¡¤ëkðUŒfDjD
// ØÃÀüÕ¡¤ë’ý WfO`UD


/////////////////////////////////////////////////// Test10.cpp /////////////////////////////////////////////////////////////
// Test10.cpp : ³ó½üë ¢×ê±ü·çó(n¨óÈê ݤóÈnš©
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
      if(__alignof(char)==1){
            printf("That's work!\n");
      }
      printf("Hello World!\n");
      return 0;
}

Anyway Thank you for your Suggesttion.(But It is not correct Answer for my situation)
your code includes a call to a function named
   printf
and the compiler will not recognize that symbol without some help.  See if this code compiles without error:

int main(int argc, char* argv[])
{
      int x=0;                                     // added
      if(__alignof(char)==1){
            // printf("That's work!\n");  // <---- remoced
            x=1;                                    // <--- added
      }
      // printf("Hello World!\n");        // <---- removed
      return 0;                                  // <-- breakpoint here and examine variable x
}
Avatar of wasawas

ASKER

Hi, DanRollin

That's not the point. My compiler cannot pass build.
I got error before Debug if I do as your last advice.
I know my code was stupid but I prefer to donot look to the algorithm of code but it should be environment term. If you have any others idea with this please tell me again.

PS.
Now I can do my job by avoid to use that __alignof and use the another instead.
So it's doesn't the matter I just wanna know why my VC++ go mess up like that.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wasawas

ASKER

Hi, DanRollins

I'm just wondering that why it became that way. Only U.S. should be the oldest minor version of Visual Studio in this world. Why they put in U.S. version but not for other country. I think it should be have  patch or something that make your environment work fine. I tried Service Pack 5 but it still doesn't work.

Sorry Can I have more your advice about where's the __alignof declared?

Best regard.
Please review what I said here:  http:#a20112600

It is not declared anywhere.  
It is a built-in keyword like sizeof()
If the compiler does not recognize it, there is nothing you can do except get a different compiler or use #define to create your own version.
Avatar of wasawas

ASKER

Hi,

Sorry to asking you a stupid question but I want to confirm that  IT IS NO WAY to make the VC++6.0 in other version except U.S. distribution can work properly.  Anyway I gave you points for that to end this. But I still wondering that in U.S. version it can run properly. MSDN didn't tell me that US VC++ 6.0 can work properly. That is the major change from VC++6.0 to VC++.NET.  I just believe your word.

Thank you.


My answer was correct and complete, yet you chose to value it as a C
Please take a look at the grading guidelines (read the following link)...

     What's the right grade to give?
     https://www.experts-exchange.com/help.jsp#hi73

...before grading your next question.