Overloading the >> operator

Stuck again =(

This is my implement file:
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
#include "cscd305.h"

CArray::CArray()
{
	this->m_array = new int[10];
	this->m_size = 10;
}

CArray::CArray(int size)
{
	this->m_array = new int[size];
	this->m_size = 7;
}

CArray::~CArray(void)
{delete[] this->m_array;}
/*
int CArray::getSize()
{
	return this->m_size;
}
*/
ostream& operator <<(ostream &out, const CArray &rhs) 
{
	out << "Size: " << rhs.m_size;
	return out;
}



hovering over: out << "Size: " << rhs.m_size; line gives this error:
Error: member CArray::m_size is inaccessible.

This is my .header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "cscd305.h";

class CArray
{
public:
	//Constructors
	CArray();
	CArray(int size);
	~CArray();

	//Output stream
	friend ostream & operator<<(ostream &out, const CArray &rhs);

	//Getters-Setters
	inline int getSize(){return this->m_size;}

private:
	int *m_array;
	int m_size;
};


The friend ostream & operator<<(ostream &out, CArray &rhs);
line gives this error:

//ERROR: Error 6 c:\carray.h 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int





These are the two other files (for main and other packages):

Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "cscd305.h"

int main()
{
	CArray integers1(7); // seven-element CArray
	CArray integers2; // 10-element CArray by default

	// print integers1 size and contents
   cout << "Size of CArray integers1 is " << integers1.getSize() << endl
      << "CArray after initialization:" << endl << integers1 << endl;

   // print integers2 size and contents
   cout << "Size of CArray integers2 is " << integers2.getSize() << endl
      << "CArray after initialization:" << endl << integers2 << endl;

}


cscd305.h header
1
2
3
4
5
6
7
8
#pragma once

#include "CArray.h"
#include <iostream>
#include <string>

using namespace std;
Last edited on
This is a namespace problem - ostream is in the standard namespace std

You either need to do:

A using directive like:

using namespace std;

or specify the standard namespace when using ostream like:

std::ostream

or a using declaration like:

using std::ostream


***********
I should also point out that this function prototype
friend ostream & operator<<(ostream &out, CArray &rhs); //const NOT present in prototype
will not match this function
1
2
3
4
5
ostream& operator <<(ostream &out, const CArray &rhs) //const has suddenly appeared here
{
	out << "Size: " << rhs.m_size;
	return out;
}


because of const keyword is not used in both.
Last edited on
I did include using namespace std in another .h file, but didnt mention it here... sorry
I edited the original post with ALL 4 files i am using

still get the same two errors even after fixing the "const"
Last edited on
Post your actual files then
Topic archived. No new replies allowed.