![](/img/trans.png)
[英]C++ typedef with nested templates is not a class, struct, or union type
[英]C++ Class Typedef Struct does not name a type
我正在嘗試在我的C ++程序中使用typedef結構。 我開始編寫以下代碼,直到我在嘗試向我的類添加一個返回模板typedef結構指針的方法時收到錯誤。
StructSource.h
template <typename T>
class StructSource {
public:
struct TestStruct{
T value;
};
};
User.h
#include "StructSource.h"
class User {
public:
typedef StructSource<int>::TestStruct IntStruct;
IntStruct *getIntStruct();
};
User.cpp
#include "User.h"
IntStruct *User::getIntStruct() {
return 0;
}
在使用GCC進行編譯時,會出現以下錯誤。
User.cpp:3:1:錯誤:'IntStruct'沒有命名類型
我無法解釋為什么會這樣。 我錯過了什么類型的信息?
“用戶”也是一個“命名空間”(范圍,實際上,正如大多數評論者指出的那樣 - “命名空間”用於快速回答),所以你必須使用
User::IntStruct *User::getIntStruct() {
return 0;
}
你需要:
User::IntStruct *User::getIntStruct() { ... }
IntStruct在User
范圍內定義,但函數的返回值不在范圍內。 有這些范圍的問題的討論在這里 。
由於IntStruct
是在類中定義的,因此必須使用類名來引用它,除非在類代碼中它是默認名稱搜索路徑的一部分。
User::IntStruct *User::getIntStruct() {
這是不一樣的命名空間,即使語法是相同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.