Hi everyone,
I am trying to Print (display) all the items of a vector (class TvectorStat)
which is into another class (class Tpile). Apparently I can't reach m_size
of Class TvectorStat because it is in the private part of the class but I
don't understand why because this class (TvectorStat) belongs to the class
Tpile...Can someone look at my files?
_______________ __________main. cpp_______
#include "vectorStat .h"
#include "pile.h"
int main()
{
Tpile p;
p.push(11);
p.push(22);
p.push(33);
p.push(44);
p.push(55);
p.push(66);
p.push(77);
p.push(88);
p.push(99);
p.push(111);
cout<<p.printLn (0)<<endl;
cout<<p.printLn (1)<<endl;
cout<<p.printLn (2)<<endl;
cout<<p.printLn (3)<<endl;
cout<<p.printLn (4)<<endl;
cout<<p.printLn (5)<<endl;
cout<<p.printLn (6)<<endl;
cout<<p.printLn (7)<<endl;
cout<<p.printLn (8)<<endl;
cout<<p.printLn (9)<<endl;
cout<<p.printLn (10)<<endl;
p.affiche();
return 0;
}
_______________ _______________ _______
_______________ _________pile.c pp___________
// pile.cpp
#include "vectorStat .h"
#include "pile.h"
void Tpile::push(int val)
{
m_data.push_bac k(val);
}
void Tpile::pop()
{
m_data.pop_back ();
}
int Tpile::top()
{
return m_data.back();// int& TvectorStat::ba ck() OU BIEN int
TvectorStat::ba ck()const ?
}
int Tpile::printLn( int pos)
{
return m_data.at(pos);
}
void Tpile::affiche( )
{
int i=0;
for (i=0;i<m_size;i ++)
cout<<m_data[i]<<endl;
}
_______________ _______________ _____
_______________ __pile.h_______ ___________
// pile.h
#ifndef PILE_H
#define PILE_H
#include <iostream>
using namespace std;
class Tpile{
public:
Tpile():m_data( 100){}
void push(int val);
void pop();
int top();
int printLn(int pos);
void affiche();
private:
TvectorStat m_data;
};
#endif
_______________ _______________ ________
_______________ ___________vect or.cpp_______
//---------------------------------------------------------
// Fichier : vectorStat.cpp
// esnig - Laboratoire de programmation
//---------------------------------------------------------
#include "vectorStat .h"
void fatalError( const char* str )
{
cout << "TvectorSta t Error : " << str << endl ;
exit(1) ;
}
TvectorStat::Tv ectorStat( int capacity )
:m_capacity(cap acity),m_size(0 )
{
m_values = new int[m_capacity] ;
if( m_values==NULL )
fatalError("All ocation à la construction") ;
}
TvectorStat::Tv ectorStat( const TvectorStat& vec )
:m_capacity(vec .m_capacity),m_ size(vec.m_size )
{
m_values = new int[vec.m_capacity] ;
if( m_values==NULL )
fatalError("All ocation à la copie") ;
for( int i=0; i<vec.m_size;i+ + )
m_values[i] = vec.m_values[i] ;
}
TvectorStat::~T vectorStat()
{
delete[] m_values ;
}
TvectorStat& TvectorStat::op erator=( const TvectorStat& opG )
{
if( this != &opG ){
delete[] m_values ;
m_capacity = opG.m_capacity ;
m_size = opG.m_size ;
m_values = new int[opG.m_capacity] ;
if( m_values==NULL )
fatalError("All ocation à l'affectation") ;
for( int i=0; i<opG.m_size;i+ + )
m_values[i] = opG.m_values[i] ;
}
return *this ;
}
void TvectorStat::pu sh_back( int value )
{
if( full() )
fatalError("pus h_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}
void TvectorStat::po p_back()
{
if( !empty() )
m_size-- ;
}
int& TvectorStat::ba ck()
{
if( empty() )
fatalError("bac k sur conteneur vide") ;
return m_values[m_size-1] ;
}
int TvectorStat::ba ck()const
{
if( empty() )
fatalError("bac k sur conteneur vide") ;
return m_values[m_size-1] ;
}
int& TvectorStat::fr ont()
{
if( empty() )
fatalError("fro nt sur conteneur vide") ;
return m_values[0] ;
}
int TvectorStat::fr ont()const
{
if( empty() )
fatalError("fro nt sur conteneur vide") ;
return m_values[0] ;
}
void TvectorStat::in sert( int index, int value )
{
if( full() )
fatalError("ins ert sur conteneur plein") ;
if( index<0 || index>m_size )
fatalError("ins ert avec index non-valide") ;
for( int i=m_size; i>=index; i-- )
m_values[i+1] = m_values[i] ;
m_values[index] = value ;
m_size++ ;
}
void TvectorStat::re move( int index )
{
if( !validIndex(ind ex) )
fatalError("rem ove avec index non-valide") ;
for( int i=index; i<m_size-1; i++ )
m_values[i] = m_values[i+1] ;
m_size-- ;
}
int& TvectorStat::at ( int index )
{
if( !validIndex(ind ex) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int TvectorStat::at ( int index )const
{
if( !validIndex(ind ex) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int& TvectorStat::op erator[]( int index )
{
return m_values[index] ;
}
int TvectorStat::op erator[]( int index )const
{
return m_values[index] ;
}
bool TvectorStat::op erator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_s ize ;
int i = 0;
while( i<m_size && egal ){
egal = m_values[i] == opG.m_values[i] ;
i++ ;
}
return egal ;
}
bool TvectorStat::op erator!=( const TvectorStat& opG )
{
return !(*this==opG) ;
}
bool TvectorStat::em pty()const
{
return m_size<=0 ;
}
bool TvectorStat::fu ll()const
{
return m_size>=m_capac ity ;
}
int TvectorStat::si ze()const
{
return m_size ;
}
int TvectorStat::ca pacity()const
{
return m_capacity ;
}
void TvectorStat::re serve( int newCapacity )
{
int* buffer ;
if( newCapacity<0 )
newCapacity = 0 ;
if( m_size>newCapac ity )
m_size = newCapacity ;
m_capacity = newCapacity ;
buffer = new int[newCapacity] ;
for( int i=0; i<m_size; i++ )
buffer[i] = m_values[i] ;
delete[] m_values ;
m_values = buffer ;
}
void TvectorStat::re size( int newSize, int def )
{
if( newSize<0 )
newSize = 0 ;
if( newSize<=m_size ){
m_size=newSize ;
return ;
}
if( newSize>m_capac ity )
fatalError("res ize supérieur à capcity") ;
while( m_size<newSize )
push_back(def) ;
}
void TvectorStat::sw ap( TvectorStat& opG )
{
int t_capacity = m_capacity ;
int t_size = m_size ;
int* t_values = m_values ;
m_capacity = opG.m_capacity ;
m_size = opG.m_size ;
m_values = opG.m_values ;
opG.m_capacity = t_capacity ;
opG.m_size = t_size ;
opG.m_values = t_values ;
}
bool TvectorStat::va lidIndex( int index )const
{
return index>=0 && index<m_size ;
}
ostream& operator<<( ostream& out, const TvectorStat& opG )
{
out << '{' << opG.size() << '/' << opG.capacity() << '}' ;
out << '[' ;
if( opG.empty() )
out << "vide" ;
else{
for( int i=0; i<opG.size()-1; i++ )
out << opG[i] << ';' ;
out << opG[opG.size()-1] ;
}
out << ']' ;
return out ;
}
_______________ _______________ _________
_______________ __________vecto rStat.h______
//---------------------------------------------------------
// Fichier : vectorStat.h
// esnig - Laboratoire de programmation
//---------------------------------------------------------
#ifndef VECTORSTAT_H
#define VECTORSTAT_H
#include <iostream>
using namespace std ;
class TvectorStat{
public:
TvectorStat( int capacity ) ;
TvectorStat( const TvectorStat& vec ) ;
~TvectorStat() ;
TvectorStat& operator=( const TvectorStat& opG ) ;
void push_back( int value ) ;
void pop_back() ;
int& back() ;
int back()const ;
int& front() ;
int front()const ;
void insert( int index, int value ) ;
void remove( int index ) ;
int& at( int index ) ; // pourquoi int&
int at( int index )const ;
int& operator[]( int index ) ; // c'est quel opérateur ?
int operator[]( int index )const ;
bool operator==( const TvectorStat& opG ) ;
bool operator!=( const TvectorStat& opG ) ;
bool empty()const ;
bool full()const ;
int size()const ;
int capacity()const ;
void reserve( int newCapacity ) ;
void resize( int newSize, int def=0 ) ;
void swap( TvectorStat& opG ) ;
private:
bool validIndex( int index )const ; // pourquoi on met cette fonction dans
private?
int* m_values ;
int m_capacity ;
int m_size ;
} ;
ostream& operator<<( ostream& out, const TvectorStat& opG ) ;// pourquoi
cette fonction est dehors?
#endif
I am trying to Print (display) all the items of a vector (class TvectorStat)
which is into another class (class Tpile). Apparently I can't reach m_size
of Class TvectorStat because it is in the private part of the class but I
don't understand why because this class (TvectorStat) belongs to the class
Tpile...Can someone look at my files?
_______________ __________main. cpp_______
#include "vectorStat .h"
#include "pile.h"
int main()
{
Tpile p;
p.push(11);
p.push(22);
p.push(33);
p.push(44);
p.push(55);
p.push(66);
p.push(77);
p.push(88);
p.push(99);
p.push(111);
cout<<p.printLn (0)<<endl;
cout<<p.printLn (1)<<endl;
cout<<p.printLn (2)<<endl;
cout<<p.printLn (3)<<endl;
cout<<p.printLn (4)<<endl;
cout<<p.printLn (5)<<endl;
cout<<p.printLn (6)<<endl;
cout<<p.printLn (7)<<endl;
cout<<p.printLn (8)<<endl;
cout<<p.printLn (9)<<endl;
cout<<p.printLn (10)<<endl;
p.affiche();
return 0;
}
_______________ _______________ _______
_______________ _________pile.c pp___________
// pile.cpp
#include "vectorStat .h"
#include "pile.h"
void Tpile::push(int val)
{
m_data.push_bac k(val);
}
void Tpile::pop()
{
m_data.pop_back ();
}
int Tpile::top()
{
return m_data.back();// int& TvectorStat::ba ck() OU BIEN int
TvectorStat::ba ck()const ?
}
int Tpile::printLn( int pos)
{
return m_data.at(pos);
}
void Tpile::affiche( )
{
int i=0;
for (i=0;i<m_size;i ++)
cout<<m_data[i]<<endl;
}
_______________ _______________ _____
_______________ __pile.h_______ ___________
// pile.h
#ifndef PILE_H
#define PILE_H
#include <iostream>
using namespace std;
class Tpile{
public:
Tpile():m_data( 100){}
void push(int val);
void pop();
int top();
int printLn(int pos);
void affiche();
private:
TvectorStat m_data;
};
#endif
_______________ _______________ ________
_______________ ___________vect or.cpp_______
//---------------------------------------------------------
// Fichier : vectorStat.cpp
// esnig - Laboratoire de programmation
//---------------------------------------------------------
#include "vectorStat .h"
void fatalError( const char* str )
{
cout << "TvectorSta t Error : " << str << endl ;
exit(1) ;
}
TvectorStat::Tv ectorStat( int capacity )
:m_capacity(cap acity),m_size(0 )
{
m_values = new int[m_capacity] ;
if( m_values==NULL )
fatalError("All ocation à la construction") ;
}
TvectorStat::Tv ectorStat( const TvectorStat& vec )
:m_capacity(vec .m_capacity),m_ size(vec.m_size )
{
m_values = new int[vec.m_capacity] ;
if( m_values==NULL )
fatalError("All ocation à la copie") ;
for( int i=0; i<vec.m_size;i+ + )
m_values[i] = vec.m_values[i] ;
}
TvectorStat::~T vectorStat()
{
delete[] m_values ;
}
TvectorStat& TvectorStat::op erator=( const TvectorStat& opG )
{
if( this != &opG ){
delete[] m_values ;
m_capacity = opG.m_capacity ;
m_size = opG.m_size ;
m_values = new int[opG.m_capacity] ;
if( m_values==NULL )
fatalError("All ocation à l'affectation") ;
for( int i=0; i<opG.m_size;i+ + )
m_values[i] = opG.m_values[i] ;
}
return *this ;
}
void TvectorStat::pu sh_back( int value )
{
if( full() )
fatalError("pus h_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}
void TvectorStat::po p_back()
{
if( !empty() )
m_size-- ;
}
int& TvectorStat::ba ck()
{
if( empty() )
fatalError("bac k sur conteneur vide") ;
return m_values[m_size-1] ;
}
int TvectorStat::ba ck()const
{
if( empty() )
fatalError("bac k sur conteneur vide") ;
return m_values[m_size-1] ;
}
int& TvectorStat::fr ont()
{
if( empty() )
fatalError("fro nt sur conteneur vide") ;
return m_values[0] ;
}
int TvectorStat::fr ont()const
{
if( empty() )
fatalError("fro nt sur conteneur vide") ;
return m_values[0] ;
}
void TvectorStat::in sert( int index, int value )
{
if( full() )
fatalError("ins ert sur conteneur plein") ;
if( index<0 || index>m_size )
fatalError("ins ert avec index non-valide") ;
for( int i=m_size; i>=index; i-- )
m_values[i+1] = m_values[i] ;
m_values[index] = value ;
m_size++ ;
}
void TvectorStat::re move( int index )
{
if( !validIndex(ind ex) )
fatalError("rem ove avec index non-valide") ;
for( int i=index; i<m_size-1; i++ )
m_values[i] = m_values[i+1] ;
m_size-- ;
}
int& TvectorStat::at ( int index )
{
if( !validIndex(ind ex) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int TvectorStat::at ( int index )const
{
if( !validIndex(ind ex) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int& TvectorStat::op erator[]( int index )
{
return m_values[index] ;
}
int TvectorStat::op erator[]( int index )const
{
return m_values[index] ;
}
bool TvectorStat::op erator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_s ize ;
int i = 0;
while( i<m_size && egal ){
egal = m_values[i] == opG.m_values[i] ;
i++ ;
}
return egal ;
}
bool TvectorStat::op erator!=( const TvectorStat& opG )
{
return !(*this==opG) ;
}
bool TvectorStat::em pty()const
{
return m_size<=0 ;
}
bool TvectorStat::fu ll()const
{
return m_size>=m_capac ity ;
}
int TvectorStat::si ze()const
{
return m_size ;
}
int TvectorStat::ca pacity()const
{
return m_capacity ;
}
void TvectorStat::re serve( int newCapacity )
{
int* buffer ;
if( newCapacity<0 )
newCapacity = 0 ;
if( m_size>newCapac ity )
m_size = newCapacity ;
m_capacity = newCapacity ;
buffer = new int[newCapacity] ;
for( int i=0; i<m_size; i++ )
buffer[i] = m_values[i] ;
delete[] m_values ;
m_values = buffer ;
}
void TvectorStat::re size( int newSize, int def )
{
if( newSize<0 )
newSize = 0 ;
if( newSize<=m_size ){
m_size=newSize ;
return ;
}
if( newSize>m_capac ity )
fatalError("res ize supérieur à capcity") ;
while( m_size<newSize )
push_back(def) ;
}
void TvectorStat::sw ap( TvectorStat& opG )
{
int t_capacity = m_capacity ;
int t_size = m_size ;
int* t_values = m_values ;
m_capacity = opG.m_capacity ;
m_size = opG.m_size ;
m_values = opG.m_values ;
opG.m_capacity = t_capacity ;
opG.m_size = t_size ;
opG.m_values = t_values ;
}
bool TvectorStat::va lidIndex( int index )const
{
return index>=0 && index<m_size ;
}
ostream& operator<<( ostream& out, const TvectorStat& opG )
{
out << '{' << opG.size() << '/' << opG.capacity() << '}' ;
out << '[' ;
if( opG.empty() )
out << "vide" ;
else{
for( int i=0; i<opG.size()-1; i++ )
out << opG[i] << ';' ;
out << opG[opG.size()-1] ;
}
out << ']' ;
return out ;
}
_______________ _______________ _________
_______________ __________vecto rStat.h______
//---------------------------------------------------------
// Fichier : vectorStat.h
// esnig - Laboratoire de programmation
//---------------------------------------------------------
#ifndef VECTORSTAT_H
#define VECTORSTAT_H
#include <iostream>
using namespace std ;
class TvectorStat{
public:
TvectorStat( int capacity ) ;
TvectorStat( const TvectorStat& vec ) ;
~TvectorStat() ;
TvectorStat& operator=( const TvectorStat& opG ) ;
void push_back( int value ) ;
void pop_back() ;
int& back() ;
int back()const ;
int& front() ;
int front()const ;
void insert( int index, int value ) ;
void remove( int index ) ;
int& at( int index ) ; // pourquoi int&
int at( int index )const ;
int& operator[]( int index ) ; // c'est quel opérateur ?
int operator[]( int index )const ;
bool operator==( const TvectorStat& opG ) ;
bool operator!=( const TvectorStat& opG ) ;
bool empty()const ;
bool full()const ;
int size()const ;
int capacity()const ;
void reserve( int newCapacity ) ;
void resize( int newSize, int def=0 ) ;
void swap( TvectorStat& opG ) ;
private:
bool validIndex( int index )const ; // pourquoi on met cette fonction dans
private?
int* m_values ;
int m_capacity ;
int m_size ;
} ;
ostream& operator<<( ostream& out, const TvectorStat& opG ) ;// pourquoi
cette fonction est dehors?
#endif
Comment