By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

method printing values from a vector not working

P: n/a
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[i]->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[i]->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[i] = vec.m_values[i] ;
}

TvectorStat::~TvectorStat()
{
delete[] m_values ;
}

TvectorStat& TvectorStat::operator=( 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[i] = opG.m_values[i] ;
}
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[i] ;
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[i] = 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::operator[]( int index )
{
return m_values[index] ;
}

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

bool TvectorStat::operator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_size ;
int i = 0;

while( i<m_size && egal ){
egal = m_values[i] == opG.m_values[i] ;
i++ ;
}
return egal ;
}

bool TvectorStat::operator!=( 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[i] = m_values[i] ;

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[i] << ';' ;
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[i] = vec.m_values[i] ;
}

TvectorStat::~TvectorStat()
{
delete[] m_values ;
}

TvectorStat& TvectorStat::operator=( 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[i] = opG.m_values[i] ;
}
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[i] ;
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[i] = 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::operator[]( int index )
{
return m_values[index] ;
}

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

bool TvectorStat::operator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_size ;
int i = 0;

while( i<m_size && egal ){
egal = m_values[i] == opG.m_values[i] ;
i++ ;
}
return egal ;
}

bool TvectorStat::operator!=( 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[i] = m_values[i] ;

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[i] << ';' ;
out << opG[opG.size()-1] ;
}
out << ']' ;
return out ;
}

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
=steFF= wrote:

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[i]->printLn(); (in my file pile.h)
Can someone tell why it says that?

The error message basically says:

There is a statement

m_data[i]->printLn();

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

m_data[i]

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

Question: What is the data type of the result of m_data[i] ?
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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.