在模板类中初始化静态指针
考虑这样一个类:
template < class T >
class MyClass
{
private:
static T staticObject;
static T * staticPointerObject;
};
...
template < class T >
T MyClass<T>::staticObject; // <-- works
...
template < class T >
T * MyClass<T>::staticPointerObject = NULL; // <-- cannot find symbol staticPointerObject.
我无法弄清楚为什么我无法成功创建该指针对象。
上面的代码都是在头文件中指定的,我提到的问题是链接步骤中的错误,所以它没有找到特定的符号。
Consider a class like so:
template < class T >
class MyClass
{
private:
static T staticObject;
static T * staticPointerObject;
};
...
template < class T >
T MyClass<T>::staticObject; // <-- works
...
template < class T >
T * MyClass<T>::staticPointerObject = NULL; // <-- cannot find symbol staticPointerObject.
I am having trouble figuring out why I cannot successfully create that pointer object.
The above code is all specified in the header, and the issue I mentioned is an error in the link step, so it is not finding the specific symbol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“无法找到符号 staticPointerObject” - 这看起来像链接器错误消息。是吗? (必须在您的问题中指定此类详细信息)。
如果是,则很可能会发生这种情况,因为您将静态成员的定义放入实现文件(.cpp 文件)中。为了使其正常工作,定义应放入头文件(.h 文件)中。
同样,必须在您的问题中指定此类详细信息。没有他们,它就会变成一场随机的猜测盛宴。
"Cannot find symbol staticPointerObject" - this looks like a linker error message. Is it? (Details like this have to be specified in your question).
If it is, them most likely it happens because you put the definition(s) of your static member(s) into an implementation file (a .cpp file). In order for this to work correctly, the definitions should be placed into the header file (.h file).
Again, details like this have to be specified in your question. Without them it turns into a random guess-fest.
我怀疑您的第一个示例的原因如下(来自 2003 C++ std 文档)。特别注意最后一句——从你的例子来看,似乎没有什么“需要成员定义存在”。
I suspect the reason your first example is the following (from the 2003 C++ std document). Note particularly the last sentence -- from your example, there seems to be nothing "that requires the member definition to exist".
您对静态成员的第一个“定义”只是一个声明 - 这是标准的引用。
第二个定义应该有效。您确定在一个编译单元中拥有所有可用的内容吗?错误消息的确切文本是什么?
以下内容使用 g++ 编译/运行 - 全部在一个文件中
Your first "definition" of static member is but a declaration - here is a quote from the standard.
The second definition should work. Are you sure you have everything available in one compilation unit? What is teh exact text of error message?
The following compiles/runs with g++ - all in one file
我找到了两种解决方案。他们都不是100%我所希望的。
显式初始化特定实例,例如
这并不方便,尤其是当我有很多不同的类型时。
这有点麻烦,但至少可用。为什么我可以实例化变量对象但不能实例化指向变量对象的指针?如果有的话,我认为反过来我会遇到更多问题(编译器提前知道指针是什么样子,但不知道我的对象是什么样子)。
如果有人有更好的答案,我很乐意看到!
I have found two solutions. Neither of them are 100% what I was hoping for.
Explicitely initialize the specific instance, e.g.
This is not convinient especially when I have a lot of different types.
This is a bit of a hassle, but at least usable. Why is it I can instansiate a variable object but not a pointer to a variable object? If anything I would think I'd have more problems the other way around (the compiler knows ahead of time what a pointer looks like, but not what my object looks like).
If someone has a better answer I'd love to see it!
我一直使用以下技巧。这个想法是将静态放在一个函数中,并且仅从该函数访问它。这种方法还允许您避免在
.cpp
文件中声明静态的需要——所有内容都可以存在于.h
文件中。按照您的示例代码:I use the following trick all the time. The idea is to put your static in a function, and access it only from that function. This approach also allows you to avoid the need to declare your static in a
.cpp
file -- everything can live in the.h
file. Following your example code: