c++ - 如何在静态成员函数中使用其他静态成员——C++
问题描述
下面我有一堂课
#include <iostream>
#include <vector>
using namespace std;
class Car
{
public:
static int b;
static char* x1(int x)
{
b=x;
return (char *)"done";
}
};
主要
int main()
{
char* ret = Car::x1(42);
for(int x=0;x<4;x++)
{cout<<ret[x]<<endl;}
return 0;
}
但我收到以下错误
/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
更新
在 Serge Balesta 发表评论后,我将 x1 函数更改为包含 Car::b=x;
,但出现此错误
/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
更新功能
static char* x1(int x)
{
Car::b=x;
return (char *)"done";
}
解决方案
推荐阅读
- python-3.x - 如何使用python将xml块添加到原始xml结构中
- javascript - 无法在父组件中运行子组件方法
- python-3.x - 皮威。如何选择所有带有预取字段的外键字段的模型项?
- asp.net-core - FHIR 的通用数据类型对象从 .NET Core 中的 JSON 解析
- ios - 如何更改标签栏控制器的选定索引?
- c# - 为什么我的查询(包含作为命令文本的 sql 文件)超时?
- .net - 从 NATS 切换到 RabbitMQ
- javascript - 角度图像上传不适用于 Internet Explorer,但它适用于谷歌浏览器
- awesome-wm - awesome-wm:为切换视图桌面创建快捷键
- javascript - window.history 删除除当前 url 之外的历史堆栈