Templated BST "Already Initialized" Error

This is a discussion on Templated BST "Already Initialized" Error within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm implementing a linked BST to create a simple program to store, search, and retrieve records. Whenever I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    Templated BST "Already Initialized" Error

    Hi all,

    I'm implementing a linked BST to create a simple program to store, search, and retrieve records. Whenever I instantiate a new CityRecord object in my main function as follows:

    Code:
    SearchTree<int, CityRecord> stree;
    SearchTree<int, CityRecord>::Position p;
    I get error

    Code:
    Error	3	c:\users\pogos\desktop\classes fall 2011\csci 220\projects\project 4\project 4\project 4\searchtree.h
    	91	error C2437: 'LBTree' : already initialized
    which refers to

    Code:
    SearchTree() : LBTree() {}
    in the search tree header file. I don't get this error when I comment out the main function body.

    There are several files and hundreds of lines of code that would be ridiculous to post here, so could anyone please point me in the right direction in terms of where to properly start declaring a new search tree in my main function? I can give more info on the code as needed. Thank you!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    C2437 is shown here: Compiler Error C2437
    It means that you are initialising LBTree (the base class) twice. The code you've posted does not demonstrate that so I'm 99.9% sure that your real code has other stuff in it which is the real cause of the problem. Afterall, if your constructor were really exactly as you posted then it is the same as the default constructor, in which case you should delete it and this would logically have to cause the same fault, which is impossible.
    Post the entire unedited constructor for SearchTree.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Quote Originally Posted by iMalc View Post
    C2437 is shown here: Compiler Error C2437
    It means that you are initialising LBTree (the base class) twice. The code you've posted does not demonstrate that so I'm 99.9% sure that your real code has other stuff in it which is the real cause of the problem. Afterall, if your constructor were really exactly as you posted then it is the same as the default constructor, in which case you should delete it and this would logically have to cause the same fault, which is impossible.
    Post the entire unedited constructor for SearchTree.
    That is actually the whole constructor for SearchTree.

    LBTree is actually a typedef of a templated LBT class as follows:

    Code:
    typedef LinkedBinaryTree<LBTEntry> LBTree;
    with

    Code:
    template <typename Key, typename Element,
    
    typename LBTEntry = Entry<Key, Element> >
    on top of SearchTree.h.

    LinkedBinaryTree's constructor is as follows:

    Code:
    LinkedBinaryTree() : _root(new Node), nodes(1) { }
    I also find it strange that the compiler wasn't reporting the error when the main function was commented out. I think I need to modify

    Code:
    SearchTree <int, CityRecord> stree;
    to include the Entry class. I tried

    Code:
    SearchTree <Entry <int, CityRecord> > stree;
    but I get a too few arguments error, which makes sense, but I dont understand what additional argument I have to add since I only need the key and the CityRecord object.

    The errors after the above modification are:

    Code:
    error C2976: 'SearchTree' : too few template arguments
    error C2133: 'stree' : unknown size
    error C2512: 'SearchTree' : no appropriate default constructor available
    Note: The SearchTree constructor I provided above is instructor supplied

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by misterpogos View Post
    That is actually the whole constructor for SearchTree.
    Note: The SearchTree constructor I provided above is instructor supplied
    Okay, then try deleting it. It does nothing anyway. What happens then?

    Quote Originally Posted by misterpogos View Post
    I also find it strange that the compiler wasn't reporting the error when the main function was commented out. I think I need to modify
    Visual Studio doesn't complain about template code that can never compile if it doesn't actually need it to compile to produce the working program. Without a main it mustn't need to compile the templated code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Whenever working with template code, remember this advice.
    Always explicitly instantiate the class. This allows the compiler to properly check for errors.
    You do this by

    template class_name<template_args>;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Thank you guys! I truly appreciate it! I resorted to writing untemplated code (templates weren't required, but they were encouraged). Unfortunately I wasn't able to grasp the concept fully in time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-02-2011, 07:15 AM
  2. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 09:39 PM
  3. C++: Variable not initialized within "while" loop
    By iceburg_2487 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2011, 11:17 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21