0

I have a classe called Nuage with the implementation:

nuage.hpp

using namespace std;

template <typename T>

class Nuage {
    private:
        vector<T> v;
        static vector<T>::const_iterator it;

    
    public:
        Nuage();
        void ajouter(T);
        int size();
        T& begin();
        ~Nuage();
};

nuage.cpp

template <typename T>
Nuage<T>::Nuage(){}

template <typename T>
void Nuage<T>::ajouter(T p) {
    v.push_back(p);
}

template <typename T>
int Nuage<T>::size() {
    return v.size();
}

template <typename T>
T& Nuage<T>::begin(){
    return v.begin();
}

template <typename T>
Nuage<T>::~Nuage(){}

When i try to test the functioning of the class from a separate file, there are two errors with the following line of the code: The following line is from a unit test that I can't change, so my code should adapt to it. Nuage::const_iterator it = n.begin();

The unit test code is:

TEST_CASE ( "TP2_Nuage::Iterateurs" ) {
 Polaire p1(12,34);
 Polaire p2(56,78);
 Polaire p3(90,12);
 Polaire p4(34,56);

 Nuage<Polaire> n;

 n.ajouter(p1);
 n.ajouter(p2);
 n.ajouter(p3);
 n.ajouter(p4);

 Polaire t[4];
 unsigned i = 0;
 Nuage<Polaire>::const_iterator it = n.begin();

 while (it!=n.end()) t[i++]=*(it++);

 REQUIRE ( t[0].getAngle() == Approx(p1.getAngle()) );
 REQUIRE ( t[0].getDistance() == Approx(p1.getDistance()) );
 REQUIRE ( t[1].getAngle() == Approx(p2.getAngle()) );
 REQUIRE ( t[1].getDistance() == Approx(p2.getDistance()) );
 REQUIRE ( t[2].getAngle() == Approx(p3.getAngle()) );
 REQUIRE ( t[2].getDistance() == Approx(p3.getDistance()) );
 REQUIRE ( t[3].getAngle() == Approx(p4.getAngle()) );
 REQUIRE ( t[3].getDistance() == Approx(p4.getDistance()) );
}

The errors are:

error: need ‘typename’ before ‘std::vector<T>::const_iterator’ because ‘std::vector<T>’ is a dependent scope

error: ‘const_iterator’ is not a member of ‘Nuage<Polaire>’
  Nuage<Polaire>::const_iterator it = n.begin()
user123
  • 17
  • 3
  • Looks like you want `Nuage::it = n.begin();`. Please show the remaining code as well. – cigien Oct 13 '20 at 22:16
  • Could you describe the intended purposes and use case of the line `static vector::const_iterator it;`? It's not clear to me what use a static member variable of that type would be and suspect you may have intended to perform a typedef instead. – Nathan Pierson Oct 13 '20 at 22:23
  • @cigien Nuage::const_iterator it = n.begin(); this is a part of a unit test that I can't change, it means that my code should adapt to this – user123 Oct 13 '20 at 22:25
  • I'm not saying you should change it, but we do need to see that code. – cigien Oct 13 '20 at 22:31
  • @cigien okay , I will put it – user123 Oct 13 '20 at 22:31

1 Answers1

0

It looks like you're trying to expose the iterators of the underlying vector with things like Nuage<T>::begin(). If that's the case, I would suggest public using declarations, as well as making sure begin has the appropriate return type:

template <typename T>    
class Nuage {
    private:
        std::vector<T> v;
    
    public:
        using iterator = typename std::vector<T>::iterator;
        using const_iterator = typename std::vector<T>::const_iterator;

        Nuage();
        void ajouter(T);
        int size();

        iterator begin();
        const_iterator begin() const;

        iterator end();
        const_iterator end() const;

        ~Nuage();
};

Here, my using declarations mean that Nuage<T>::iterator is the same type as std::vector<T>::iterator, and the begin functions return one.

I've also made a couple other changes to your code. I dropped the using namespace std; from the header file, since that's pretty frowned-on behavior. I also added a const version of begin() to make it more closely match the std::vector interface. And finally I've added declarations of Nuage::end functions, which will surely be needed for that unit test.

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30