[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Howto apply __alignof it doesn't work with undeclare error

Posted on 2007-10-17
12
Medium Priority
?
449 Views
Last Modified: 2007-10-24
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.


0
Comment
Question by:wasawas
  • 7
  • 5
12 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 20106393
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.
0
 

Author Comment

by:wasawas
ID: 20106485
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20112600
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
0
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
LVL 49

Expert Comment

by:DanRollins
ID: 20112655
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
0
 

Author Comment

by:wasawas
ID: 20121114
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)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20125421
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
}
0
 

Author Comment

by:wasawas
ID: 20128095
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.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 20129422
If that last test still failed, then we have ruled out every possibility that  I have ever experienced.  It did not fail with my copy of VC++ 6.0  If you have applied the latest VC service pack, then it should work on yours.

Note in this page:
   C++ Compiler, Linker, and C++ Language
   http://msdn2.microsoft.com/en-us/library/aa984716(vs.71).aspx  

that Microsoft indicates that the __alignof keyword is **new for VC 7**
Another page indicates that __alignof is "new to VC++ .NET 2002".  
It could be that it was added to VC 6 in the U.S. and not elsewhere.   I can say that it does not appear in older versions of the complier documentation.

So that is your answer:  
It is not supported by the version of VC++ that you are using.
0
 

Author Comment

by:wasawas
ID: 20129509
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20129776
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.
0
 

Author Comment

by:wasawas
ID: 20135600
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.


0
 
LVL 49

Expert Comment

by:DanRollins
ID: 20137095
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?
     http://www.experts-exchange.com/help.jsp#hi73

...before grading your next question.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question