1

On compiling following code I get error "expected unqualified-id before string constant"

In file "Notification_Constants.h"

namespace NOTIFICATION_CONSTANTS
{
    #define SERVICE_EMAIL "service@company.com"
}

In file SendEmail.cpp

#include "Notification_Constants.h"

void UserPreferences::get_senders_email(String &_email)
{
    _email = NOTIFICATION_CONSTANTS::SERVICE_EMAIL;
}

If i assign like following it works properly, what is the reason for the compilation error.

_email = SERVICE_EMAIL;

There is a similar question but the reason is not mentioned.

String class declaration with relevant methods

class String
{
public:

String();
String(const String& src);
String(const char *new_str);
String& operator=(const String& src);
String& operator=(const char *new_str);
};
Community
  • 1
  • 1
Sirish
  • 9,183
  • 22
  • 72
  • 107
  • SERVICE_EMAIL string is enclosed in double quotes, it was copy and paste mistake – Sirish May 05 '11 at 10:37
  • 1
    We do not have enough information to correctly identify the problem. Please at least provide more information about your `String` type. – Lstor May 05 '11 at 10:48
  • I added the quotes so others won't get confused. – Tamás Szelei May 05 '11 at 10:59
  • @Lstor I have updated the question with declaration of method in class String only with relevant methods – Sirish May 05 '11 at 11:04
  • On which line do you get the error? Are there any other errors? There shouldn't be an error according to the information you've provided so far. – Lstor May 05 '11 at 11:46
  • @Lstor I get the error for line _email = SERVICE_EMAIL; In the link to similar question I have mentioned it is the same problem but no one answered the reason for the same – Sirish May 05 '11 at 12:20
  • @Lstor one important point i missed is SERVICE_EMAIL is defined in another namespace, and I am accessing it as NAMESPACE::SERVICE_EMAIL.I updated the code accordingly, its error on myside as code is very complicated to copy and paste completely – Sirish May 05 '11 at 12:25
  • Ah. That's crucial information. The solution is posted below. – Lstor May 05 '11 at 12:36

4 Answers4

9

First, you should put quotation marks around the email address:

#define SERVICE_EMAIL "service@company.com"

Second, you should not use #define at all. Use a const variable instead:

const String SERVICE_EMAIL = "service@company.com";

#defines are type unsafe, have no scope and are generally evil.

Last, you may want to consider using std::string instead of your String class.

Update:

The problem is that the preprocessor #defines are nothing more than text substitutions. When the preprocessor is done, your compiler will see

_email = NOTIFICATION_CONSTANTS::"service@company.com";

There is no string constant in that namespace. SERVICE_EMAIL is not an identifier of any kind - it is just an indication to the preprocessor to substitute any occurrence of SERVICE_EMAIL with "service@company.com".

The solution is to remove the namespace qualifier:

_email = SERVICE_EMAIL;

Better solution:

If you do not have access to the #define, you should wrap it in a header file, if possible:

#include "Notification_Constants.h"

namespace NOTIFICATION_CONSTANTS {
    const String REAL_SERVICE_EMAIL = SERVICE_EMAIL;
}

and then use NOTIFICATION_CONSTANTS::REAL_SERVICE_EMAIL instead, which has scope, is a type, is a proper member of the namespace, and so on.

Lstor
  • 2,265
  • 17
  • 25
  • sorry SERVICE_EMAIL string is enclosed in double quotes, it was copy and paste mistake. Problem is it is defined in other domain which I cannot modify, how can i construct a string object out of it. – Sirish May 05 '11 at 10:37
1

The problem is, I think since I don't know the String type, is that SERVICE_EMAIL should be a string literal:

#define SERVICE_EMAIL "service@company.com"

but then, it should fail even in the latter solution. Maybe your code snippet doesn't show the real problem.

Simone
  • 11,655
  • 1
  • 30
  • 43
0
#define SERVICE_EMAIL "service@company.com"
Erik
  • 88,732
  • 13
  • 198
  • 189
0

What's the type of service@company.com? I think you wish to use a string to store an email address.

Try:

 #define SERVICE_EMAIL "service@company.com"
Heisenbug
  • 38,762
  • 28
  • 132
  • 190