473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. template <class T>class list{
  5. protected:
  6.     typedef struct node{
  7.         node* prev;
  8.         T* data;
  9.         node* next;
  10.     };
  11.     node* first;
  12.     node* last;
  13. public:
  14.     list();
  15.     void push_back(T& t);
  16.     void pop_back();
  17.     T& front();
  18.     T& back();
  19.     node& end();
  20.     node& begin();
  21.     void print_list();
  22. };
  23. template <class T>
  24. list<T>::list(){
  25.     first=0;
  26.     last=0;
  27. };
  28. template <class T>
  29. T& list<T>::back(){
  30.     return last->data;
  31. }
  32. template <class T> 
  33. T& list<T>::front(){
  34.     return first->data;
  35. };
  36. template <class T>
  37. list<T>::node& list<T>::end(){         //error in this line.
  38.     return *last;
  39. }
  40. template <class T> 
  41. list<T>::node& list<T>::begin(){       //error in this line.
  42.     return *first;
  43. };
  44. template <class T>
  45. void list<T>::push_back(T& t){       
  46.     node *n={0,&t,0};
  47.     if(front==0){
  48.         first = n;
  49.         last = n;
  50.     }else{
  51.         last->next=n;
  52.         n->prev=last;
  53.         last = n;
  54.     }
  55. }
  56. template <class T>
  57. void list<T>::pop_back(){
  58.     if(last == first){
  59.         last=0;
  60.         first=0;
  61.     }else{
  62.         last->prev->next=0;
  63.         last=last.prev;
  64.     }
  65. }
  66.  
  67. template <class T>
  68. void list<T>::print_list(){
  69.     node* p = front;
  70.     while(p!=0){
  71.        p=p->next;
  72.        cout << p->data << "-";
  73.     }
  74.     cout << endl;
  75. };
  76.  
  77.  
Mar 7 '08 #1
4 3911
Banfa
9,065 Expert Mod 8TB
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.
Mar 8 '08 #2
sfuo
6
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:

Expand|Select|Wrap|Line Numbers
  1. class Tile
  2. {
  3.     private:
  4.     SDL_Rect tile;
  5.     string type;
  6.     string texture;
  7.     int clip_num;
  8.     int col_num;
  9.     int ID;
  10.     struct warp_coords
  11.     {
  12.         string name;
  13.         int x;
  14.         int y;
  15.     }warp;
  16.     struct playerStart_coords
  17.     {
  18.         int x;
  19.         int y;
  20.     }playerStart;
  21.     public:
  22.     Tile( int x, int y, string a, string b, int c, int d );
  23.     string get_type();
  24.     string get_texture();
  25.     warp_coords get_warp();
  26.     playerStart_coords get_playerStart();
  27.     int get_ID();
  28.     int get_clip_num();
  29.     int get_col_num();
  30.     void show();
  31. };
k so I wanna return the structure warp and playerStart so I made:

Code:

Expand|Select|Wrap|Line Numbers
  1. warp_coords Tile::get_warp()
  2. {
  3.     return warp;
  4. }
  5.  
  6. playerStart_coords Tile::get_playerStart()
  7. {
  8.     return playerStart;
  9. }
my errors are
'warp_coords' does not name a type
'playerStart_coords' does not name a type

I thought you could return structures like int string char and stuff like that. Any help would be great.
Apr 25 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Like the compiler says, 'warp_coords' and 'playerStart_coords' are not types.

However, Tile::warp_coords and Tile::playerStart are types.
Apr 25 '09 #4
sfuo
6
thanks it works great and I would have never figured it out
Apr 26 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chris Mantoulidis | last post by:
I have an abstract class BaseDate and have declared its functions as pure virtual. Then I have another class MathDate whose parent is BaseDate. In base date I have this functions (among...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
22
by: Michael Rasmussen | last post by:
Hi all, Not sure if this is OT? I have a function in a library written in C++ which returns a strdup(s.c_str()) to an application written i C. Running Valgrind on my C-application shows this...
17
by: Pietro Cerutti | last post by:
i Group, to my understanding, defining a function parameter as "const" means that the function is not going to change it. Why does the compiler says "return discards qualifiers from pointer...
2
by: beet | last post by:
Hi all, I tried to declare a c++ struct like following in a header file; I want to include this header file in other files to create and access this struct. ------ 1 #ifndef _SEARCHDATA_H_...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.