访问静态constexpr std ::数组没有类的定义 [英] Access static constexpr std::array without out-of-class definition
问题描述
我有一个定义一些数组的类。
Points.hpp
class Points {
public:
static constexpr std :: array<双,1> a1 = {{
+0.0}};
static constexpr std :: array<双,2> a2 = {{
-1.0 / std :: sqrt(3.0),
+1.0 / std :: sqrt(3.0)}};
};
我的主文件然后使用这些数组。
< h2> main.cpp
#includePoints.hpp
int main()
{
//关于如何访问点的示例。
auto point = Points :: a2 [0];
//用点做某事。
}
当我编译代码时,使用C ++ 11和g ++ 4.8.2 ,我得到以下链接器错误:
未定义引用`Points :: a2'
我试图创建一个Points.cpp文件,以便编译器可以从中创建一个目标文件。
Points.cpp
#includePoints.hpp
但是没有修复链接器错误。
可以在类声明中将变量初始化为C ++ 11中的static constexpr,然后按照我的方式访问它们,如下面的问题所示:http://stackoverflow.com/a/24527701/1991500
我需要为Points制作一个构造函数,然后实例化班上?我做错了什么?
感谢任何反馈!感谢!
解决方案根据@dyp建议,我研究了静态数据成员的定义。
我的问题需要我定义我的Points类的静态成员变量。
按照这些问题的例子:
和
我需要添加:
// in some .cpp
constexpr std :: array<双,1>点数:a1;
I have a class that defines some arrays.
Points.hpp
class Points { public: static constexpr std::array< double, 1 > a1 = { { +0.0 } }; static constexpr std::array< double, 2 > a2 = { { -1.0 / std::sqrt( 3.0 ), +1.0 / std::sqrt( 3.0 ) } }; };
My main file then uses these arrays.
main.cpp
#include "Points.hpp" int main() { // Example on how to access a point. auto point = Points::a2[0]; // Do something with point. }
When I compile my code, using C++11 and g++ 4.8.2, I get the following linker error:
undefined reference to `Points::a2'
I attempted to create a Points.cpp file so that the compiler can create an object file from it.
Points.cpp
#include "Points.hpp"
But that did not fix the linker error.
I was under the impression that it was possible to initialize variables as static constexpr in C++11 in the class declaration, and then access them the way I'm doing it, as shown in this question: http://stackoverflow.com/a/24527701/1991500
Do I need to make a constructor for Points and then instantiate the class? What am I doing wrong?
Any feedback is appreciated! Thanks!
解决方案As per @dyp suggestion, I looked into the definition of static data members.
My problem requires me to define the static member variables of my Points class.
Following the examples in these questions:
Is a constexpr array necessarily odr-used when subscripted?
and
Defining static members in C++
I need to add:
// in some .cpp constexpr std::array< double, 1 > Points::a1;
这篇关于访问静态constexpr std ::数组没有类的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!