Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GNU G++ Compiler vs AIX xlC compiler

Posted on 1997-02-04
1
Medium Priority
?
824 Views
Last Modified: 2008-03-04
I'm having trouble understanding why the xlC compiler refuse
to compile code that GNU will compile.  Looking at the code
it seems to be legal C++ code.  Are different standards being used?
Thanks
Karl

--Here's my code
        I put together a simple program that demonstrates the errors.
Here's the .H.   I'm not worried about the warnings, just the things that "break" the compiler.  Ideally, we should
be able to change compilers without changing code. I know this assumes a great deal, but perhaps you can help.
Thanks
Karl
--------------------------------------------------------

#ifndef FOO_H
#define FOO_H


class foo
{
      public:
            foo();

            // Error two
            //virtual int foo_method() {}
            int foo_other_method(int );
      private:
            // Error one
            const int FOO_CONST_ONE = -1;
            static int FOO_CONST_TWO = -2;            

};


#endif

-----------------------------
Here's the .C
----------------------------
#include <iostream.h>
#include <stdlib.h>
#include "foo.H"


int main()
{
}
foo::foo()
{
      int i = 0;
      int result;

      cout << i  << endl;
      
      result = foo_other_method(i);

      cout << result;
}


int foo::foo_other_method(int i)
{

      int two = 2;
      if(i >= 1)
      {
            return two;
      }
      else
      {
            //error #3
            exit(-1);
      }
      
}
---------------------------------------------------


Here's the comipiler output under g++ and xlC
-------------------------------------------------
g++  -Wall -I. -o foo foo.C

foo.H: In method 'int foo::foo_method()';
In file included from foo.C:3:
foo.H:11: warning: control reaches end of non-void function 'foo::foo_method()'
foo.C: At top level:
foo.C:7: warning: return type for 'main' changed to integer type

<<<Note: the above warning is only generated with the "-Wall" flag set, but the file compiles fine>>>>
-----------------------------------
xlC -I. -o foo  foo.C
"foo.H", line 15.43: 1540-041: (S) An initializer is not allowed for "class member".
"foo.H", line 16.44: 1540-041: (S) An initializer is not allowed for "class member".
"foo.H", line 11.43: 1540-331: (E) Return value of type "int" is expected.
"foo.C", line 36.1: 1540-017: (W) Return value of type "int" is expected.
0
Comment
Question by:kwaldman
1 Comment
 
LVL 3

Accepted Solution

by:
os012897 earned 300 total points
ID: 1162342
Well, I never worked with xIC so far, but I could tell some tails
about code-sompatibitlty between gcc and cc .... :I!

Different compilers implement the ANSI C++ standard to various
extends, e.g. there are still compilers out there that do not
support templates.

xIC seems to be very strict on return values: Why?

1) Gives the error on the .H file for the virtual function that
   should return an int but does not!
2) Gives you the error in the .C file where you exit, but do not
   return!

For the other two errors
         "An initializer is not allowed for "class member""
there is to say, that you never initialize members in the
class-definition. You do it at the implementation of the class-
constructor like this:

foo :: foo () : FOO_CONST_ONE (-1), FOO_CONST_TWO (-2)

{  implementation of the constructor .....
}

What happens is that you call a kind of "constructor" that sets
the values for the variables, which is why setting the value of
the constant works.

Just keep in mind, that no two C++ compilers are the same and
that there is hardly a program out there that works on different
compilers without applying changes to it!

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

824 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