Unqualified-id before string constant

江枫思渺然 提交于 2019-12-07 07:22:26

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.

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.

#define SERVICE_EMAIL "service@company.com"

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!