I have the following class where I have a private member vectortest which I would like to use as a computation variable for operations within the class member functions. Then, I would copy the computed vector to another vector cvectorin, which is a public member but a constant so that the contents cannot be modified, yet is accessible. I tried the following (both assignment as well as std::copy):
class constVecTest
{
private:
vector<int> vectortest;
public:
constVecTest(vector<int> vin);
const vector<int> cvectorin;
vector<int> vectorin;
void vecOp1();
void vecOp2(vector<int> vin);
};
constVecTest::constVecTest(vector<int> vin)
{
vecOp1();
vecOp2(vin);
}
void constVecTest::vecOp1()
{
vectortest = {1, 0, 1, 0, 1};
// cvectorin = vectortest;
copy(vectortest.begin(), vectortest.end(), back_inserter(cvectorin));
}
void constVecTest::vecOp2(vector<int> vin)
{
vectortest = vin;
vectorin = vectortest;
}
int main()
{
vector<int> input {1, 2, 3, 4, 5}, cvo, cvc;
constVecTest cvt(input);
cvo = cvt.vectorin;
cvc = cvt.cvectorin;
return 0;
}
But I get the error:
error: passing ‘const std::vector<int>’ as ‘this’ argument discards qualifiers [-fpermissive]
I'm new to vectors and couldn't find any good example on such a scenario. How do I achieve what I intend to? If the above approach is not the right one, I would be glad to learn a new alternative.
Aucun commentaire:
Enregistrer un commentaire