473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 3933
Banfa
9,065 Recognized Expert Moderator Expert
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 New Member
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_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.
Apr 25 '09 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Like the compiler says, 'warp_coords' and 'playerStart_co ords' are not types.

However, Tile::warp_coor ds and Tile::playerSta rt are types.
Apr 25 '09 #4
sfuo
6 New Member
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
1273
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 others): virtual BaseDate * set_day(int) = 0; I've got that function for month and year handling as well. I wanted
19
2632
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; } RtWidget;
6
2690
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 completed, for all declarations of that type, by ^^^ # declaring the same structure or union tag with its defining # content later in the same scope. ^^^^^ (6.2.5#23)
5
8732
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 conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
9
2675
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 type-stored on the stack Regards thomson
17
2446
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, typename V> Vector<U_or_V> operator+ ( const Vector<U>&, const Vector<V>&);
22
3255
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 results in memory leaks due to the c_str is never freed. Example:
17
12783
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 target type" when I *access* a member of an argument defined as const? Please see the code below:
2
281
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_ 2 #define _SEARCHDATA_H_ 3
0
8968
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.