如何实例化对象的静态向量?

发布于 2024-12-06 14:06:00 字数 513 浏览 0 评论 0原文

我有一个 A 类,它有一个静态对象向量。这些对象属于 B 类,

class A {
  public:
    static void InstantiateVector();
  private:
    static vector<B> vector_of_B;
}

在函数 InstantiateVector() 中,

for (i=0; i < 5; i++) {
  B b = B();
  vector<B>.push_back(b);
}

但我使用 Visual Studio 2008 时遇到编译错误:无法解析的外部符号... 是否可以使用上述方法实例化静态向量?对于要创建的对象 b,必须从输入文件读取一些数据,并将其存储为 b 的成员变量

或者这是不可能的,并且只能简单的静态向量?我在某处读到,要实例化静态向量,必须首先定义一个 const int a[] = {1,2,3},然后将 a[] 复制到向量中

I have a class A, which has a static vector of objects. The objects are of class B

class A {
  public:
    static void InstantiateVector();
  private:
    static vector<B> vector_of_B;
}

In function InstantiateVector()

for (i=0; i < 5; i++) {
  B b = B();
  vector<B>.push_back(b);
}

But I have compilation error using visual studio 2008: unresolved external symbol...
Is it possible to instantiate static vector using above method? For object b to be created, some data has to be read from input file, and stored as member variables of b

Or it is not possible, and only simple static vector is possible? I read somewhere that to instantiate static vector, you must first define a const int a[] = {1,2,3}, and then copy a[] into vector

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-12-13 14:06:00

您必须提供 vector_of_b 的定义,如下所示:

// A.h
class A {
  public:
    static void InstantiateVector();
  private:
    static vector<B> vector_of_B;
};

// A.cpp
// defining it fixes the unresolved external:
vector<B> A::vector_of_B;

作为旁注,您的 InstantiateVector() 会生成大量不必要的副本,这些副本可能(也可能不会)被优化掉。

vector_of_B.reserve(5);  // will prevent the need to reallocate the underlying
                         // buffer if you plan on storing 5 (and only 5) objects
for (i=0; i < 5; i++) {
  vector_of_B.push_back(B());
}

事实上,对于这个简单的示例,您只是默认构造 B 对象,最简洁的方法是将循环全部替换为:

// default constructs 5 B objects into the vector
vector_of_B.resize(5);

You have to provide the definition of vector_of_b as follows:

// A.h
class A {
  public:
    static void InstantiateVector();
  private:
    static vector<B> vector_of_B;
};

// A.cpp
// defining it fixes the unresolved external:
vector<B> A::vector_of_B;

As a side note, your InstantiateVector() makes a lot of unnecessary copies that may (or may not) be optimized away.

vector_of_B.reserve(5);  // will prevent the need to reallocate the underlying
                         // buffer if you plan on storing 5 (and only 5) objects
for (i=0; i < 5; i++) {
  vector_of_B.push_back(B());
}

In fact, for this simple example where you are just default constructing B objects, the most concise way of doing this is simply to replace the loop all together with:

// default constructs 5 B objects into the vector
vector_of_B.resize(5);
画▽骨i 2024-12-13 14:06:00

将静态成员对象的定义添加到类实现文件中:

#include "A.h"

std::vector<B> A::vector_of_B;

或者更确切地说,吸收并消除初始化例程:

std::vector<B> A::vector_of_B(5, B()); // same effect as `A::InstantiateVector()`

注意:在 C++98/03 中,vector_of_B(5 )vector_of_B(5, B()) 相同。在 C++11 中则不然。

Add the definition of the static member object to your class implementation file:

#include "A.h"

std::vector<B> A::vector_of_B;

Or rather, absorbing and obviating the initialization routine:

std::vector<B> A::vector_of_B(5, B()); // same effect as `A::InstantiateVector()`

Note: In C++98/03, vector_of_B(5) and vector_of_B(5, B()) are identical. In C++11 they're not.

无远思近则忧 2024-12-13 14:06:00

您可以使用静态助手或使用 boost::assign

1>使用小助手:

#include <boost\assign.hpp>
class B{};

class A {
 public:
    static bool m_helper;
    static bool InstantiateVector()
    {
        for (int i=0; i < 5; ++i) 
        {
            B b;
            vector_of_B.push_back(b);
        }
        return true;
    }
private:
    static vector<B> vector_of_B;
};

bool A::m_helper = InstantiateVector();//helper help initialize static vector here

2>使用 boost::assign 更容易,一行就足够了:

vector<B> A::vector_of_B = boost::assign::list_of(B())(B())(B())(B())(B());

You can either use a static helper or use boost::assign

1>using a small helper:

#include <boost\assign.hpp>
class B{};

class A {
 public:
    static bool m_helper;
    static bool InstantiateVector()
    {
        for (int i=0; i < 5; ++i) 
        {
            B b;
            vector_of_B.push_back(b);
        }
        return true;
    }
private:
    static vector<B> vector_of_B;
};

bool A::m_helper = InstantiateVector();//helper help initialize static vector here

2> Use boost::assign which is eaiser, one line is enough:

vector<B> A::vector_of_B = boost::assign::list_of(B())(B())(B())(B())(B());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文