Operator<< can't access member variable outside of .h header.

closed account (26q2b7Xj)
I am having trouble understanding why VS won't let me access the Vector's private member of m_gabes_vector in my .cpp file. In my Vector.h I have:
friend std::ostream& operator<<(std::ostream& out, const Vector &v);

EDIT: If there's any improvements I can make please feel free to let me know! I'll like to apply modern C++ to my Vector class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include "Vector.h"
#include <iostream>
#include <stdexcept>

Vector::Vector()
	: m_gabes_vector{ new double[8] },
	  m_capacity{ 8 },
	  m_size{ 0 }
{
	for (int index{}; index < 8; ++index)
		m_gabes_vector[index] = 0.0;
}

Vector::Vector(int capacity)
	: m_gabes_vector{ new double[capacity] {} },
	  m_capacity{ capacity },
	  m_size{ 0 }
{
	if (capacity <= 0)
		throw std::logic_error{ "capcaity must be at least 1." };

	for (int index{}; index < m_capacity; ++index)
		m_gabes_vector[index] = 0.0;
}

Vector::Vector(std::initializer_list<double> list)
	: m_gabes_vector{ new double[list.size()] },
	  m_capacity{ static_cast<int>(list.size()) },
	  m_size{ static_cast<int>(list.size()) }
{
	int index{};
	for (const auto &element : list)
	{
		m_gabes_vector[index] = element;
		++index;
	}
}

void Vector::push_back(const double &element)
{
	if (m_size == m_capacity)
		reserve(2 * m_capacity);

	m_gabes_vector[m_size++] = element;
}

// Allocate new storage space
void Vector::reserve(int new_alloc)
{
	if (new_alloc <= m_capacity)
		return;

	double* temp{ new double[new_alloc] };

	for (int index{}; index < m_size; ++index)
		temp[index] = m_gabes_vector[index];

	delete[] m_gabes_vector;

	m_gabes_vector = temp;

	m_capacity = new_alloc;
}

void Vector::resize(int new_size)
{
	reserve(new_size);

	for (int index{ m_size }; index < new_size; ++index)
		m_gabes_vector[index] = 0.0;

	m_capacity = new_size;
}



std::ostream& operator<<(std::ostream& out, const Vector& v)
{
	for (int index{}; index < v.get_size(); ++index)
		out << v.m_gabes_vector[index] << ' ';

	return out;
}

Vector::~Vector()
{
	if (m_gabes_vector)
	{
		delete[] m_gabes_vector;
		m_gabes_vector = nullptr;
}
Last edited on
paste the error message verbatim and the content of Vector.h
closed account (26q2b7Xj)
Error: member is inaccessible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef VECTOR_H_
#define VECTOR_H_

#include <initializer_list>


class Vector
{
public:
	Vector();
	Vector(int capacity);
	Vector(std::initializer_list<double> lst);
	~Vector();

	// Extension of Vector
	void push_back(const double &element);
	void reserve(int new_alloc);
	void resize(int new_size);

	
	friend std::ostream& operator<<(std::ostream &out, const Vector &v);

	// getters
	constexpr int get_size() const
	{ return m_size; }

	constexpr int get_capacity() const
	{ return m_capacity; }


	
		  

private:
	double* m_gabes_vector{};
	int m_capacity;
	int m_size;

};

std::ostream& operator<<(std::ostream& out, const Vector& v);
#endif 

Do you realize that your function implementation in the source file is implementing the non-friend function you declared on line 41, not the friend function you declared on line 21?

closed account (26q2b7Xj)
That was an attempt to see if having the declaration outside of operator<< would cause it to work. I am honestly stuck.

Edit: It seems all I needed to do was #include <ostream> to get it to work.
Last edited on
Well if you would have posted the complete error message (all of them), as requested, someone may have spotted that problem sooner.

By the way you probably should be #including <fstream> instead of <ostream>.

closed account (26q2b7Xj)
Yeah thanks!
Topic archived. No new replies allowed.