chrise
chrise

Reputation: 4253

assignment of read-only location of non const variable

Why does the below macro fail

#define XML_TEST(K, V) K = V;

struct teststruct {
    teststruct() {
        TEST("x_", 10);
    }
    int x_;
};

This doesnt compile with the following error:

test.h:30: error: assignment of read-only location ‘"x_"’
#define XML_TEST(K, V) K = V;);

simply doing

teststruct() {
    x_ = 10;
}                   ^

works fine. Also I dont see anything being const here. Does anyone have any idea what I am missing here?

Upvotes: 0

Views: 335

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70277

TEST("x_", 10);

will process into

"x_" = 10;

which is most definitely a problem, as we're assigning to a constant string. I believe you mean

TEST(x_, 10);

Upvotes: 4

Related Questions