0

I am getting errors compiling a C++ class, it relates to a Struct which is to be returned from a method. I have striped the code down to the minimum and still get the errors. I am using Visual Studio 6.0.

Code

// 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_)

Errors received

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)

Any ideas why i am getting these errors?

Thanks

0

2 Answers 2

2

ProductInfo is a nested class in TestClass so you must keep the namespace here.

TestClass::ProductInfo TestClass::GetProdInfo()

The standard says:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, thanks mate. I knew i was missing something simple.
Done, was waiting the required 5 mins before accepting
0

You must either un-nest your struct or change the return type of GetProdInfo

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

    return PI;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.