I am attempting to hide the fact that a class is using a vector to hold the data. I need to do so without creating a proxy class, which I have seen is straightforward.
I get the following compile error for the 2nd listing:
1 2 3 4 5 6 7 8 9
c++ -g -std=c++14 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char"main.cpp" (in directory: /c++/tester)
In file included from main.cpp:3:0:
tester1.h:13:30: error: ‘tester<T>::sample_container_type’ is not a templateusing sample_container = sample_container_type<T>;
^
tester1.h:24:22: error: ‘tester<T>::sample_container’ is not a templateusing iterator = sample_container<T>::iterator;
^
Compilation failed.
Any ideas on how I can hide the vector without getting the above errors?
> ‘tester<T>::sample_container_type’ is not a template
note where is T.
If you do tester<int>::sample_container_type foo then `foo' would be of type std::vector<int>
same for `sample_container', they are not templates.
So you may do using iterator = typename sample_container::iterator;
¿why do you need `sample_container' and `sample_container_type'?
thanks for replying. I've modified the code to take your suggestion into account. The question remains though on how I can make sample_container known publicly without showing that it is of type vector.
#ifndef _TESTER1_H_
#define _TESTER1_H_
#include <memory>
#include <iostream>
#include <vector>
#include <initializer_list>
template <typename T>
class tester {
private:
using sample_container = std::vector<T>;
sample_container sample_items;
mutable std::unique_ptr<T> balance;
T calc_balance() const;
void print(std::ostream & os) const;
public:
using sample_container; // public interface. LINE GENERATES ERROR!!
// users should be able to pass data in in 1 of 3 ways
// but the fact that vector appears as 1 of the ways does
// not expose the implementation details of the private vector
explicit tester<T>(const std::initializer_list<T> & test_balance ) :
sample_items{ test_balance }, balance(nullptr) { }
explicit tester<T>(const sample_container & test_balance ) :
sample_items{ test_balance }, balance(nullptr) { }
tester<T>() = default;
~tester<T>() = default;
tester<T>(const tester<T> & bal) { }; // copy constructor
typedeftypename sample_container::iterator iterator;
typedeftypename sample_container::const_iterator const_iterator;
iterator begin() {return sample_items.begin();}
const_iterator begin() const {return sample_items.begin();}
const_iterator cbegin() const {return sample_items.cbegin();}
iterator end() {return sample_items.end();}
const_iterator end() const {return sample_items.end();}
const_iterator cend() const {return sample_items.cend();}
T get_balance() const;
T get_avg() const;
const sample_container & get_data() const { return sample_items; }
template <typename Y>
friend std::ostream & operator<<(std::ostream & os, const tester<Y> & tst);
};
#include "tester1.cpp"
#endif
compile error generated is:
1 2 3 4 5 6
c++ -g -std=c++14 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char"main.cpp" (in directory: /c++/tester)
In file included from main.cpp:3:0:
tester1.h:20:11: error: expected nested-name-specifier before ‘sample_container’
using sample_container; // public interface. LINE GENERATES ERROR!!
^
Compilation failed.