#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 an 'int' in this line:
myInt A::getK() { return k; }
How can I get the compiler to recognize myInt as int?
Syntax. Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways.
typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.
A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.
typedef
creates synonyms, not new types, so myInt
and int
are already the same. The problem is scope — there is no myInt
in a global scope, you have to use A::myInt
outside of the class.
A::myInt A::getK() { return k; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With