2015-09-15 44 views
0

我收到編譯C++類的錯誤,它涉及將從方法返回的Struct。我已經將代碼分解到最低限度,並仍然得到錯誤。我正在使用Visual Studio 6.0。在C++類中導致編譯錯誤的結構

代碼

// TestClass.cpp: implementation of the TestClass class. 
// 
////////////////////////////////////////////////////////////////////// 

#include "stdafx.h" 
#include "TestClass.h" 

////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 

TestClass::TestClass() 
{ 

} 

TestClass::~TestClass() 
{ 

} 

ProductInfo TestClass::GetProdInfo() 
{ 
    ProductInfo PI; 

    return PI; 
} 

// TestClass.h: interface for the TestClass class. 
// 
////////////////////////////////////////////////////////////////////// 

#if !defined(AFX_TestClass_H__081E411D_44F9_4E0B_9FE7_CF6F708BE769__INCLUDED_) 
#define AFX_TestClass_H__081E411D_44F9_4E0B_9FE7_CF6F708BE769__INCLUDED_ 

#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 

class TestClass 
{ 
public: 
    struct ProductInfo 
    { 
     char cCode; 
     char cItem[20]; 
     long lValue; 
    }; 

public: 
    TestClass(); 
    virtual ~TestClass(); 

private: 
    ProductInfo GetProdInfo(); 
}; 

#endif // !defined(AFX_TestClass_H__081E411D_44F9_4E0B_9FE7_CF6F708BE769__INCLUDED_) 

收到的錯誤

Compiling... 
TestClass.cpp 
C:\Work\TestStruct\TestClass.cpp(22) : error C2143: syntax error : missing ';' before 'tag::id' 
C:\Work\TestStruct\TestClass.cpp(22) : error C2501: 'ProductInfo' : missing storage-class or type specifiers 
C:\Work\TestStruct\TestClass.cpp(22) : fatal error C1004: unexpected end of file found 
Error executing cl.exe. 

TestStruct.exe - 3 error(s), 0 warning(s) 

任何想法,爲什麼我得到這些錯誤?

感謝

回答

2

ProductInfoTestClass嵌套類,所以你必須在這裏保留的命名空間。

TestClass::ProductInfo TestClass::GetProdInfo() 

標準表示:

9.7嵌套類聲明

如果類X在命名空間範圍被定義,一個嵌套類ý可以聲明 X類或稍後在類X或 的定義中定義的類稍後將在包含defini的命名空間範圍中定義 類X.和灰

7.3.1命名空間定義

聲明的封裝命名空間是在該聲明詞法出現, 除了一個空間成員的其原始名稱空間外部的重新聲明這些命名空間(例如,在7.3.1.2中規定的定義爲 )。這樣的重新聲明與原始聲明具有相同的封閉名稱空間。

+0

輝煌,感謝隊友的返回類型。我知道我錯過了簡單的事情。 –

+0

完成,正在等待所需的5分鐘才能接受 –

0

您必須取消巢你的結構或改變GetProdInfo

TestClass::ProductInfo TestClass::GetProdInfo() 
{ 
    ProductInfo PI; 

    return PI; 
} 
相關問題