在 C++ 中导致编译错误的结构 Class
Struct Causing Compile Error in C++ Class
我在编译 C++ class 时遇到错误,它与要从方法返回的结构有关。我已将代码条带化到最低限度,但仍然出现错误。我正在使用 Visual Studio 6.0.
代码
// 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)
知道我为什么会收到这些错误吗?
谢谢
ProductInfo
是 TestClass
中的嵌套 class,因此您必须在此处保留命名空间。
TestClass::ProductInfo TestClass::GetProdInfo()
标准说:
9.7 Nested class declarations
If class X is defined in a namespace scope, a nested class Y may be
declared in class X and later defined in the definition of class X or
be later defined in a namespace scope enclosing the definition of
class X.
7.3.1 Namespace definition
The enclosing namespaces of a declaration are those namespaces in which the declaration lexically appears,
except for a redeclaration of a namespace member outside its original namespace (e.g., a definition as
specified in 7.3.1.2). Such a redeclaration has the same enclosing namespaces as the original declaration.
您必须取消嵌套结构或更改 GetProdInfo
的 return 类型
TestClass::ProductInfo TestClass::GetProdInfo()
{
ProductInfo PI;
return PI;
}
我在编译 C++ class 时遇到错误,它与要从方法返回的结构有关。我已将代码条带化到最低限度,但仍然出现错误。我正在使用 Visual Studio 6.0.
代码
// 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)
知道我为什么会收到这些错误吗?
谢谢
ProductInfo
是 TestClass
中的嵌套 class,因此您必须在此处保留命名空间。
TestClass::ProductInfo TestClass::GetProdInfo()
标准说:
9.7 Nested class declarations
If class X is defined in a namespace scope, a nested class Y may be declared in class X and later defined in the definition of class X or be later defined in a namespace scope enclosing the definition of class X.
7.3.1 Namespace definition
The enclosing namespaces of a declaration are those namespaces in which the declaration lexically appears, except for a redeclaration of a namespace member outside its original namespace (e.g., a definition as specified in 7.3.1.2). Such a redeclaration has the same enclosing namespaces as the original declaration.
您必须取消嵌套结构或更改 GetProdInfo
TestClass::ProductInfo TestClass::GetProdInfo()
{
ProductInfo PI;
return PI;
}