gpt4 book ai didi

c++ - 在 C++ 类中导致编译错误的结构

转载 作者:行者123 更新时间:2023-11-28 02:20:49 26 4
gpt4 key购买 nike

我在编译 C++ 类时遇到错误,它与要从方法返回的 Struct 相关。我已将代码 strip 化到最低限度,但仍然出现错误。我正在使用 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)

我为什么会收到这些错误有什么想法吗?

谢谢

最佳答案

ProductInfoTestClass 中的嵌套类,因此您必须在此处保留命名空间。

TestClass::ProductInfo TestClass::GetProdInfo()

标准说:

9.7 Nested class declarations

If class X is defined in a namespace scope, a nested class Y may bedeclared in class X and later defined in the definition of class X orbe later defined in a namespace scope enclosing the definition ofclass 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 asspecified in 7.3.1.2). Such a redeclaration has the same enclosing namespaces as the original declaration.

关于c++ - 在 C++ 类中导致编译错误的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32595213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com