Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi...
I have a class like so :
C++
class ERROR {
public :
	enum ENUM_ERROR{
		ERROR1001,
		ERROR1002,
		ERROR1004
	};
	std::string ERROR(std::string*);
};

And i want to use the enum as a entry :
C++
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 :
C++
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?
Posted
Comments
Sergey Alexandrovich Kryukov 10-Sep-15 3:38am    
What did you mean by
std::string ERROR(std::string*);?
—SA
JENOVA_SHINRA 10-Sep-15 3:52am    
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) {
JENOVA_SHINRA 10-Sep-15 3:44am    
I just wanted to create a entry like i saw in NS2...
I saw in NS2 that so many variables has been created like that.

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:
C++
class ERROR {
public :
    enum ENUM_ERROR
    {
        ERROR1001,
        ERROR1002,
        ERROR1004
    };
    static std::string Error(ENUM_ERROR ee);
};

C++
std::string ERROR::Error(ERROR::ENUM_ERROR ee) 
{
  std::ostringstream ERROR1002 (std::ostringstream::ate);
  //..
}
 
Share this answer
 
Comments
JENOVA_SHINRA 10-Sep-15 4:06am    
Thanks so much... It really worked.
CPallini 10-Sep-15 4:10am    
You are welcome.
Sergey Alexandrovich Kryukov 10-Sep-15 4:25am    
5ed.
—SA
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
 
Share this answer
 
Comments
CPallini 10-Sep-15 4:10am    
Have my 5.
Sergey Alexandrovich Kryukov 10-Sep-15 4:26am    
Thank you, Carlo.
Believe or not, but I just posted another, bigger one, Solution 3, in response to the comment which reveals that the inquirer is not really getting it. :-)
—SA
JENOVA_SHINRA wrote:

This is how NS2 creates it:
C++
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
 
Share this answer
 
Comments
JENOVA_SHINRA 10-Sep-15 7:55am    
Thanks anyway ;)
Sergey Alexandrovich Kryukov 10-Sep-15 10:50am    
You are welcome.
But are you getting the idea? Are you going to accept the answer formally?
—SA
JENOVA_SHINRA 12-Sep-15 10:31am    
Yes
Thanks to your advice.
Sergey Alexandrovich Kryukov 13-Sep-15 12:08pm    
You are very welcome, but I don't see that the answer is formally accepted.
—SA
JENOVA_SHINRA 14-Sep-15 3:49am    
There ;)

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