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
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