compilation error for not including iostream

S

subramanian100in

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
 
I

Ian Collins

Consider the following program:
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?
 
M

Michael DOUBEZ

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top