Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compilation error for not including iostream

Reply
Thread Tools

compilation error for not including iostream

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      01-16-2009
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-16-2009
http://www.velocityreviews.com/forums/(E-Mail Removed), 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
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-16-2009
(E-Mail Removed), 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Semi OT: Mixing iostream and iostream.h red floyd C++ 3 03-08-2005 05:57 PM
iostream + iostream.h S. Nurbe C++ 7 01-15-2005 07:49 AM
is MS newer <iostream> is slower than older <iostream.h>? ai@work C++ 9 12-16-2004 08:21 PM
Problem while including iostream.h in files targetted by ns2.26 make Pradyot Dhulipala C++ 1 05-12-2004 01:19 PM
#include <iostream.h> or <iostream> John Tiger C++ 10 08-06-2003 01:30 PM



Advertisments