Solved

GNU G++ Compiler vs AIX xlC compiler

Posted on 1997-02-04
1
778 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 150 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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