S
Stephane Vollet
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.cpp___________
// pile.cpp
#include "vectorStat.h"
#include "pile.h"
void Tpile:ush(int val)
{
m_data.push_back(val);
}
void Tpile:op()
{
m_data.pop_back();
}
int Tpile::top()
{
return m_data.back();// int& TvectorStat::back() OU BIEN int
TvectorStat::back()const ?
}
int Tpile:rintLn(int pos)
{
return m_data.at(pos);
}
void Tpile::affiche()
{
int i=0;
for (i=0;i<m_size;i++)
cout<<m_data<<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
______________________________________
__________________________vector.cpp_______
//---------------------------------------------------------
// Fichier : vectorStat.cpp
// esnig - Laboratoire de programmation
//---------------------------------------------------------
#include "vectorStat.h"
void fatalError( const char* str )
{
cout << "TvectorStat Error : " << str << endl ;
exit(1) ;
}
TvectorStat::TvectorStat( int capacity )
:m_capacity(capacity),m_size(0)
{
m_values = new int[m_capacity] ;
if( m_values==NULL )
fatalError("Allocation à la construction") ;
}
TvectorStat::TvectorStat( 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("Allocation à la copie") ;
for( int i=0; i<vec.m_size;i++ )
m_values = vec.m_values ;
}
TvectorStat::~TvectorStat()
{
delete[] m_values ;
}
TvectorStat& TvectorStat:perator=( 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("Allocation à l'affectation") ;
for( int i=0; i<opG.m_size;i++ )
m_values = opG.m_values ;
}
return *this ;
}
void TvectorStat:ush_back( int value )
{
if( full() )
fatalError("push_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}
void TvectorStat:op_back()
{
if( !empty() )
m_size-- ;
}
int& TvectorStat::back()
{
if( empty() )
fatalError("back sur conteneur vide") ;
return m_values[m_size-1] ;
}
int TvectorStat::back()const
{
if( empty() )
fatalError("back sur conteneur vide") ;
return m_values[m_size-1] ;
}
int& TvectorStat::front()
{
if( empty() )
fatalError("front sur conteneur vide") ;
return m_values[0] ;
}
int TvectorStat::front()const
{
if( empty() )
fatalError("front sur conteneur vide") ;
return m_values[0] ;
}
void TvectorStat::insert( int index, int value )
{
if( full() )
fatalError("insert sur conteneur plein") ;
if( index<0 || index>m_size )
fatalError("insert avec index non-valide") ;
for( int i=m_size; i>=index; i-- )
m_values[i+1] = m_values ;
m_values[index] = value ;
m_size++ ;
}
void TvectorStat::remove( int index )
{
if( !validIndex(index) )
fatalError("remove avec index non-valide") ;
for( int i=index; i<m_size-1; i++ )
m_values = m_values[i+1] ;
m_size-- ;
}
int& TvectorStat::at( int index )
{
if( !validIndex(index) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int TvectorStat::at( int index )const
{
if( !validIndex(index) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int& TvectorStat:perator[]( int index )
{
return m_values[index] ;
}
int TvectorStat:perator[]( int index )const
{
return m_values[index] ;
}
bool TvectorStat:perator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_size ;
int i = 0;
while( i<m_size && egal ){
egal = m_values == opG.m_values ;
i++ ;
}
return egal ;
}
bool TvectorStat:perator!=( const TvectorStat& opG )
{
return !(*this==opG) ;
}
bool TvectorStat::empty()const
{
return m_size<=0 ;
}
bool TvectorStat::full()const
{
return m_size>=m_capacity ;
}
int TvectorStat::size()const
{
return m_size ;
}
int TvectorStat::capacity()const
{
return m_capacity ;
}
void TvectorStat::reserve( int newCapacity )
{
int* buffer ;
if( newCapacity<0 )
newCapacity = 0 ;
if( m_size>newCapacity )
m_size = newCapacity ;
m_capacity = newCapacity ;
buffer = new int[newCapacity] ;
for( int i=0; i<m_size; i++ )
buffer = m_values ;
delete[] m_values ;
m_values = buffer ;
}
void TvectorStat::resize( int newSize, int def )
{
if( newSize<0 )
newSize = 0 ;
if( newSize<=m_size ){
m_size=newSize ;
return ;
}
if( newSize>m_capacity )
fatalError("resize supérieur à capcity") ;
while( m_size<newSize )
push_back(def) ;
}
void TvectorStat::swap( 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::validIndex( 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 << ';' ;
out << opG[opG.size()-1] ;
}
out << ']' ;
return out ;
}
_______________________________________
_________________________vectorStat.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.cpp___________
// pile.cpp
#include "vectorStat.h"
#include "pile.h"
void Tpile:ush(int val)
{
m_data.push_back(val);
}
void Tpile:op()
{
m_data.pop_back();
}
int Tpile::top()
{
return m_data.back();// int& TvectorStat::back() OU BIEN int
TvectorStat::back()const ?
}
int Tpile:rintLn(int pos)
{
return m_data.at(pos);
}
void Tpile::affiche()
{
int i=0;
for (i=0;i<m_size;i++)
cout<<m_data<<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
______________________________________
__________________________vector.cpp_______
//---------------------------------------------------------
// Fichier : vectorStat.cpp
// esnig - Laboratoire de programmation
//---------------------------------------------------------
#include "vectorStat.h"
void fatalError( const char* str )
{
cout << "TvectorStat Error : " << str << endl ;
exit(1) ;
}
TvectorStat::TvectorStat( int capacity )
:m_capacity(capacity),m_size(0)
{
m_values = new int[m_capacity] ;
if( m_values==NULL )
fatalError("Allocation à la construction") ;
}
TvectorStat::TvectorStat( 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("Allocation à la copie") ;
for( int i=0; i<vec.m_size;i++ )
m_values = vec.m_values ;
}
TvectorStat::~TvectorStat()
{
delete[] m_values ;
}
TvectorStat& TvectorStat:perator=( 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("Allocation à l'affectation") ;
for( int i=0; i<opG.m_size;i++ )
m_values = opG.m_values ;
}
return *this ;
}
void TvectorStat:ush_back( int value )
{
if( full() )
fatalError("push_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}
void TvectorStat:op_back()
{
if( !empty() )
m_size-- ;
}
int& TvectorStat::back()
{
if( empty() )
fatalError("back sur conteneur vide") ;
return m_values[m_size-1] ;
}
int TvectorStat::back()const
{
if( empty() )
fatalError("back sur conteneur vide") ;
return m_values[m_size-1] ;
}
int& TvectorStat::front()
{
if( empty() )
fatalError("front sur conteneur vide") ;
return m_values[0] ;
}
int TvectorStat::front()const
{
if( empty() )
fatalError("front sur conteneur vide") ;
return m_values[0] ;
}
void TvectorStat::insert( int index, int value )
{
if( full() )
fatalError("insert sur conteneur plein") ;
if( index<0 || index>m_size )
fatalError("insert avec index non-valide") ;
for( int i=m_size; i>=index; i-- )
m_values[i+1] = m_values ;
m_values[index] = value ;
m_size++ ;
}
void TvectorStat::remove( int index )
{
if( !validIndex(index) )
fatalError("remove avec index non-valide") ;
for( int i=index; i<m_size-1; i++ )
m_values = m_values[i+1] ;
m_size-- ;
}
int& TvectorStat::at( int index )
{
if( !validIndex(index) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int TvectorStat::at( int index )const
{
if( !validIndex(index) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int& TvectorStat:perator[]( int index )
{
return m_values[index] ;
}
int TvectorStat:perator[]( int index )const
{
return m_values[index] ;
}
bool TvectorStat:perator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_size ;
int i = 0;
while( i<m_size && egal ){
egal = m_values == opG.m_values ;
i++ ;
}
return egal ;
}
bool TvectorStat:perator!=( const TvectorStat& opG )
{
return !(*this==opG) ;
}
bool TvectorStat::empty()const
{
return m_size<=0 ;
}
bool TvectorStat::full()const
{
return m_size>=m_capacity ;
}
int TvectorStat::size()const
{
return m_size ;
}
int TvectorStat::capacity()const
{
return m_capacity ;
}
void TvectorStat::reserve( int newCapacity )
{
int* buffer ;
if( newCapacity<0 )
newCapacity = 0 ;
if( m_size>newCapacity )
m_size = newCapacity ;
m_capacity = newCapacity ;
buffer = new int[newCapacity] ;
for( int i=0; i<m_size; i++ )
buffer = m_values ;
delete[] m_values ;
m_values = buffer ;
}
void TvectorStat::resize( int newSize, int def )
{
if( newSize<0 )
newSize = 0 ;
if( newSize<=m_size ){
m_size=newSize ;
return ;
}
if( newSize>m_capacity )
fatalError("resize supérieur à capcity") ;
while( m_size<newSize )
push_back(def) ;
}
void TvectorStat::swap( 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::validIndex( 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 << ';' ;
out << opG[opG.size()-1] ;
}
out << ']' ;
return out ;
}
_______________________________________
_________________________vectorStat.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