Why do I see an incomplete type when I define the constructor outside the class declaration?

O

oliver.lin

In my simple test code, I tried to define my constructor outside of the
class declaration headr file.
The header file: file_handler.h
============================================================
1 #include <string>
2 using namespace std;
3 class file_handler
4 { //Declare member variables
5 private:
6 string file_rd, file_wr; //Define the file name for read
and write.
7 string name[100]; //Define a string array without
pre-defined size.
8 float score[100]; //The score corresponding to the name.
9 public:
10 file_handler();
11 //Constructor, Initialize the default file name.
=============================================================

The function definition: file_handler.cpp
=============================================================
1 #include <iostream>
2 #include <string>
3 #include "file_handler.h"
4 using namespace std;
5 file_handler::file_handler()
6 {//file_rd="tmp.dat";
7 //file_wr="tmp.dat";
8 score[0]=0;
9 }//Default constructor
==============================================================

The compile with g++ -Wall -g is OK. But when I debug the program and
step into the member function of the file_handler, I saw the following
complain from gdb:
===============================================================
(gdb) step
(gdb) step
_ZN12file_handler10data_fetchEv (this=<incomplete type>) at
file_handler.cpp:21
(gdb)
===============================================================
With the incomplete type of "this" pointer, I can't watch the member
variables of the object. But strangely enough, all the member variables
can be assgined value coorectly.
When I put the constructor definiton in the header file or just remove
this definition, this point gets it value and I can see any variable
from GDB.
I wonder is the problem from my program or from the g++/gdb.
My paltform: solaris 2.6
Compiler g++2.95/gcc 2.95
Debugger: gdb4.16
 
I

Ian

In my simple test code, I tried to define my constructor outside of the
class declaration headr file.
The header file: file_handler.h
============================================================
1 #include <string>
2 using namespace std;

Never do this in a header!

from GDB.
I wonder is the problem from my program or from the g++/gdb.
My paltform: solaris 2.6
Compiler g++2.95/gcc 2.95
Debugger: gdb4.16
Very old compiler and OS version. Why not use a more up to date version?

Ian
 
O

oliver.lin

============================================================
1 #include <string>
2 using namespace std;

Never do this in a header!

-->I remove this from header file but the problem is still exist.
--> Compiler/OS version: My company deploy these for us, Do you think
it's a bug of compiler?
 
E

Earl Purple

Never do this in a header!

-->I remove this from header file but the problem is still exist.
--> Compiler/OS version: My company deploy these for us, Do you think
it's a bug of compiler?

You should not put "using namespace std" into your header in global
scope because then you are forcing anyone who includes your header to
put all of std into the global namespace.

It is not so bad if you declare it inside another namespace (where it
is then limited to that namespace).

However that itself will not be the cause of your error.

Incomplete types are run-time are only a problem in a "delete"
statement because you are allowed to delete an incomplete pointer.
However they might then cause a runtime error if you try to do so.
Otherwise your code should fail to compile and incomplete types cannot
cause runtime errors.
 
R

Ram

=============================================================
The function definition: file_handler.cpp
=============================================================
1 #include <iostream>
2 #include <string>
3 #include "file_handler.h"
4 using namespace std;
5 file_handler::file_handler()
6 {//file_rd="tmp.dat";
7 //file_wr="tmp.dat";
8 score[0]=0;
9 }//Default constructor
==============================================================

The compile with g++ -Wall -g is OK. But when I debug the program and
step into the member function of the file_handler, I saw the following
complain from gdb:
===============================================================
(gdb) step
(gdb) step
_ZN12file_handler10data_fetchEv (this=<incomplete type>) at
file_handler.cpp:21
(gdb)
===============================================================
With the incomplete type of "this" pointer, I can't watch the member
variables of the object. But strangely enough, all the member variables
can be assgined value coorectly.
When I put the constructor definiton in the header file or just remove
this definition, this point gets it value and I can see any variable
from GDB.
I wonder is the problem from my program or from the g++/gdb.
My paltform: solaris 2.6
Compiler g++2.95/gcc 2.95
Debugger: gdb4.16

You haven't mentioned the code at which it is failing. Whats at
file_handler.cpp:21?

-Ramashish
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top