Class Constructor Warning/Error

Jun 8, 2010 at 1:52pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template< class NODETYPE >
class R_Tree
{
public:
    // Constructors
    R_Tree();
     // Destructor
    ~R_Tree();
}

void main()
{
    R_Tree< float> RT();
}


When I use this code I get a warning "Prototyped function not called (was a variable definition intended?). When i change the initialization to:
1
2
3
4
void main()
{
    R_Tree< float> RT;
}


I get 2 external linking errors: "error lnk2001: unresolved external symbol "Public:__thiscall R_Tree<float>::~R_Tree<float>(void) referenced in function_main" and "Public:__thiscall R_Tree<float>::R_Tree<float>(void) referenced in function_main"

Help?
Jun 8, 2010 at 2:37pm
When you add () compiler thinks, that you're declaring a function. You should remove them.
You get the errors because your constructor and destructor are undefined.
Jun 8, 2010 at 2:46pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template< class NODETYPE >
R_Tree< NODETYPE >::R_Tree()
{
    Root = NULL;
}

template< class NODETYPE >
R_Tree< NODETYPE >::~R_Tree()
{
    if(Root != NULL)
    {

    }
}


This is defined in the associated .cpp file.
Jun 8, 2010 at 2:51pm
You can't put the implementations of template classes or functions in .cpp files; you have to put them in the header file.
Jun 8, 2010 at 2:55pm
Ahh thank you, fixed it.
Jun 8, 2010 at 5:47pm
Seems that all public functions seem to get this external error unless all of the code is written in the header file. Is there anyway around this? Just seems like a bad way to code having all the logic in the header file.
Jun 8, 2010 at 6:11pm
Officially, if you have the one (?) standards compliant compiler available, you can have the definition in another translation unit with the use of the 'export' keyword. Of course, even that requirement is being removed in c++ 0x, so better to not look into it. As a workaround, put the implementations in a separate '.hpp' file, and then at the bottom of your .h file, include the .hpp file.

--Rollie
Jun 8, 2010 at 6:13pm
That is called the inclusion model, and it is a widely accepted/supported practice for template definitions. I highly recommend it over some lesser-known and less-supported models, such as the separation model or explicit instantiation.

It might seem odd but you have been given sound advice. :)
Last edited on Jun 8, 2010 at 6:14pm
Topic archived. No new replies allowed.