Click here to Skip to main content
15,886,807 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
//.CPP file 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
namespace XNameSpace{

	UserId ClassName::GetId() // 'GetId' is not a member of ClassName
	{
		String^ strUserName = "sdfasd";
		String^ strPasscode = "sdfa";
		UserId ^ObjRole= AuthenticationManager::NewKTRole();

		Dictionary<String^, Object^>^ users= gcnew Dictionary<String^, Object^>();

		users->Add("UserName", strUserName);
		usersusers->Add("Passcode", strPasscode);

		UserId Id = (UserId)ObjRole->GetId(users);

		return Id;
	};
}

//.H FILE
C++
#include "stdafx.h"

namespace XNameSpace{
	
	class ClassName
	{
	public:
		UserId GetId(); // Error 'GetId' Unknown override specifier // syntax error : '(' , unexpected token preceding ;
	};

	enum UserId
	{
		KTUniversal = -1,

		None,
		//
		C4,
		//
		C5,
		//
		C1,
		//
		C2,
		//
		C3,
	};


What I have tried:

I am getting errors
1) 'GetId' Unknown override specifier
2) syntax error : '('
3) unexpected token preceding ;
4) 'GetId' is not a member of ClassName
Usually we keep declaration in header and its implementation in CPP file.

Could anyone help on this topic?
Posted
Updated 1-Nov-17 22:19pm
v2
Comments
Afzaal Ahmad Zeeshan 2-Nov-17 4:16am    
Your UserId is an enum, and you are expecting to call a function on it. Any reason for that?

Move the enum definition above the class definition in the header file or add a forward declaration:
namespace XNameSpace{

    // Forward declaration for UserId enum
    enum UserId;

    // Can now use the UserId enum here
    class ClassName
    {
    public:
        UserId GetId();
    };
    
    // enum definition
    enum UserId
    {
        KTUniversal = -1,
        // ...
    };
    // ...
}
Your enum is treated like a class: It must be declared before first used. The only exception is when it is part (member) of a class. Then it might be defined after being used by other members:
C++
class ClassName
{
public:
    // Can use ClassNameUserId here because it is a class member
    ClassNameUserId GetId();

    enum ClassNameUserId
    {
        KTUniversal = -1,
    };
};
 
Share this answer
 
Comments
KarstenK 2-Nov-17 4:21am    
It is best to also implement the enums in the beginning of header file. Sometimes it makes sense to make the enums public.
Jochen Arndt 2-Nov-17 4:29am    
Of course. Therefore, I suggested that (which lets the enum being public).

I provided the additional examples to show all possible solutions and help explaining why he got those errors.
Quote:
UserId GetId();</blockquoteUserId type is undeclared.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900