在另一个类中使用一个类的静态对象
Use of static object of one class in the other
我试图测试调用静态对象从一个类在另一个。我在这里得到链接错误LNK2001:未解析的外部符号"公共:静态类K G::ob1"我不知道出了什么问题,在互联网上我找不到任何关于静态对象的信息,只有静态与所有其他配置。因此,我请求你的帮助。我是否需要创建一个K的对象来让这个整体起作用,或者我是否可以抽象到不创建任何对象?
#include <iostream>
using namespace std;
class K
{
int a;
public:
K(int x) { a = x; };
void print() { cout << " a is: " << a << endl; };
};
class G
{
public:
static K ob1;
static void printG()
{
ob1.print();
};
};
int main()
{
K o1(10);
G::printG();
system("pause");
}
必须定义静态变量。它类似于c中的全局变量。
K G::ob1;
int main(){ .... }
相关文章:
- 分离一个静态常量 std::thread?
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- 我可以使用 constexpr 函数声明一个静态数组吗?
- 我正在尝试在我的类中创建一个静态成员,但编译器警告我它是未定义的
- 一个静态变量多副本
- 两个C++库如何共享一个静态缓冲区?
- 为什么静态指针返回函数中有一个"静态"键?
- C++ 给出编译器错误,将一个(静态)常量全局变量设置为另一个静态常量变量
- cmake-创建一个静态库
- 对另一个静态库中静态库方法的未定义引用
- 如何创建一个静态类来引用带有C++的字符串变量
- QMAKE:将静态库链接到另一个静态库
- 长镜头 -- 可能有一个静态类成员,该类成员具有访问非静态成员的 lambda
- 为 c++ 构建一个静态库,该库在内部依赖于第三方库
- 使用类模板参数推导来创建一个静态接口
- 具有两个动态维度和一个静态维度的多维数组
- 创建一个静态包含ffmpeg的共享库
- NetBeans创建一个静态库C
- 在抽象类中,可以使用抽象类A的类型创建一个静态字段