繁体   English   中英

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

[英]Struct Causing Compile Error in C++ Class

我在编译C ++类时遇到错误,它与要从方法返回的Struct有关。 我已经将代码缩减到最低限度,但仍然收到错误。 我正在使用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嵌套类声明

如果类X是在名称空间范围中定义的,则嵌套类Y可以在类X中声明,然后在类X的定义中定义,或者稍后在包含类X的定义的名称空间范围中定义。

7.3.1命名空间定义

声明的包围名称空间是声明按词法出现的那些名称空间,除了在其原始名称空间(例如7.3.1.2中指定的定义)之外重新声明名称空间成员之外。 这样的重新声明具有与原始声明相同的封闭名称空间。

您必须取消嵌套您的结构或更改GetProdInfo的返回类型

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

    return PI;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM