Hit a snag whilst adding a subclass to std::vector

  Kiến thức lập trình

My idea was initially to create my own custom vector with methods built in for various mathematical calculations, as well as the usual std::vector features, for the sake of regression analysis. I decided it made more sense to expand the std::vector library with a subclass, which will inherit the std::vector features whilst having those mathematical methods as well.
I decided to create two new vectors, one for each data set used with methods for averaging, summing and so on, and these vectors are to be inserted into the second vector which calculates the correlation coefficient, p-value and other things which require two or more data sets.

In the process I got the error message: “‘child_vector’ in ‘class std::vector’ does not name a template type” for the declaration of the vector in the global scope, and “‘thing’ was not declared in this scope” in int main().
Placing the declaration into int main() instead brings the error message “‘child_vector’ is not a member of ‘std::vector'”. Adding to the end of “class child_vector” yields the error message “child_vector is not a template type.” Noting out the declaration and the std::cout lines causes the error messages to disappear.

The code is:

#include <iostream>
#include <vector>

namespace std
{
    template <typename T>
    class child_vector : public vector <T>
    {
        public:
        // things go here   
    };
    
    template <typename T>   
    class parent_vector : public vector <T>
    {
        public:
        // things go here   
};  }       // namespace std

int main()
{
    std::vector <float>::child_vector <float> thing = {1, 2, 3, 4, 5};
    std::cout << thing[1] << "n";
    
    return 0;
}

This is the first time I have attempted something like this involving classes, and searching Google finds few results which address this error message in this situation. I am using g++23 on a Windows system.
As I also see this effort as a learning experience, any kind of input will be appreciated.

LEAVE A COMMENT