What's the cause of this error message?

This is a discussion on What's the cause of this error message? within the C++ Programming forums, part of the General Programming Boards category; If the structure is internal to the class you can't use it as a return value, it doesn't matter if ...

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 08:33 PM

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