2

below I have a class

#include <iostream>

#include <vector>
using namespace std;

class Car
{

public:
static int b;


static char* x1(int x)
{
    b=x;
    return (char *)"done";
}

};

in main

int main()
{
    char* ret = Car::x1(42);
    for(int x=0;x<4;x++)
    {cout<<ret[x]<<endl;}
  
    return 0;
}

but I am getting following error

/usr/bin/ld: /tmp/ccRfvgz8.o: warning: relocation against `_ZN3Car1bE' in read-only section `.text._ZN3Car2x1Ei[_ZN3Car2x1Ei]'
/usr/bin/ld: /tmp/ccRfvgz8.o: in function `Car::x1(int)':
main.cpp:(.text._ZN3Car2x1Ei[_ZN3Car2x1Ei]+0xc): undefined reference to `Car::b'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

Update

after the comment by Serge Balesta I changed x1 function to include Car::b=x; but I get this error

/usr/bin/ld: /tmp/ccjMKe4u.o: warning: relocation against `_ZN3Car1bE' in read-only section `.text._ZN3Car2x1Ei[_ZN3Car2x1Ei]'
/usr/bin/ld: /tmp/ccjMKe4u.o: in function `Car::x1(int)':
main.cpp:(.text._ZN3Car2x1Ei[_ZN3Car2x1Ei]+0xc): undefined reference to `Car::b'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

updated function

static char* x1(int x)
{
    Car::b=x;
    return (char *)"done";
}
user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    In C++ string literals are a `const char *`. They are ***not*** a `char *`, and casting them to a `char *` does not fix this bug. – Sam Varshavchik Jun 29 '21 at 12:40
  • You need a proper *definition* for static members. Here you should add `int Car::b;` just before `main`. – Serge Ballesta Jun 29 '21 at 13:02
  • @SergeBallesta see the question update after I add what u said. now different error – user786 Jun 29 '21 at 15:44
  • You have to define a static duration object: its definition must occur at file level outside of any function: `...` `int Car::b;` `int main() {...` – Serge Ballesta Jun 30 '21 at 06:33
  • @SergeBallesta Can u please explain it in English. Or u think I know all these "duration object" and etc. – user786 Jun 30 '21 at 09:15
  • If you want to learn C++, you will have to learn those terms ;-) . Static duration if for *something* (what I call an object) which will last for the whole program. That is variables declared at file level (outside of any function) **and static members of classes**. Long story made short, a *static member* has to be defined at file level and not inside a function. – Serge Ballesta Jun 30 '21 at 09:50
  • @SergeBallesta Thanks for ur very helpful and detailed comment. But can u please add that in my question and please if u can show me how it's done. I am new to c++ but I am some level of good in C – user786 Jun 30 '21 at 11:48
  • Just write `int Car::b;` exactly one line above `int main()`. – Serge Ballesta Jun 30 '21 at 12:05
  • @SergeBallesta and where the declaration will go? – user786 Jun 30 '21 at 12:55
  • I have written a full code example as an [additional anwser](https://stackoverflow.com/a/68195380/3545273) to the duplicate. – Serge Ballesta Jun 30 '21 at 13:07

0 Answers0