method printing values from a vector not working

S

=steFF=

Hi,

I am trying to print the values I have in a vector and my compiler says:
error C2227: left of '->printLn' must point to class/struct/union
on this instruction: m_data->printLn(); (in my file pile.h)
Can someone tell why it says that?

Also, I don't understand why I am not allowed to put const on this method:
void printLn();
when I write void printLn() const; It says : 'printLn' : overloaded member
function 'void (void)' not found in 'Tpile'
Though I should be abble to make it a "const method" as I am not changing my
object when using it.

here my code:
//main.cpp

#include "pile.h"
#include "vectorStat.h"

int main()
{
Tpile p;

p.push(11);
p.push(22);
p.push(33);
p.push(44);

cout<<p.size();

p.printLn();

return 0;
}

//pile.cpp

int Tpile::back()
{
return m_data.back();
}

void Tpile::pop()
{
m_data.pop_back();
}

void Tpile::push(int value)
{
m_data.push_back(value);
}

void Tpile::printLn()
{
for (int i=0;i<size();i++)

m_data->printLn(); //pourquoi pas const?
}

int Tpile::size()
{
return m_data.size();
}

// pile.h
#ifndef PILE_H
#define PILE_H

#include <iostream>
#include "vectorStat.h"
using namespace std;

class Tpile{

public:

Tpile():m_data(100){}

int back();

void push(int value);

void pop();

void printLn() const; // const ?

int size();

private:

TvectorStat m_data;

};
#endif

//vectorStat.cpp
#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::eek: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::push_back( int value )
{
if( full() )
fatalError("push_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}

void TvectorStat::pop_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::eek:perator[]( int index )
{
return m_values[index] ;
}

int TvectorStat::eek:perator[]( int index )const
{
return m_values[index] ;
}

bool TvectorStat::eek: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::eek: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
#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::eek: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::push_back( int value )
{
if( full() )
fatalError("push_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}

void TvectorStat::pop_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::eek:perator[]( int index )
{
return m_values[index] ;
}

int TvectorStat::eek:perator[]( int index )const
{
return m_values[index] ;
}

bool TvectorStat::eek: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::eek: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 ;
}
 
K

Karl Heinz Buchegger

=steFF= said:
Hi,

I am trying to print the values I have in a vector and my compiler says:
error C2227: left of '->printLn' must point to class/struct/union
on this instruction: m_data->printLn(); (in my file pile.h)
Can someone tell why it says that?


The error message basically says:

There is a statement

m_data->printLn();

now take the left of '->printLn()', which leaves you with

m_data

Now, using -> on that subexpression, requires m_data to be
a pointer (or to be an expression which evaluates to a pointer),
which it isn't. m_data is *not* a pointer. Hence the error
message.

Question: What is the data type of the result of m_data ?
Also, I don't understand why I am not allowed to put const on this method:
void printLn();
when I write void printLn() const; It says : 'printLn' : overloaded member
function 'void (void)' not found in 'Tpile'
Though I should be abble to make it a "const method" as I am not changing my
object when using it.

Right. I can't make anything with that error message. First fix your first problem
and see if this influences your second problem. It might do so, since both problems
are located in the same function and the compiler might just got confused with your
first problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top