473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

method printing values from a vector not working

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
1 1262
=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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mario | last post by:
Hello all, I'm trying hard to make possible to print some simple text from python to the default printer using wxPython, after days of internet searches I found this page:...
16
by: gb | last post by:
Hi All, Ive created a popup page using 'var openWindow = window.open("new","pop")' And added content using openWindow.document.write(" "); statements. But now I would like to be able to print...
5
by: Mr. B | last post by:
This is driving me NUTZ!!! I've been screwing around on this for a week now. And I have tried to find examples similar to what I have (nada). Got lots of streaming a TXT file... bah! I am...
14
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and...
4
by: Tracey | last post by:
Hey folks, I have an ASP.NET web app that's working just fine, but a small percentage have two printing problems that don't happen to the vast majority of users. These problems are listed below...
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
4
by: Cleverbum | last post by:
I have created a class Particle which has a method toString() that prints out all the useful information. In my main program I create an array of these objects and once I've fiddled with them a bit...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.