Thread: What's the cause of this error message?

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If the structure is internal to the class you can't use it as a return value, it doesn't matter if the member function is public, private, protected or whatever.

    What do you plan to do with this return value? Normally a private member function will only work on either local variables or class member variables. So instead of returning a value, alter a class member variable.

    Jim

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Quote Originally Posted by Subsonics View Post
    Thanks, unfortunately that moved the problem on to other errors like:

    error: expected initializer before ‘struct’
    error: expected constructor, destructor, or type conversion before ‘->’ token
    Are you sure that you did it without any mistake?

    This compiles fine.

    ABC.h
    Code:
    #ifndef ABC_H_INCLUDED
    #define ABC_H_INCLUDED
    
    #include <cstddef>
    
    template <class T> class ABC {
    
    public:
    
        struct node_t {
            T data;
            struct node_t *next;
        };
    
        node_t *new_node(T element);
    
    };
    
    
    #endif // ABC_H_INCLUDED
    .cpp
    Code:
    #include "ABC.h"
    
    //template <class T>
    //typename ABC<T>::node_t *ABC<T>::node_node(T element)
    template <class T>
    typename ABC<T>::node_t *ABC<T>::new_node(T element) {
        struct node_t *node = new struct node_t;
        node->data = element;
        node->next = NULL;
        return node;
    }
    
    int main(void) {
        return 0;
    }

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by jimblumberg View Post
    If the structure is internal to the class you can't use it as a return value, it doesn't matter if the member function is public, private, protected or whatever.
    That seems odd, the fact that it's private will prevent it from ever being called from the outside anyway. But, I may end up just returning void*, but as I said creating instances of the struct within the class can be done and is done.

    Quote Originally Posted by jimblumberg View Post
    What do you plan to do with this return value? Normally a private member function will only work on either local variables or class member variables. So instead of returning a value, alter a class member variable.

    Jim
    Add it as a part in a linked data structure. The struct is local to the class, I also have class member variables but they are 100% unrelated to this issue.

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by fightmx View Post
    Are you sure that you did it without any mistake?

    This compiles fine.
    That's odd, what compiler are you using? I tried to copy/paste what you posted and compiled it:

    Code:
    g++ paste.cpp
    And got these errors:

    Code:
    paste.cpp:6: error: prototype for ‘typename ABC<T>::node_t* ABC<T>::new_node(T)’ does not match any in class ‘ABC<T>’
    ABC.h:16: error: candidate is: void* ABC<T>::new_node(T)
    paste.cpp:6: error: template definition of non-template ‘typename ABC<T>::node_t* ABC<T>::new_node(T)’

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    I tested both using Visual Studio and MinGW (I'm using windows now).

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jimblumberg
    If the structure is internal to the class you can't use it as a return value, it doesn't matter if the member function is public, private, protected or whatever.
    That is not true. For example:
    Code:
    template <class T>
    class ABC {
    private:
        struct node_t {
            T data;
            node_t* next;
        };
    public:
        void foo(const T& element)
        {
            node_t node(new_node(element));
        }
    private:
        node_t new_node(const T& element);
    };
    
    template <class T>
    typename ABC<T>::node_t ABC<T>::new_node(const T& element) {
        node_t node;
        node.data = element;
        node.next = 0;
        return node;
    }
    
    int main()
    {
        ABC<int> x;
        x.foo(123);
    }
    By the way, this is C++, not C, so the struct node_t thing is an anachronism.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Error message
    By Holmeslice in forum C Programming
    Replies: 4
    Last Post: 10-07-2012, 03:33 AM
  2. C++ Error message when trying to run
    By Huzapro0 in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2011, 08:45 AM
  3. Replies: 13
    Last Post: 10-08-2010, 05:35 PM
  4. Error message
    By vopo in forum Windows Programming
    Replies: 5
    Last Post: 10-03-2007, 03:11 AM
  5. Need help with error message!
    By smokedragon in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2002, 09:33 PM