CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2011
    Posts
    9

    Accessing class functions from parameter of template?

    Hey everyone,
    I have been having some problems getting my code to compile where I need to have a class studentRecord I defined as one of the types of another template class bst, and then need to use a member function from class studentRecord to return a new type of the template class bst.

    My implementation needs to look like this:

    int main( )
    {

    bst<studentRecord, int> *mydb;
    mydb = mydb.create_database();

    Essentially, i need to create a new bst that with the first type being studentRecord. create_database is a member function of studentRecord, here is the inplementation of that function.

    template<class Item, class Key>
    bst<studentRecord, int>* studentRecord::create_database()
    {
    bst<studentRecord, int> blank;
    bst<studentRecord, int> *root;
    int num_courses = 0;
    int count = 0;
    string str;
    while(true)
    {
    getline(cin, str);
    if (str == ".")break;
    else blank.id = atoi(str);
    blank.key_value = blank.id;
    getline(cin, blank.name);
    getline(cin, blank.major);
    cin >> num_courses;
    for (int j = 0; j < num_courses; ++j)
    {
    cin >> blank->courses[j].num;
    cin >> blank->courses[j].cred;
    getline(cin, blank->courses[j].grade);
    }
    insert_node(blank, blank.key_value, blank.item_value);
    for(count; count < 1; count++)
    {
    root = &blank;
    }
    }
    return root;
    }

    I obviously dont have all the code correct, but I need to get my program to compile before i can test it, and I am having a very hard time figuring out the correct way to call the function from studentRecord to return the correct bst.

    The error i am getting right now is,

    studentExam.cxx:15: error: request for member `create_database' in `mydb',
    which is of non-aggregate type `bst<studentRecord, int>*'

    and I cant seem to get anywhere from backwards at this point.

    Let me know if posting more code would make the problem clearer.

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Accessing class functions from parameter of template?

    I didn't check the rest because well its just too **** fugly without code tags to be bothered with but i did spot this...
    Code:
    bst<studentRecord, int> *mydb;
    mydb = mydb.create_database();
    Now here mydb is a pointer and so that means that the dot operator is completely wrong. In this case we use the arrow operator like...
    Code:
    bst<studentRecord, int> *mydb;
    mydb = mydb->create_database();
    However this brings us to another not so subtle problem in that while mydb has the type bst<studentRecord,int>* you cant actually call create_database through that pointer until it actually points at an object. The existence of a pointer to an object does not necessarily mean that there is anything actually being pointed to which is the case here.
    Now another problem is that the function you are trying to call resides in studentRecord and not in bst yet you try to call it via a bst<...>*. Does bst have a function called that which calls the version in studentRecord?
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    OK, so if I can declare a studentRecord in my constructor for bst, I should be able to access it, but if I try to just put it in like this:

    template <class Item, class Key>
    bst<Item, Key>::bst(void){
    studentRecord record;
    }

    I get

    bst.h: In constructor `bst<Item, Key>::bst()':
    bst.h:71: error: syntax error before `;' token

    And sorry about the tags, I have never posted code in a forum before so I dont have a very good idea of what is expected from me if i paste some code in.

  4. #4
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    OK i figured this out, I have my constructor creating a new studentRecord object whenever a bst is created, but now with

    bst<studentRecord, int> *mydb;
    mydb = mydb->create_database();

    I am getting

    `create_database' undeclared (first use this
    function)

    I would think that my includes work because I can get my studentRecord.cxx file to compile fine, so I assume that if I #include studentRecord.cxx in my main program, I should be able to access all of the member functions of both classes.

    Possibly something wrong with the way I am calling the function?

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Accessing class functions from parameter of template?

    what you need to do is to call the function through the studentRecord class something like...
    Code:
    bst<studentRecord, int> *mydb;
    studentRecord myrecord;
    mydb = myrecord.create_database();
    But then its hard to tell for sure from what you posted.
    Remember to use code tags they look like [code]your code here[/code]
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  6. #6
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    OK thanks, that makes sense and seems like it should work but now it just gives me

    Code:
    error: no matching function for call to `studentRecord::create_database()'
    The function is definitely there, but I just cant seem to be able to use it.

  7. #7
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Accessing class functions from parameter of template?

    post a small but complete example that exhibits the problem (inside code tags). Try to cut down your code to just the bare essentials, dont dump the lot on us.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  8. #8
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    bst class and constructors:

    Code:
    template<class Item, class Key>
    class bst   
    {
    
    public:
        Item item_value;
        bst* left_tree;
        bst* right_tree;
        Key key_value;
        bst<Item, Key> *catchall;
        typedef void (*Process)();
        typedef int Param;
        
        bst();
        bst(Item item, Key key);
    ...
    template <class Item, class Key>
    bst<Item, Key>::bst(){
        studentRecord record;
        item_value = record;
    }
    
    template <class Item, class Key>
    bst<Item, Key>::bst(Item item, Key key){
        item_value = item;
        key_value = key;
        left_tree = NULL;
        right_tree = NULL;
    }

    studentRecord class and create_database function:

    Code:
    in studentRecord.h
    
    #ifndef BST_H
    #define BST_H
    using namespace std;
    
    struct course{
        int num;
        int cred;
        char grade;
    };
    
    class studentRecord{
    public:
    
        template<class Item, class Key>
        bst<studentRecord, int>* create_database();
    implementation of create_database();

    Code:
    in studentRecord.cxx
    
    template<class Item, class Key>
    bst<studentRecord, int>* studentRecord::create_database()
    {
        bst<studentRecord, int> blank;
        bst<studentRecord, int> *root;
        int num_courses = 0;
        int count = 0;
        string str;
        while(true)
        {
    	getline(cin, str);
    	if (str == ".")break;
    	else blank.id = atoi(str);
    	blank.key_value = blank.id;
    	getline(cin, blank.name);
    	getline(cin, blank.major);
    	cin >> num_courses;
    	for (int j = 0; j < num_courses; ++j)
    	{
    	    cin >> blank->courses[j].num;
    	    cin >> blank->courses[j].cred; 
    	    getline(cin, blank->courses[j].grade);
    	}   
    	insert_node(blank, blank.key_value, blank.item_value);
    	for(count; count < 1; count++)
    	{
    	    root = &blank;
    	}
        }
        return root;   
    }
    and my main file:

    Code:
    #include "studentRecord.cxx"
    using namespace std;
    
    int main( )
    {
        bst<studentRecord, int> *mydb;
        studentRecord myrecord;
        mydb = mydb->item_value::create_database();
    ...
    and finally the error i am currently getting:
    Code:
    studentExam.cxx:15: error: cannot convert `studentRecord' to `
       bst<studentRecord, int>*' in assignment

  9. #9
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Accessing class functions from parameter of template?

    why are you templating create_database in studentRecord?
    Its not necessary.

    should just be
    Code:
    bst<studentRecord, int>* create_database();
    Also on another note why is student_record.h header guard BST_H??

    and this
    Code:
    mydb = mydb->item_value::create_database();
    is still wrong for the reasons listed in above posts.
    Last edited by Russco; May 3rd, 2011 at 09:16 PM.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  10. #10
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    I was templating create_database because i have to use a member function of bst in create_database:

    Code:
    insert_node(blank, blank.key_value, blank.item_value);
    I cant figure out how to use it without templating.
    I changed create_database to

    Code:
    bst<studentRecord, int>* studentRecord::create_database()
    {
        bst<studentRecord, int> blank;
        bst<studentRecord, int> *root;
        int num_courses = 0;
        int count = 0;
        string str;
        while(true)
        {
    	getline(cin, str);
    	if (str == ".")break;
    	else blank.item_value.id = atoi(str);
    	blank.key_value = blank.item_value.id;
    	getline(cin, blank.item_value.name);
    	getline(cin, blank.item_value.major);
    	cin >> num_courses;
    	for (int j = 0; j < num_courses; ++j)
    	{
    	    cin >> blank.item_value.courses[j].num;
    	    cin >> blank.item_value.courses[j].cred; 
    	    getline(cin, blank.item_value.courses[j].grade);
    	}   
    	blank.insert_node(blank, blank.key_value, blank.item_value);
    	for(count; count < 1; count++)
    	{
    	    root = &blank;
    	}
        }
        return root;   
    }
    and now my errors look like

    Code:
    In file included from studentExam.cxx:8:
    studentRecord.cxx: In member function `bst<studentRecord, int>* 
       studentRecord::create_database()':
    studentRecord.cxx:170: error: cannot convert `std::string' to `const char*' for 
       argument `1' to `int atoi(const char*)'
    studentRecord.cxx:179: error: no matching function for call to `getline(
       std::istream&, char&)'
    studentRecord.cxx:181: error: no matching function for call to `
       bst<studentRecord, int>::insert_node(bst<studentRecord, int>&, int&, 
       studentRecord&)'
    bst.h:87: error: candidates are: void bst<Item, Key>::insert_node(bst<Item, 
       Key>*&, const Key&, const Item&) [with Item = studentRecord, Key = int]

  11. #11
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    Oh, and the code for insert_node()
    Code:
    template <class Item, class Key>
    void bst<Item, Key>::insert_node(bst<Item, Key>*& root, const Key& k,
    				 const Item& entry){
        if(root == NULL){
    	root = new bst<Item, Key>(entry, k);
    	return;
        }
        else if(k < root->key_value){
    	insert_node(root->left_tree, k, entry);
        }
        else{
    	insert_node(root->right_tree, k, entry);
        }
    }

  12. #12
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Accessing class functions from parameter of template?

    atoi expects a const char* and you are giving it a std::string. change
    Code:
    else blank.item_value.id = atoi(str);
    to
    Code:
    else blank.item_value.id = atoi(str.c_str());
    The other errors are due to the getline further down. You are trying to read a single char as a string. You could make grade a string or use cin >> instead of getline. Be aware mixing getlines and operator >> can lead to problems due to newlines being left in the stream. Making grade a string might be an easier option depending on how its being used elsewhere. Fix those and see where we go from there.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  13. #13
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    Thanks for all the help, I have now been able to get everything to work except for the insert_node in create_database. I still am getting this:

    Code:
    In file included from studentExam.cxx:8:
    studentRecord.cxx: In member function `bst<studentRecord, int>* 
       studentRecord::create_database()':
    studentRecord.cxx:184: error: no matching function for call to `
       bst<studentRecord, int>::insert_node(bst<studentRecord, int>&, int&, 
       studentRecord&)'
    bst.h:87: error: candidates are: void bst<Item, Key>::insert_node(bst<Item, 
       Key>*&, const Key&, const Item&) [with Item = studentRecord, Key = int]

  14. #14
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Accessing class functions from parameter of template?

    insert_node is expecting a pointer, you pass it an object. pass its address instead by prefixing it with & like so
    Code:
    blank.insert_node(&blank, blank.key_value, blank.item_value);
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  15. #15
    Join Date
    May 2011
    Posts
    9

    Re: Accessing class functions from parameter of template?

    Followed your advice but I am still getting the same error, do I need to pass the other parameters in a different way too possibly?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured