[英]C++ accessing element in vector of class objects
I'm trying to access an element within a vector of class objects but I don't get it right. 我正在尝试访问类对象向量中的元素,但我没有把它弄好。 I guess it's something wrong with the constructor/deconstructor and reference but even the other questions like C++ destructor issue with std::vector of class objects , c++ vector of class object pointers or C++ vector of objects vs. vector of pointers to objects .
我想构造函数/解构函数和引用有问题,但即使是其他问题,如C ++析构函数问题,类对象的std :: vector,类对象指针的c ++向量或对象的 C ++向量与指向对象的指针向量 。 Hopefully someone can help me to fix my code snippet.
希望有人可以帮我修改我的代码片段。
class Node {
public:
Node();
Node(int id, const std::string& name);
Node(const Node& orig);
virtual ~Node();
void cout(void);
int get_id(void);
private:
int _id;
std::string _name;
};
#include "node.h"
Node::Node() {
}
Node::Node(int id, const std::string& name) : _id(id), _name(name) {
this->cout();
}
Node::Node(const Node& orig) {
}
Node::~Node() {
}
void Node::cout(void) {
std::cout << "NODE " << _id << " \"" << _name << "\"" std::endl;
}
int Node::get_id(void) {
return _id;
}
#include "node.h"
class Com {
public:
std::vector<Node> nodes;
Com();
com(const Com& orig);
virtual ~Com();
void cout_nodes(void);
private:
};
#include "communication.h"
Com::Com() {
nodes.push_back(Node(169, "Node #1"));
nodes.push_back(Node(170, "Node #2"));
}
Com::Com(const Com& orig) {
}
Com::~Com() {
}
void Com::cout_nodes(void) {
for (uint8_t i = 0; i < nodes.size(); i++) {
nodes[i].cout();
}
}
If I run Com com;
如果我运行
Com com;
I get the expected output of: 我得到了预期的输出:
[I 171218 13:10:10 Cpp:22] < NODE 169 "Node #1"
[I 171218 13:10:10 Cpp:22] < NODE 170 "Node #2"
but running com.cout_nodes();
但是运行
com.cout_nodes();
results in: 结果是:
[I 171218 13:10:14 Cpp:22] < NODE 0 ""
[I 171218 13:10:14 Cpp:22] < NODE 0 ""
As in C++ vector of objects vs. vector of pointers to objects everything works fine when I use references but I couldn't get std::iterator
and find_if
working than. 就像C ++向量对象与指向对象指针的向量一样,当我使用引用时,一切正常,但是我无法使
std::iterator
和find_if
工作。
find_if
statement and index calculation find_if
语句和索引计算 auto iterator = std::find_if(nodes.begin(), nodes.end(), [&](Node node) {
return node.get_id() == 169;
});
if (iterator != nodes.end()) {
size_t index = std::distance(nodes.begin(), iterator );
std::cout << "Index of ID #169: " << index << std::endl;
}
You defined this copy constructor: 您定义了此复制构造函数:
Node::Node(const Node& orig) {
}
And it doesn't do any copying. 它没有做任何复制。 It default initializes all the members of the
Node
being constructed. 它默认初始化正在构造的
Node
所有成员。 Since std::vector::push_back
does a copy of its argument, your get bogus copies. 由于
std::vector::push_back
会复制其参数,因此您的get伪造副本。
Rather than forcibly define an operation which the compiler can just as easily synthesize correctly by itself (you only have an int
and a std::string
as members), just don't declare it. 而不是强制定义一个编译器可以很容易地自己合成的操作(你只有一个
int
和一个std::string
作为成员),而不是声明它。
Or, if you do want to be explicit (or need to be, such as with the default c'tor), just explicitly default it: 或者,如果你想要显式(或者需要,例如使用默认的c'tor),只需显式默认它:
class Node {
Node() = default;
Node(const Node& orig) = default;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.