473,698 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_bac k(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 << "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 ;
}
//vectorStat.h
#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 ;
}

Jul 23 '05 #1
1 1278
=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
4967
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: http://wiki.wxpython.org/index.cgi/Printing but is impossible to use this script even if I do exactly as said there. I think the script is buggy or I am not able to use it, even if seems very simple to use... Anyone can give me an hint on how to easily and simply print...
16
10426
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 this page? openWindow.document.write("<a href='#' onClick='self.close()'>Close<\/a><\/p>")
5
2155
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 really stuck here (probably the ol' Not seeing the tree cause of the forest thingy). But I just can not get this to work. The following is my Test example code. Just needs a Button1 on a blank Form. Add in PrintDocument1, PrintPreviewDialog1,...
14
7871
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 std::string. I had something working with std::string. I simply treated it as an STL container, and iterated over its elements. The results were a bit confusing to me. Some of the stuff was printing out as 1 or 2 characters hex numbers, as I...
4
1831
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 and do NOT appear on the print preview, but do when they actually print: Radio buttons aren't printing at all. Disable values are not printing, like the text in a disabled text box.
21
8140
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 maximum value N 2. loop through 1...N 3. count # times each occurred
4
1679
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 I want to use the toString() method on them all. Is there a way of defining toString() so that I don't have to loop through all of the particles printing each individually. The code might explain it better than I can so the relevant part of...
23
7402
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
11
1678
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, the behaviours of std::vector::iterator can not meet my requirements. I need to redefine the vector::iterator's behavious such as ++. It means "next position in the object sequence" in STL but now I redefine it to mean "next position within a 2-d...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.