Solved

GNU G++ Compiler vs AIX xlC compiler

Posted on 1997-02-04
1
762 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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now