我是一名自学成才的C++程序员(仍处于新手级别)。C++ Vectors插入新对象
我想我已经了解了C++的工作原理,但是我不能用这种方式来包装我的头: 我想创建一个std :: vector并使用定义好的类的不同元素来填充std :: vector:
// other code
while (getline(cfgDataStream, cfgData)) //parsing cycle of the config file
{
std::stringstream ss(cfgData); //creating a stream in order to fill fields
ss >> string1 >> IP1 >> IP2 >> PORT2 >> INDEX;
//they are all strings save the last one, which is a int
if (ss.fail())
{
//bad things happen
}
//FIRST IDEA: Using insert()
CModbusServer MBtemp* = new CModbusServer(this, IP2.c_str(), PORT2, INDEX)
std::vector<CModbusServer*>::iterator iterator = this->m_pServerCollection.begin(); //I get the vector initial position
m_pServerCollection.insert(iterator + (INDEX), MBTemp); // I put the new object in the right index (I don't trust the order in the config file)
//SECOND IDEA: Using push_back()
m_pServerCollection.push_back(new CModbusServer(this, IP2.c_str(), PORT2, INDEX)); //I attach each new object to the end of vector (i trust the order in the config file)
}
基本上我想创建CModbusServer的一个对象,并插入其指针在载体中,从而使我在每个矢量位置n不同CModbusServer对象。 这是我迷路的地方,我尝试了两种插入方式(如代码所示),但没有成功。
CModbusServer除其他外还有一个const char * ipAddress字段。如果我尝试访问该字段(即在.Format(_T("%S))
函数中使用它),我会得到随机数据。试图明白为什么我注意到,在向量中,我没有n个不同的对象,而是使用new CModbusServer(this, IP2.c_str(), PORT2, INDEX)
创建的最后一个对象的n个副本。可能发生这种情况是因为我有一个指针向量,但这些应该是指向不同对象的指针......
我正在使用Visual Studio 2015与MFC为了实现基于对话框的应用程序。我有一个AppEngine类,它从其他类中调用方法,并具有CModbusServer元素的向量。 CModbusServer.h如下:
class CModbusServer
{
public:
CModbusServer(void *parentEngine, const char* , unsigned short , int);
~CModbusServer();
const char* ipAddress;
unsigned short port;
int indNode;
modbus_t *MBserver;
bool isConnected;
}
所以,我的问题是:
1)为什么我不能访问的ip地址字段(而不是读“192.0.2.1”我读随机字符),而我理论上应该能够使用theApp.CModbusServerVector[properIndex]->ipAddress
来读取它?
2)我在填充矢量时犯了一个错误,但我看不到它在哪里,最重要的是,它为什么错了。
感谢您的帮助,请原谅我的英文和任何遗漏。
编辑:
CModbusServer的构造函数的代码是这样的:
CModbusServer::CModbusServer(void *pE, const char* ip, unsigned short nport, int ind)
: parentEngine(pE), //used in order to keep track of the parent dialog
ipAddress(ip),
port(nport),
indNode(ind)
{
this->isConnected = false;
this->m_socket = INVALID_SOCKET;
memset(&m_socketstructhint, 0, sizeof m_socketstructhint);
m_socketstructhint.ai_family = AF_UNSPEC;
m_socketstructhint.ai_socktype = SOCK_STREAM;
m_socketstructhint.ai_protocol = IPPROTO_TCP;
MBserver = modbus_new_tcp(ipAddress, (int)nport);
}
请告诉我,如果我遗漏任何其他有用的信息。
最初我用CString
来管理字符串,但后来我遇到了越来越多的问题,最后得到了一个编译和const char*
工作代码。我设法建立了一个连接并读取所需的modbus寄存器,但之后我陷入了isAddress打印问题。
modbus_new_tc(ip,port)
是在libmodbus库中找到的一种方法,libmodbus库是为我必须使用的C编写的免费软件库。
编辑2:有关angew答案:
所以,如果我是正确的,发生的事情是,我创建一个临时组指针,由构造函数中使用的(我现在已经增加了相关码)。但是,不应该是构建的对象与我通过的参数无关吗?这些参数不是被复制的吗?对不起,如果问题很愚蠢,但我还在学习。
索引是连续的,尽管在配置文件中也可能是0-1-2-3(每行1个)或0-3-1-2,这就是我所说的“不信任他们”。
由于push_back
方法有相同的问题,可能问题是在构造函数中。令我感到困惑的是,通过逐步执行,我可以看到,while循环的每次迭代都会得到新的正确数据,但是放入第i个位置的时候,会放在第一个位置(即:原始数据:abc,1st run vector = a; 2nd run vector = bb,3rd run vector = ccc)
我不知道std::unique_ptr<>
,我会查找它。
我试过使用std:string甚至CString,但问题在于libmodbus库。
没有拷贝构造函数/析构函数/赋值/移动操作符的类中的原始指针可能是危险的。 – doctorlove
如果您正在学习C++,您可能会发现我们的[良好C++书籍列表](http://stackoverflow.com/q/388242/1782465)有用。 – Angew