C ++ typedef and return types: how to make the compiler recognize a return type created using typedef?

#include <iostream> using namespace std; class A { typedef int myInt; int k; public: A(int i) : k(i) {} myInt getK(); }; myInt A::getK() { return k; } int main (int argc, char * const argv[]) { A a(5); cout << a.getK() << endl; return 0; } 

myInt is not recognized by the compiler as 'int' in this line:

 myInt A::getK() { return k; } 

How can I make the compiler recognize myInt as int?

+7
source share
3 answers

typedef creates synonyms, not new types, so myInt and int already the same. The problem is in scope - there is no myInt in the global myInt , you must use A::myInt outside the class.

 A::myInt A::getK() { return k; } 
+19
source

A::myInt A::getK() { return k; }

You must define the typedef type because you created it inside scope A

+2
source

Put the definition outside the class.

-2
source

All Articles