0

I want to directly access the vector BigArray::v which is a class member, and print it out. But the compiler won't build my code:

#include <iostream>
#include <vector>
using namespace std;

class BigArray
{
private:

    vector<int> v={1,2,3,4,5,6,7,8,9,10};
    int accessCounter;
public:
    const vector<int> & getVector() const {return v;}
};


int main(int argc, const char * argv[]) {
    // insert code here...
    BigArray b;
    cout<< *b.getVector()<< endl;
    return 0;
}
jotik
  • 17,044
  • 13
  • 58
  • 123
dice
  • 7
  • 3
  • 3
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Feb 14 '17 at 21:08
  • 1
    You are trying to dereference a `vector` and then print it – LWimsey Feb 14 '17 at 21:08
  • 1
    You should have seen from [much simpler code like this](http://ideone.com/n5UO7k) that you can't "print a vector" using simply `cout` and the vector. So solve that simple problem first before introducing classes into the mix. – PaulMcKenzie Feb 14 '17 at 21:11
  • 1
    If the compiler *"won't build the code"* it tells you why - you should read that carefully as it helps finding the solution – UnholySheep Feb 14 '17 at 21:13

1 Answers1

1

There are two issues:

  1. b.getVector() returns a reference to the vector, so by using *b.getVector() you attempt to dereference a reference, which is invalid. You probably meant to use just b.getVector() instead of *b.getVector().

  2. There is no overload for streaming a vector into a std::ostream like std::cout. You'll have to write your own, e.g.:

#include <iostream>
#include <vector>
using namespace std;

class BigArray {
private: /* Fields: */
    vector<int> v={1,2,3,4,5,6,7,8,9,10};
    int accessCounter;
public: /* Methods: */
    const vector<int> & getVector() const {return v;}
};

template <typename T>
std::ostream & operator<<(std::ostream & os, std::vector<T> const & v) {
     bool first = true;
     os << '{';
     for (auto const & elem : v) {
         if (!first) {
             os << ", ";
         } else {
             first = false;
         }
         os << elem;
     }
     return os << '}';
}

int main(int argc, const char * argv[]) {
    // insert code here...
    BigArray b;
    cout<< b.getVector() << endl;
    return 0;
}
jotik
  • 17,044
  • 13
  • 58
  • 123
  • Your loop never sets `first` to false, so `", "` is not outputted between the items. You need to move the `first` assignment out of the `if` block, or at least into an `else`. Alternatively, you can use `std::copy()` and `std::ostream_iterator` instead of using a manual loop. Let `std::ostream_iterator` handle delimiting the items for you, eg: `template std::ostream& operator<<(std::ostream &os, std::vector const & v) { os << '{'; std::copy(v.begin(), v.end(), std::ostream_iterator(os, ", ")); return os << '}'; }` – Remy Lebeau Feb 14 '17 at 21:35
  • @RemyLebeau Corrected handling of `first`. You are correct that the `std::copy`/`std::ostream_iterator` thingie is an alternative (and its even a better alternative). However, which of these do you think the OP will best understand at this point? – jotik Feb 14 '17 at 21:45