visual如何在托管C++类中为静态变量赋值
visual How can I give a value to a static variable in a managed C++ class?
在VC++CLR项目中,我有一个类temp。我正在尝试将静态变量temp1设置为5。
我得到一个编译错误:
错误32错误LNK2020:未解析的令牌(0A0005FB)";公用:静态int temp::temp1";(?temp1@temp@@2HA)C:\Users\user100\Documents\VisualStudio 2012\NewProject 32位\从数据创建最小条形图2\create最小数据条形图\从数据创建最小条形图5.obj
错误33错误LNK2001:未解析的外部符号";public:静态inttemp::temp1";(?temp1@temp@@2HA)C:\Users\user100\Documents\VisualStudio 2012\NewProject 32位\从数据创建最小条形图2\create最小数据条形图\从数据创建最小条形图5.obj
我该怎么修?
class temp
{
public:
static int temp1;
};
int main(array<System::String ^> ^args)
{
temp::temp1 = 5;
}
在类中声明静态变量时,实际上不会创建内存。你需要一个单独的变量callout来为它创建RAM。这就是编译器告诉你的。
//Outside your class declaration:
int temp::temp1;
定义静态成员变量:
class temp
{
public:
static int temp1;
};
int temp::temp1 = 0;
// Fixed main() ;)
int main(int argc, char** argv)
{
temp::temp1 = 5;
return 0;
}
相关文章:
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 模板基类中的静态变量
- 类和静态变量
- 不同作用域中的静态变量和全局变量
- 静态变量声明和定义
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 在类中继承静态变量?
- "local scope"中的 C++ 初始化静态变量
- 使用静态变量的递归调用的不同输出
- 复制文件流C++静态变量
- 跨模板化函数编译的静态变量
- C++编译器是否优化了顺序静态变量读取?
- C++,每个循环初始化一个新的静态变量
- (为什么)我们可以在初始化中将非静态类成员分配给静态变量吗?
- 这些语句是否等效(静态变量、常量变量和泛型)
- 程序如何知道静态变量是否需要初始化?
- 类外的静态变量实例化
- 无法解析静态变量
- 函数局部静态变量:从性能角度来看的优点/缺点
- 访问从 CPP 文件到其他头文件的静态变量