How to return a struct type which is declared in class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • z060053
    New Member
    • Mar 2008
    • 1

    How to return a struct type which is declared in class?

    Code:
       
    #include <iostream>
    using namespace std;
    template <class T>class list{
    protected:
        typedef struct node{
            node* prev;
            T* data;
            node* next;
        };
        node* first;
        node* last;
    public:
        list();
        void push_back(T& t);
        void pop_back();
        T& front();
        T& back();
        node& end();
        node& begin();
        void print_list();
    };
    template <class T>
    list<T>::list(){
        first=0;
        last=0;
    };
    template <class T>
    T& list<T>::back(){
        return last->data;
    }
    template <class T> 
    T& list<T>::front(){
        return first->data;
    };
    template <class T>
    list<T>::node& list<T>::end(){         //error in this line.
        return *last;
    }
    template <class T> 
    list<T>::node& list<T>::begin(){       //error in this line.
        return *first;
    };
    template <class T>
    void list<T>::push_back(T& t){       
        node *n={0,&t,0};
        if(front==0){
            first = n;
            last = n;
        }else{
            last->next=n;
            n->prev=last;
            last = n;
        }
    }
    template <class T>
    void list<T>::pop_back(){
        if(last == first){
            last=0;
            first=0;
        }else{
            last->prev->next=0;
            last=last.prev;
        }
    }
    
    template <class T>
    void list<T>::print_list(){
        node* p = front;
        while(p!=0){
           p=p->next;
           cout << p->data << "-";
        }
        cout << endl;
    };
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Really you should post some text explaining your problem as well as the code.

    However your problem is that you have declared your structure as private to the class but you are returning it from a public function. The code calling the function will not have access to the type because it is private to the class so this leads to an inconsistency which the compiler is picking up.

    Comment

    • sfuo
      New Member
      • Apr 2009
      • 6

      #3
      I have a similar problem with my code. Pretty much what I would like to know is how do I return a structure that was created within a class. Here is what I have:

      Header:

      Code:
      class Tile
      {
      	private:
      	SDL_Rect tile;
      	string type;
      	string texture;
      	int clip_num;
      	int col_num;
      	int ID;
      	struct warp_coords
      	{
      		string name;
      		int x;
      		int y;
      	}warp;
      	struct playerStart_coords
      	{
      		int x;
      		int y;
      	}playerStart;
      	public:
      	Tile( int x, int y, string a, string b, int c, int d );
      	string get_type();
      	string get_texture();
      	warp_coords get_warp();
      	playerStart_coords get_playerStart();
      	int get_ID();
      	int get_clip_num();
      	int get_col_num();
      	void show();
      };
      k so I wanna return the structure warp and playerStart so I made:

      Code:

      Code:
      warp_coords Tile::get_warp()
      {
      	return warp;
      }
      
      playerStart_coords Tile::get_playerStart()
      {
      	return playerStart;
      }
      my errors are
      'warp_coords' does not name a type
      'playerStart_co ords' does not name a type

      I thought you could return structures like int string char and stuff like that. Any help would be great.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Like the compiler says, 'warp_coords' and 'playerStart_co ords' are not types.

        However, Tile::warp_coor ds and Tile::playerSta rt are types.

        Comment

        • sfuo
          New Member
          • Apr 2009
          • 6

          #5
          thanks it works great and I would have never figured it out

          Comment

          Working...