如何在c ++中的构造函数中通过枚举类型获取入口 [英] How to get a entrance via enum type in a constructor in c++

查看:119
本文介绍了如何在c ++中的构造函数中通过枚举类型获取入口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......

我有一个类似的课程:

  class 错误{
public
enum ENUM_ERROR {
ERROR1001,
ERROR1002,
ERROR1004
};
std :: string ERROR(std :: string *);
};



我想使用enum作为条目:

 std: :string ERROR :: ERROR(  ERROR :: ENUM_ERROR错误 ){
std :: ostringstream ERROR1002(std :: ostringstream :: ate);
ERROR1002<< \ 033 [01; 31mERROR 1002:您必须在001和<<之间插入一个值; MAX_NODE<< !\ 033 [0m<< \ n \ 033 [01; 33m您已插入:;
switch (错误){
case ERROR :: ERROR1001:
{
return \ 033 [ 01; 31mERROR 1001:你必须插入一个整数!\ 033 [0m;
}
case ERROR :: ERROR1002:
{
return ERROR1002;
}
case ERROR :: ERROR1004:
{
return \ 033 [01; 31mERROR 1004:两个数字不能相等\ 033 [0m< /跨度>;
}
}
}



但是编译器给了我这三个错误:

 definition.h: 13  13 :错误:'ERROR'有 已被声明为
std :: string ERROR :: ERROR(ERROR :: ENUM_ERROR错误){
^
definition.h: 13 26 :错误:'错误'是 a class 命名空间
std :: string ERROR :: ERROR(ERROR :: ENUM_ERROR错误){
^
definition.h: 13 51 :error:expected','';'before'{'token
std :: string ERROR ::错误(ERROR :: ENUM_ERROR错误){
^





我做错了什么?

解决方案

Quote:

std :: string ERROR(std :: string *);



错误:如果你给的话普通方法的类的名称,然后编译器认为它是构造函数,但构造函数必须没有返回值。



Quote:

td :: string ERROR :: ERROR(ERROR :: ENUM_ERROR错误)



错误:此方法没有声明(因为签名不匹配)。此外,前一个错误的相同参数适用:类的名称是为构造函数保留的。



可能你的意思是:

< pre lang =c ++> class 错误{
public
enum ENUM_ERROR
{
ERROR1001,
ERROR1002,
ERROR1004
};
static std :: string错误(ENUM_ERROR ee);
};



 std :: string ERROR ::错误(ERROR :: ENUM_ERROR ee)
{
std :: ostringstream ERROR1002(std :: ostringstream :: ate);
// ..
}


这不是有效的C ++代码。您需要在使用之前声明所有类型。行 std :: string ERROR(std :: string *); 的目的不明确,似乎对我没有任何意义。我建议你删除它。或者改为创建一个构造函数。



也许你需要解释你的目标,但枚举类型可以完美地用在开关语句中,几乎就是你这样做的方式。你的编译错误与它完全无关。



-SA


JENOVA_SHINRA写道:



这就是NS2创建它的方式:

 < span class =code-keyword> int 命令( int  const   char  *  const  *); 
这个是它如何使用它:
AODV :: command(< span class =code-keyword> int argc, const char * const * argv){

不要告诉我某些软件创建无法编译的东西。 :-)



我不知道是什么让你想到为什么这个代码示例与你有任何关系。这不是很明显吗?你想成为一个函数的名称与类的名称相同,这立即使它不是一个有效的函数。这就是为什么我提到了一个构造函数。



让我告诉你我的想法:你的整个活动是一个很大的滥用:试图编写代码,没有任何线索使用您尝试使用的语言。这只是浪费时间,你和我们的。在这里,我花时间回答你的问题,但没有希望你可以使用答案,所以这只是浪费。提出某种类型的问题有其先决条件。在你至少阅读一些C ++书籍之前不要开始开发(稍后,你可以将它作为参考),当然,做一些简单的练习就像你去的那样非常有用。



-SA


Hi...
I have a class like so :

class ERROR {
public :
	enum ENUM_ERROR{
		ERROR1001,
		ERROR1002,
		ERROR1004
	};
	std::string ERROR(std::string*);
};


And i want to use the enum as a entry :

std::string ERROR::ERROR(ERROR::ENUM_ERROR error) {
	std::ostringstream ERROR1002 (std::ostringstream::ate);
	ERROR1002 << "\033[01;31mERROR 1002 : You must insert a value between 001 and " <<  MAX_NODE << "!\033[0m" << "\n\033[01;33mYou have inserted : ";
	switch(error) {
	case ERROR::ERROR1001:
	{
		return "\033[01;31mERROR 1001 : You must insert an integer number!\033[0m";
	}
	case ERROR::ERROR1002:
	{
		return ERROR1002;
	}
	case ERROR::ERROR1004:
	{
		return "\033[01;31mERROR 1004 : Both numbers must not be equal\033[0m";
	}
	}
}


but compiler gives me this three errors :

definition.h:13:13: error: ‘ERROR’ has not been declared
 std::string ERROR::ERROR(ERROR::ENUM_ERROR error) {
             ^
definition.h:13:26: error: ‘ERROR’ is not a class or namespace
 std::string ERROR::ERROR(ERROR::ENUM_ERROR error) {
                          ^
definition.h:13:51: error: expected ‘,’ or ‘;’ before ‘{’ token
 std::string ERROR::ERROR(ERROR::ENUM_ERROR error) {
                                                   ^



What am i doing wrong?

解决方案

Quote:

std::string ERROR(std::string*);


Wrong: if you give the name of the class to an ordinary method, then the compiler considers it a constructor, but constructors must NOT have return values.

Quote:

td::string ERROR::ERROR(ERROR::ENUM_ERROR error)


Wrong: there is no declaration for this method (because the signature doesn't match). Moreover, the same argument of the previous mistake applies: the name of the class is reserved for constructors.

Possibly you meant:

class ERROR {
public :
    enum ENUM_ERROR
    {
        ERROR1001,
        ERROR1002,
        ERROR1004
    };
    static std::string Error(ENUM_ERROR ee);
};


std::string ERROR::Error(ERROR::ENUM_ERROR ee) 
{
  std::ostringstream ERROR1002 (std::ostringstream::ate);
  //..
}


This is not a valid C++ code. You need declare all types before using them. The purpose of the line std::string ERROR(std::string*); is not clear and does not seem to make any sense to me. I suggest you just remove it. Or create a constructor instead.

Perhaps you need to explain your goals, but the enumeration type can be perfectly used in the switch statement, pretty much the way you do it. Your compilation errors are totally unrelated to it.

—SA


JENOVA_SHINRA wrote:


This is how NS2 creates it:

int command(int, const char *const *);
and this is how it uses it :
AODV::command(int argc, const char*const* argv) {

Don't tell me that some software "creates" something which would not compile. :-)

I have no idea what makes you thinking why this code sample has anything to do with yours. Isn't it quite obvious? Your wanna-be-a-function has the name which is the same as the name of the class, which immediately makes it not a valid function. That's why I mentioned a constructor.

Let me tell you what I think: your whole activity is one big abuse: trying to write "code" having no clue of the use of the language you are trying to use. This is just a waste of time, yours and ours. Here, I spend time on answering your questions, but have little hope that you can use the answers, so it would be just the waste. Asking questions of certain type has its prerequisites. Don't start development before you read some C++ book at least once (later on, you can use it as a reference), of course, doing some simple exercises as you go would be very useful.

—SA


这篇关于如何在c ++中的构造函数中通过枚举类型获取入口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆