Class Constructor Warning/Error

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?
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.
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.
You can't put the implementations of template classes or functions in .cpp files; you have to put them in the header file.
Ahh thank you, fixed it.
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.
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
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
Topic archived. No new replies allowed.