我正在尝试检索此数据时将记录更新到vecData
内的向量counter.cpp
后创建实例我得到的值为0
的main.cpp
#include <iostream>
#include "counter.hpp"
#define MAX_PORTS 3
int main()
{
Counter *counter = new Counter(3);
counter->age = 1;
counter->id = 12;
counter->pincode = 123;
std::vector<Counter> counterData(MAX_PORTS);
counter->storeCounterData(counter,1);
counter->storeCounterData(counter,2);
counter->storeCounterData(counter,3);
counter->getCounterData(counterData);
// getCounterData here is not able to retrieve value updated in above vector `vecData`
Counter *count = new Counter(3);
count->getCounterData(counterData);
return 0;
}
counter.cpp
#include "counter.hpp"
#include <iostream>
#include <vector>
Counter::Counter()
{
}
Counter::Counter(int vecSize):vecData(vecSize)
{
}
void Counter::storeCounterData(Counter *counter,int user)
{
vecData[user].age = counter->age;
vecData[user].id = counter->id;
vecData[user].pincode = counter->pincode;
std::cout<<"vector size inside storeCounterData = "<<vecData.size()<<std::endl;
}
void Counter::getCounterData(std::vector<Counter> &counter)
{
counter = vecData;
std::cout<<"vector size inside getCounterData = "<<vecData.size()<<std::endl;
std::vector<Counter>::iterator it;
for( it = vecData.begin(); it != vecData.end(); ++it)
{
std::cout<<vecData[1].age<<std::endl;
std::cout<<vecData[1].id<<std::endl;
std::cout<<vecData[1].pincode<<std::endl;
}
}
counter.hpp
#include <vector>
class Counter
{
private:
std::vector<Counter> vecData; // vectors
public:
Counter();
Counter(int vecSize);
int age, id, pincode;
void storeCounterData(Counter *counter,int user);
void getCounterData(std::vector<Counter> &counter);
};
输出
storeCounterData = 3 中的./ a.out矢量大小
storeCounterData = 3中的矢量大小
storeCounterData = 3中的矢量大小
getCounterData = 3内的矢量大小
1 12 123
1 12 123
1 12 123
getCounterData = 3内的矢量大小
0 0 0
0 0 0
0 0 0
答案 0 :(得分:4)
这是C ++,不要将宏用于常量:
#define MAX_PORTS 3
改为使用const
变量:
const int max_ports = 3;
为什么要在堆上创建此变量?
Counter *counter = new Counter(3);
counter->age = 1;
counter->id = 12;
counter->pincode = 123;
C ++不是Java或C#,不要这样做。你没有delete
这些变量,所以你泄漏了内存。不要这样做。这是正确的方法:
Counter counter(3);
counter.age = 1;
counter.id = 12;
counter.pincode = 123;
然后更改setCounterData
以获取引用,或者传入对象的地址:
counter->storeCounterData(&counter,1);
您正在使用值1
,2
和3
索引到向量,但向量只有三个元素,因此正确的索引为0
,{ {1}}和1
。当2
和vecData[user]
是一个严重错误时执行user==3
。别这么做。
为什么总是访问vecData.size()==3
而不是使用迭代器?
vecData[1]
你可能意味着这个:
std::vector<Counter>::iterator it;
for( it = vecData.begin(); it != vecData.end(); ++it)
{
std::cout<<vecData[1].age<<std::endl;
std::cout<<vecData[1].id<<std::endl;
std::cout<<vecData[1].pincode<<std::endl;
}
或者更简单:
std::vector<Counter>::iterator it;
for( it = vecData.begin(); it != vecData.end(); ++it)
{
std::cout<<it->age<<std::endl;
std::cout<<it->id<<std::endl;
std::cout<<it->pincode<<std::endl;
}
(注意用 for (auto& c : vecData)
{
std::cout << c.age << std::endl;
std::cout << c.id << std::endl;
std::cout << c.pincode << std::endl;
}
运算符周围的空格来读取它是多么容易。)
当我尝试检索此数据时,我得到的值为0
您总是打印0的原因是因为您从未填充的向量中获取数据,该向量仅包含三个新的<<
对象:
Counter
您尚未在 Counter *count = new Counter(3);
count->getCounterData(counterData);
中存储任何值,因此您打印的值只是count->vecData[1]
构造函数创建的默认值,无法初始化其成员:
Counter
这意味着成员变量只是垃圾。你碰巧得到了零,因为你的程序刚刚启动所以堆恰好充满了零,但实际上它是未定义的行为来读取这些变量。
您的Counter::Counter()
{
}
课程需要正确初始化这些成员:
Counter