473,396 Members | 2,129 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,396 software developers and data experts.

Error with typedef inside a class...

gpraghuram
1,275 Expert 1GB
I wrote the following code
Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. //typedef map<int,int> Maps;
  6.  
  7. class A
  8. {
  9. public:
  10.                 typedef map<int,int> Maps;
  11.  
  12.     private:
  13.         Maps *m_MapP;
  14.     public:
  15.         Maps* getMapP();
  16.         int* getIntP();
  17. };
  18.  
  19. Maps* A::getMapP()
  20. {
  21.     return m_MapP;
  22. }
  23.  
  24. int* A::getIntP()
  25. {
  26.     return NULL;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     return 0;
  33. }
  34.  
When i compile this i am getting compiler error in the method definition of getMapP.
But when i move the typedf declaration above the class then it is working fine.
I cant figure out why this is happeneing.
can somebody comment on this.

Thanks
Raghuram
Apr 4 '08 #1
4 2459
I wrote the following code
Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. //typedef map<int,int> Maps;
  6.  
  7. class A
  8. {
  9. public:
  10.                 typedef map<int,int> Maps;
  11.  
  12.     private:
  13.         Maps *m_MapP;
  14.     public:
  15.         Maps* getMapP();
  16.         int* getIntP();
  17. };
  18.  
  19. Maps* A::getMapP()
  20. {
  21.     return m_MapP;
  22. }
  23.  
  24. int* A::getIntP()
  25. {
  26.     return NULL;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     return 0;
  33. }
  34.  
When i compile this i am getting compiler error in the method definition of getMapP.
But when i move the typedf declaration above the class then it is working fine.
I cant figure out why this is happeneing.
can somebody comment on this.
Because the Maps typedef is inside your class, you need to scope it accordingly.

Expand|Select|Wrap|Line Numbers
  1. A::Maps* A::getMapP()
  2.  
should do the trick.

Regards,

-Joby
Apr 4 '08 #2
gpraghuram
1,275 Expert 1GB
Because the Maps typedef is inside your class, you need to scope it accordingly.

Expand|Select|Wrap|Line Numbers
  1. A::Maps* A::getMapP()
  2.  
should do the trick.

Regards,

-Joby
What u say is right.
But why we have to specify the scope resolution for the return type alone.
But for argument you need not specify this.
Check this code
Expand|Select|Wrap|Line Numbers
  1. A::Maps* A::getMapP()
  2. {
  3. }
  4. A::Maps* A::getMapP(Maps *p)//This works fine without A:: 
  5.  
My doubt is what is C++ syntax for this typedef,,,that is what my question is.


Raghuram
Apr 5 '08 #3
What u say is right.
But why we have to specify the scope resolution for the return type alone.
But for argument you need not specify this.
Check this code
Expand|Select|Wrap|Line Numbers
  1. A::Maps* A::getMapP()
  2. {
  3. }
  4. A::Maps* A::getMapP(Maps *p)//This works fine without A:: 
  5.  
My doubt is what is C++ syntax for this typedef,,,that is what my question is.


Raghuram
Interesting. My guess here is that the default scope of the arguments of a method are the same as the scope of the method itself. In other words, the compiler checks A:: for a definition of Maps before checking globally.

As I said, this is just a guess, maybe a browse through Stroustrup will explain this definitively.
Apr 7 '08 #4
gpraghuram
1,275 Expert 1GB
What i thought is
The typedef gets resolved only inside the class method and the class method signature.
As return type is not part of signature , i think this is not getting resolved.

Other experts please correct me if i am wrong

Raghuram
Apr 7 '08 #5

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

Similar topics

0
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and...
5
by: Mr A | last post by:
Hi! I'm trying to do the following: emplate <typename Resource> class ResourceManager { public: typedef std::list<Resource*>::iterator Iterator; typedef std::list<Resource*>::const_iterator...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
3
by: Rohini | last post by:
Hi , I am getting the following LINK 2005 error when I tried to build my project in vc++7.1. I am doing the build process in win32release mode. I have searched google on this but whatever I...
7
by: Alden Pierre | last post by:
Hello, I'm trying to create my own user define container, but I'm having a little hard time figuring out why is my class considered undefined by my compiler. Here is the following code. //...
7
by: for.fun | last post by:
Hi everybody, I have the following problem : B class need A::MyEnum type and A class need B::MyEnum type. In both case, the class type is incomplete so it is obvious that the ::MyEnum can not...
3
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i...
2
by: sandy | last post by:
I am trying to upper case a string, so I have this method: string FileSystem::toupper(string S) { for (int i=0; i<S.length(); ++i) { S=toupper(S); } return S;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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
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...
0
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,...

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.