views:

241

answers:

7

Alright, so this is annoying the hell out of me and I'm sure its a simple thing to do. Basically, I'm working with an open source C++ client called POCO to make a email client for a class...

Basically, I have a pop3 client object that retrieves emails from my email server, and then puts the emails in an object called MailMessage. Now, I want to be able to get my attachments, and the only functionality it seems that I have to do that is the following function:

static const std::string & contentTransferEncodingToString(
    ContentTransferEncoding encoding
);

Problem is, I had no idea what the following was:

ContentTransferEncoding encoding

After digging into the source code, I found out it has something to do with "enums" (this is public by the way):

enum ContentTransferEncoding
    {
     ENCODING_7BIT,
     ENCODING_8BIT,
     ENCODING_QUOTED_PRINTABLE,
     ENCODING_BASE64
    };

Basically, the attachment I'm trying to open uses 7 bit encoding. Does ANYONE know how to deal with these enums, and how I can pass them into the contentTransferEncodingToString function?

Thank you so much for your efforts :)

EDIT:

So, unreal, but I didn't realize that the function I was trying to access was protected, it wasn't the enums, so the way you all suggested to access the enums was correct! And I guess the way I was trying to access them was also correct =P. Just a big stupid mistake.

But thanks for all your efforts!!! Great community :)

+2  A: 

Very simple, just call your function with an element of the enum:

std::string str = contentTransferEncodingToString(ENCODING_8BIT);

Enums are a enumeration. You could get the same result by defining a bunch of

const int ENCODING_7BIT = 0;
const int ENCODING_8BIT = 1;

However, what would happen if you pass a 8 to your function? Defining an enum allows to

  • Restrict the number of items you allow
  • Don't have to worry about how it is represented (thus having some abstraction)
Tristram Gräbener
copying the string
Ruben Bartelink
I have, but it definitely does not work. I get the following:'ENCODING_8BIT' : undeclared identifier
shawnjan
I tried your modified version but to no avail... I got the following:'Poco::Net::MailMessage::contentTransferEncodingToString' : cannot convert parameter 1 from 'const int' to 'Poco::Net::MailMessage::ContentTransferEncoding'
shawnjan
Oh. Don't you have a namespace problem then ?
Tristram Gräbener
The modification was just to explain what enums where about (on the implementation side, not on the user side)
Tristram Gräbener
Ah, well I'm still quite confused :S. I dont understand what the compiler wants me to pass it...
shawnjan
That error message suggests that you could refer to the enum constant as `Poco::Net::MailMessage::ENCODING_7BIT`. Try passing that to the function.
sth
Didn't work, says I couldnt access the protected variable in MailMessage...
shawnjan
+4  A: 

You can either say

const std::string& s =  contentTransferEncodingToString(ENCODING_7BIT)

or

const std::string& s =  contentTransferEncodingToString(ContentTransferEncoding::ENCODING_7BIT)
Ruben Bartelink
I Ruben, it didn't seem to work... The first option had the following error:'ENCODING_8BIT' : undeclared identifier and the second option says that ContentTransferEncoding is a protected variable... Interesting since ContentTransferEncoding is public in the source code...
shawnjan
Assuming you're using VS, you could press F12 (go to declaration) to find out what ContentTransferEncoding is referring to (it might be e.g. a protected variable). If there are 2 and you're getting the wrong one, you'll need to disambiguate by putting the namespace prefixes in front of things
Ruben Bartelink
The other thing you could do is derive a class from MailMessage and add a method that uses ContentTransferEncoding in there [as one would be able to acccess protected enum values in that context]
Ruben Bartelink
The second example is not std C++. Enumerations don't (yet) have a scope. After a bit of searching, it seems that these enum's are declared in the class MailMessage inside namespace Net in namespace Poco. So the correct qualification should be: Poco::Net::MailMessage::ENCODING_7BIT.
Richard Corden
@RC: For once, I had actually tested :P MSVC says: warning C4482: nonstandard extension used: enum 'A' used in qualified name (Looks like there are bigger problems here than this though :D)
Ruben Bartelink
+2  A: 
chedi
HI! I've actually tried that (the name is Poco:Net:MailMessage not Message), but it doesn't work, the problem is ContentTransferEncoding is protected...
shawnjan
my bad, Poco:Net:MailMessage. can you paste your entire code and error message ?
chedi
+2  A: 

Is the class that tries to call contentTransferEncodingToString inheriting from Poco::NET::MailMessage?

The method contentTransferEncodingToString is protected not public and can thus only be called from a class that inherits MailMessage.

If this isn't the problem could you please post the error message exactly as printed by the compiler.

Joakim Karlsson
+2  A: 

It's true that the function you are calling is protected and static, which meant you have must have something like this :


class test : public Poco::Net::MailMessage{
  pubic:
    std::string myFunc(){
       // you can you the protected function here
       return ContentTransferEncoding(ENCODING_7BIT);
       // or 
       // because you have inherited all the class
       // return CTE_7BIT;

}
chedi
missing an l in there :D
Ruben Bartelink
+2  A: 

Does ANYONE know how to deal with these enums, and how I can pass them into the contentTransferEncodingToString function?

Several answers have shown the basic way to use enums. When trying them you're getting error messages that the syntax is correct, but that the methods that use those enums are not accessible from the scope you're in.

The answer, then, is to get into a scope where you can access what you want.

The methods in question are apparently protected, which means the way to access them is through a derived class. I'm not saying this is good design, but it's clearly what POCO's designers expect you to use.

Max Lybbert
According to the docs the enum ContentTransferEncoding is not protected. (Protected members are defined by a blue icon with a P inside.) - http://www.appinf.com/docs/poco/Poco.Net.MailMessage.html#16477
Joakim Karlsson
Edited, based on final answer by shawnjan
Max Lybbert
A: 

So, unreal, but I didn't realize that the function I was trying to access was protected, it wasn't the enums, so the way you all suggested to access the enums was correct! And I guess the way I was trying to access them was also correct =P. Just a big stupid mistake.

But thanks for all your efforts!!! Great community :)

shawnjan