37

我正在设计一个具有std::vector<int>作为实例变量的类。我正在使用 astd::vector因为我需要在运行时设置它的大小。以下是我的代码的相关部分:

my_class.h:

#include <vector>
using std::vector;
class MyClass {
    int size;
    vector<int> vec;
}

my_class.cc:

#include "my_class.h"
using std::vector
MyClass::MyClass(int m_size) : size(m_size) {
     vec = new vector<int>(size,0);
}

当我尝试编译时,我收到以下错误消息:

g++ -c -Wall my_class.cc -o my_class.o

my_class.cc: In constructor ‘MyClass::MyClass(int):

  my_class.cc:4 error: no match for ‘operator=’ in ‘((MyClass*)this)->My_Class::vec = ((*(const allocator_type*)(& std::allocator<int>())), (operator new(24u), (<statement>, ((std::vector<int>*)<anonymous>))))’

make: *** [my_class.o] Error 1

但是,当我将违规行更改为:

vector<int> temp(size,0);
vec = temp;

它现在可以顺利编译,我得到了所需的行为,并且可以访问我的向量

vec[i]  // i having been defined as an int yada yada yada

这种解决方法没问题,但我想了解它为什么有效并且第一种方法失败。提前致谢。

4

3 回答 3

48

Just do:

MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0)

You already seem to know about initializer lists, why not initialize vector there directly?

vec = new vector<int>(size,0);

is illegal because new returns a pointer and in your case vec is an object.

Your second option:

vector<int> temp(size,0);
vec = temp;

although it compiles, does extra work for no gain. By the time you reach the assignment, two vectors would already have been constructed and discarded afterwards.

于 2012-07-10T14:26:14.377 回答
15

在你的类中使用向量是合法的,问题是你如何初始化它:

#include <vector>

class MyClass {
public:
    MyClass(int m_size);

    // ... more things...
private:
    int size;
    vector<int> vec;
}

您正在分配一个指向新向量对象的指针,就好像该向量对象未初始化一样。

vec = new vector<int>(size,0);

如果你真的想让它工作,那么你应该将你的vec对象声明为:

vector<int> * vec;

并且不要忘记添加一个析构函数:

MyClass::~MyClass {
    delete vec;
}

为什么当你丢弃new粒子时它会起作用?因为您正在创建一个新对象vector,并覆盖类中的那个(但这并不能保证正确消除原始对象)。

你实际上不需要这样做。vector当您到达 MyClass 的构造函数时,您的对象已经被初始化(调用它的默认构造函数)。如果您只想确保为size项目保留内存:

MyClass::MyClass(int m_size): size(m_size) {
    vec.reserve( size );
}

如果你希望你的向量有size元素,那么:

MyClass::MyClass(int m_size): size(m_size), vec(m_size, 0)
    {}

最后,正如其中一位评论者指出的那样,一旦构建了向量,实际上就不需要大小。所以你可以摆脱该size成员:

class MyClass {
public:
    MyClass(int m_size): vec(m_size, 0)
        {}

    unsigned int getSize() const
        { return vec.size(); }

    // ... more things...
private:
    vector<int> vec;
}

希望这可以帮助。

于 2012-07-10T14:36:39.433 回答
2
#include <vector>
#include <iostream>
#include <string>
#include <typeinfo>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::to_string;

class Parse
{
private:
    string         m_str;
    vector<string> m_vec;
public:
    // Constructor with all defaults (1 of 4 constructors)
    Parse(){ 
        cout << "\ncreating class with all default values\n";
        m_str = "";
        m_vec.push_back("");    
    }

    // Constructor with all cases used
    Parse  (string         &tmp_str,
            vector<string> tmp_vec):

            m_str          (tmp_str),
            m_vec          (tmp_vec)
    {
        cout << "Your vector contains " + to_string(m_str.size()) + " arguments\n";
    }

    // Constructor with other contents given but not vector
    Parse  (string         &tmp_str): 
            m_str          (tmp_str)
    {
        m_vec.push_back("");
    }
    // Constructor with only Vector given but not other contents
    Parse  (vector<string>   tmp_vec):
            m_vec           (tmp_vec)
    {
        m_str = "";
    }

    string get_str_var(){return m_str;}

    void classed_print_vector_strings()
    {
        for (string i : m_vec){ cout << i << " \n";}
    }

};



// rm ./class_vector; g++ class_vector.cpp -o class_vector -std=c++17; ./class_vector arg1 arg2 arg3

int main(int argc, char *argv[])
{
    // turn **argv to a vector
    vector<string> args(argv, argv + argc);
    // iterate from argv through argv+argc

    // initialize with default arguments.
    Parse tracker1;
    // initalize with all used arguments
    Parse tracker2(args[0], args);
    // initalize with only the vector
    Parse tracker3(args);
    // initalzie without the vector, but with another arg
    Parse tracker4(args[0]);

    cout << "\nTracker 1 ---------------------\n";
    tracker1.classed_print_vector_strings();
    cout << "\nTracker 2 ---------------------\n";
    tracker2.classed_print_vector_strings();
    cout << "\nTracker 3 ---------------------\n";
    tracker3.classed_print_vector_strings();
    cout << "\nTracker 4 ---------------------\n";
    tracker4.classed_print_vector_strings();


    return 0;
}
rm ./class_vector; g++ class_vector.cpp -o class_vector -std=c++17; ./class_vector arg1 arg2 arg3

这将向您展示如何创建一个类,该类将使您可以选择使用或不使用带有和/或不存在其他参数的向量来初始化该类。

于 2018-02-17T21:28:37.890 回答