[英]C++ typedef and return types: how to get the compiler to recognize the return type created with 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識別為此行中的“int”:
myInt A::getK() { return k; }
如何讓編譯器將myInt識別為int?
typedef
創建同義詞,而不是新類型,因此myInt
和int
已經相同。 問題是范圍 - 全局范圍內沒有myInt
,你必須在類之外使用A::myInt
。
A::myInt A::getK() { return k; }
A::myInt A::getK() { return k; }
您必須限定typedef
類型,因為您在A
類范圍內創建了它。
把定義放在類之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.