compilation error for not including iostream

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Jan 16, 2009.

  1. , India

    , India Guest

    Consider the following program:

    The following is in File Name: 1.h
    -------------------------------------------------
    #ifndef _1_H
    #define _1_H

    #include <iostream>

    namespace MyNamespace
    {
    class Test
    {
    public:
    inline explicit Test(int arg = -1);
    inline int value() const;

    private:
    int val;
    };

    inline Test::Test(int arg) : val(arg)
    {
    std::cout << "from Test one arg ctor: val = " << value() <<
    std::endl;
    }

    inline int Test::value() const
    {
    return val;
    }

    extern Test object;
    }

    #endif

    The following is in File Name : initialization.cpp
    --------------------------------------------------------------------
    #include "1.h"

    namespace MyNamespace
    {
    Test object(100);
    }

    The following is in File Name : main.cpp
    ----------------------------------------------------------
    #include <cstdlib>
    #include <iostream>
    #include "1.h"

    using namespace std;
    using namespace MyNamespace;

    int main()
    {
    cout << "from main: " << object.value() << endl;

    return EXIT_SUCCESS;
    }

    When I compile the above program with g++3.4.3 as
    g++ -std=c++98 -pedantic -Wall -Wextra main.cpp initialization.cpp
    or as
    g++ -std=c++98 -pedantic -Wall -Wextra initialization.cpp main.cpp

    I get the following compilation error:
    /tmp/cckSyy17.o(.text+0x20): In function
    `__static_initialization_and_destruction_0(int, int)':
    : undefined reference to `MyNamespace::Test::Test(int)'
    collect2: ld returned 1 exit status

    However if I #include <iostream> in initialization.cpp, then the
    compilation goes fine and the program produces the output
    from Test one arg ctor: val = 100
    from main: 100

    Why does initialization.cpp require '#include <iostream>' ?

    Kindly explain.

    Thanks
    V.Subramanian
     
    , India, Jan 16, 2009
    #1
    1. Advertising

  2. , India

    Ian Collins Guest

    , India wrote:
    > Consider the following program:
    >

    <snip code>
    >
    > When I compile the above program with g++3.4.3 as
    > g++ -std=c++98 -pedantic -Wall -Wextra main.cpp initialization.cpp
    > or as
    > g++ -std=c++98 -pedantic -Wall -Wextra initialization.cpp main.cpp
    >
    > I get the following compilation error:
    > /tmp/cckSyy17.o(.text+0x20): In function
    > `__static_initialization_and_destruction_0(int, int)':
    > : undefined reference to `MyNamespace::Test::Test(int)'
    > collect2: ld returned 1 exit status
    >
    > However if I #include <iostream> in initialization.cpp, then the
    > compilation goes fine and the program produces the output
    > from Test one arg ctor: val = 100
    > from main: 100
    >
    > Why does initialization.cpp require '#include <iostream>' ?
    >
    > Kindly explain.
    >

    Compiler bug?

    --
    Ian Collins
     
    Ian Collins, Jan 16, 2009
    #2
    1. Advertising

  3. , India wrote:
    > Consider the following program:

    [snip]
    >
    > When I compile the above program with g++3.4.3 as
    > g++ -std=c++98 -pedantic -Wall -Wextra main.cpp initialization.cpp
    > or as
    > g++ -std=c++98 -pedantic -Wall -Wextra initialization.cpp main.cpp
    >
    > I get the following compilation error:
    > /tmp/cckSyy17.o(.text+0x20): In function
    > `__static_initialization_and_destruction_0(int, int)':
    > : undefined reference to `MyNamespace::Test::Test(int)'
    > collect2: ld returned 1 exit status
    >
    > However if I #include <iostream> in initialization.cpp, then the
    > compilation goes fine and the program produces the output
    > from Test one arg ctor: val = 100
    > from main: 100
    >
    > Why does initialization.cpp require '#include <iostream>' ?


    g++ 3.4.3 doesn't have a problem with it so it may be a compiler bug or
    a problem in your tool chain.

    Concerning the inclusion of iostream, it should not change anything
    since it is already included in 1.h.

    --
    Michael
     
    Michael DOUBEZ, Jan 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Tiger
    Replies:
    10
    Views:
    5,648
  2. Pradyot Dhulipala
    Replies:
    1
    Views:
    710
    John Harrison
    May 12, 2004
  3. ai@work
    Replies:
    9
    Views:
    561
    Ron Natalie
    Dec 16, 2004
  4. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    793
    red floyd
    Jan 15, 2005
  5. red floyd
    Replies:
    3
    Views:
    551
    Dietmar Kuehl
    Mar 8, 2005
Loading...

Share This Page