Home > front end >  How to modify this function for argument usage
How to modify this function for argument usage

Time:01-25

i have this function with constructor:

std::vector<std::vector<double>>& getTrainSet()  { return trainSet; }
void setTrainSet(const std::vector<std::vector<double>>& trainSet) { this->trainSet = trainSet; }

Which is called like this:

testNet.setTrainSet(std::vector<std::vector<double>>() = {
        { 1.0, 1.0, 0.73 },{ 1.0, 1.0, 0.81 },{ 1.0, 1.0, 0.86 },
        { 1.0, 1.0, 0.95 },{ 1.0, 0.0, 0.45 },{ 1.0, 1.0, 0.70 },
        { 1.0, 0.0, 0.51 },{ 1.0, 1.0, 0.89 },{ 1.0, 1.0, 0.79 },{ 1.0, 0.0, 0.54 } 
        });

How do I modify this function so it will let me pass my variables like this: testNet.setTrainSet(vector);

CodePudding user response:

You can also do it in two ways,

  1. Declaring it first and then passing,
std::vector<std::vector<double>> vec = {
    { 1.0, 1.0, 0.73 }, { 1.0, 1.0, 0.81 }, { 1.0, 1.0, 0.86 },
    { 1.0, 1.0, 0.95 }, { 1.0, 0.0, 0.45 }, { 1.0, 1.0, 0.70 },
    { 1.0, 0.0, 0.51 }, { 1.0, 1.0, 0.89 }, { 1.0, 1.0, 0.79 },
    { 1.0, 0.0, 0.54 }
};

testNet.setTrainSet(vec);
  1. or by passing it directly into the setter.
testNet.setTrainSet({
    { 1.0, 1.0, 0.73 }, { 1.0, 1.0, 0.81 }, { 1.0, 1.0, 0.86 },
    { 1.0, 1.0, 0.95 }, { 1.0, 0.0, 0.45 }, { 1.0, 1.0, 0.70 },
    { 1.0, 0.0, 0.51 }, { 1.0, 1.0, 0.89 }, { 1.0, 1.0, 0.79 },
    { 1.0, 0.0, 0.54 }
});

CodePudding user response:

Maybe you are trying to declare the vector before calling the function?

std::vector<std::vector<double>> vec = {
    { 1.0, 1.0, 0.73 },{ 1.0, 1.0, 0.81 },{ 1.0, 1.0, 0.86 },
    { 1.0, 1.0, 0.95 },{ 1.0, 0.0, 0.45 },{ 1.0, 1.0, 0.70 },
    { 1.0, 0.0, 0.51 },{ 1.0, 1.0, 0.89 },{ 1.0, 1.0, 0.79 },{ 1.0, 0.0, 0.54 } 
    });
testNet.setTrainSet(vec);
  • Related