0

I'm trying to work with a function outside the class vector inside the vector class.

inside the main when I'm calling the same func it worked fine but when I call it inside the vector class, I get this error

➜  ft_containers git:(main) ✗ clang++ main.cpp -g
In file included from main.cpp:17:
In file included from ./containers/../colors.hpp:24:
./containers/vector.hpp:139:5: error: use of undeclared identifier 'PrintVecData'
                                PrintVecData<value_type>(*this);
                                ^
./containers/vector.hpp:139:18: error: unexpected type name 'value_type': expected expression
                                PrintVecData<value_type>(*this);
                                             ^
2 errors generated.
➜  ft_containers git:(main) ✗ 

color.hpp

#ifndef COLORS_H
#define COLORS_H

# define GREEN "\e[1;32m"
# define RESET "\e[0m"
# define RED "\e[1;91m"
# define CYAN "\e[1;36m"
# define YELLOW "\e[1;33m"
# define PURPLE "\e[1;35m"
# define BLUE "\e[1;34m"

#include "containers/vector.hpp"

template<typename T>
void PrintVecInfo(ft::vector<T>& ft_vec, std::vector<T>& std_vec)
{
    std::cout << "ft::capacity : " << ft_vec.capacity() << std::endl;
    std::cout << "ft::size : " << ft_vec.size() << std::endl;
    std::cout << "ft::max_size : " << ft_vec.max_size() << std::endl;
    std::cout << "std::capacity : " << std_vec.capacity() << std::endl;
    std::cout << "std::size : " << std_vec.size() << std::endl;
    std::cout << "std::max_size : " << std_vec.max_size() << std::endl;
}


template<typename T>
void PrintVecData(ft::vector<T>& ft_vec, std::vector<T> std_vec = std::vector<T>())
{
    std::cout << "ft:  ";
    for (size_t i = 0; i < ft_vec.size(); i++)
        std::cout << "|" << *(ft_vec.begin() + i) << "|" ;
    std::cout << std::endl << "std: ";
    for (size_t i = 0; i < std_vec.size(); i++)
        std::cout << "|" << *(std_vec.begin() + i) << "|" ;
    std::cout << std::endl;
    PrintVecInfo<T>(ft_vec, std_vec);
}

#endif

the "containers/vector.hpp" file is :

#include "iterator.hpp"
#include "utils.hpp"

#include <string>
#include <iostream>
#include <algorithm>
namespace ft
{
    template < class T, class Allocator = std::allocator<T> >
    class vector
    {
        public: /*             Modifiers                         */
            void insert (iterator position, size_type n, const value_type& val)
            {
                difference_type diff = this->end() - position;
                difference_type posIndex = position - this->begin();
                if (this->size() + n > this->capacity())
                {
                    if (this->size() + n > this->capacity() * 2)
                        this->reserve(this->size() + n);
                    else
                        this->reserve(this->capacity() * 2);
                }
                iterator it = this->begin() + posIndex;
                for (size_t i = 0; i < diff; i++)
                {
                    *(it + n) = *(it);
                    it++;
                }
                it = this->begin() + posIndex;
                for (size_t i = 0; i < n; i++)
                    *(it + i) = val;
                PrintVecData<value_type>(*this) <==== here
                this->_size += n;
            };

}

when I call PrintVecData<value_type>(vec) in main it work fine with no problem but when I call the same function inside the vector class I the error above;

DarkSide77
  • 719
  • 1
  • 4
  • 21
  • 2
    Please show the contents of `containers/vector.hpp`. – 0x5453 Jan 13 '22 at 16:39
  • The fine `containers/vector.hpp` is so long – DarkSide77 Jan 13 '22 at 16:44
  • 3
    _"I remove some content from the fine cuz it's too long"_ You need to create a [mcve]. – Ted Klein Bergman Jan 13 '22 at 16:44
  • 3
    It looks like you have a circular dependency between the headers. Why are the printing functions in "colors.hpp" and not in "vector.hpp" or a separate file? – molbdnilo Jan 13 '22 at 16:44
  • 1
    Learning to create a [mre] is a valuable skill. You'll find that often, the creation of a MRE will reveal to you the problem that you're experiencing. – Drew Dormann Jan 13 '22 at 16:46
  • @TedKleinBergman I live the part of the code you need the rest no need – DarkSide77 Jan 13 '22 at 16:49
  • Make sure that `vector.hpp` does not include `colors.hpp` – drescherjm Jan 13 '22 at 16:52
  • Recent versions of C++ should deduce that `` from the `*this` argument, so you wouldn't need to use it. Anyway, your first problem is that `vector.hpp` has to know about `color.hpp`'s `PrintVecData`. – rturrado Jan 13 '22 at 16:52
  • @drescherjm I didn't include `colors.hpp` inside ` `vector.hpp` – DarkSide77 Jan 13 '22 at 16:53
  • Can the implementation of `PrintVecData<>` and `PrintVecInfo<>` be moved to vector.hpp – drescherjm Jan 13 '22 at 16:55
  • yes actually that's what I did now but I want to know why it didn't work that way @drescherjm – DarkSide77 Jan 13 '22 at 16:56
  • 1
    @DarkSide77 No, you didn't since people are still asking questions about the code you left out. Creating a [mcve] is not only to help us, but to help future readers with the same problem as you to understand your question better. Also, 90% of the time you'll be able to find the problem yourself in the process of creating a [mcve]. – Ted Klein Bergman Jan 13 '22 at 16:58
  • In your original code If you include colors.hpp from vectors.hpp you create a circular chain of includes. That is described by this: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) but possibly more difficult because of template implementations need to be in the header. If you don't then the implementations of `PrintVecData<>` and `PrintVecInfo<>` are not available in vectors.hpp – drescherjm Jan 13 '22 at 16:58
  • @TedKleinBergman in the first I didn't show the content of `containers/vector.hpp` but now I did – DarkSide77 Jan 13 '22 at 17:02
  • @drescherjm so now I declare the function `PrintVecData<>` `PrintVecInfo<>` inside the `vector.hpp` and still the same problem – DarkSide77 Jan 13 '22 at 17:05

0 Answers0