473,376 Members | 1,043 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,376 software developers and data experts.

Thanks and excuse me, but now another problem

Excuse me for the previous post, I have tried to modify the classes of the
Savannah example and now I succeed to compile the classes but I have a
strange problem.

When I declar an object of the class Savan, I cannot see the methdo of the
class:
Savan s();
Leone l(........);
s.addLeone(l);
the methdo addLeone() isn't view, but all the method of the class Savan
aren't view.
//Savan s();
//s.addLeone(l);
error C2228: the element on the left ".addLeone" must have a type class,
structure or union
the type is "overloaded-function"
Why this behaviour?
If I define a constructor like this Savan(Leone &l, Zebra &z), I can view
and I can use all the methods.
//Savan s(l, z);
//s.addLeone(d);
In this way I don't receiver message error, but When I start the program all
is blocked
This constructor is implemented in this way:

Savan::Savan(Leone &l, Zebra &z){
addLeone(l);
addZebra(z);
}

Another question:
How can I do to add an object to a dynamic array:
I have defined 2 pointer and two int:

Leone *lPtr;
Zebra *zPtr;
int lsize, zsize;

For add an object to lPtr or zPtr, I have defined the method in this way:
void Savan::addLeone(Leone &l){
Leone *tmp;
tmp=lPtr;
lsize=lsize+1;
lPtr=new Leone[lsize];
lPtr=tmp;
lPtr[lsize-1]=l;
delete[] tmp;
}
Is correct what I write?
The constructor without arguments inizialize lPtr=0; zPtr=0; lsize=0;
zsize=0;

Thanks and excuse me.
Jul 22 '05 #1
3 1314
Piotre Ugrumov wrote:
Excuse me for the previous post, I have tried to modify the classes of
the Savannah example and now I succeed to compile the classes but I
have a strange problem.

When I declar an object of the class Savan, I cannot see the methdo of
the class:
Savan s();
This declares a function named s that takes no parameters and returns an
object of type Savan.
Leone l(........);
s.addLeone(l);
the methdo addLeone() isn't view, but all the method of the class
Savan aren't view.
//Savan s();
//s.addLeone(l);
error C2228: the element on the left ".addLeone" must have a type
class, structure or union
the type is "overloaded-function"
Why this behaviour?
Because s actually is a function. Try teplacing it with:

Savan s;
If I define a constructor like this Savan(Leone &l, Zebra &z), I can
view and I can use all the methods.
//Savan s(l, z);
This defines an object of type Savan that is initialized with the values
l and z.
//s.addLeone(d);
In this way I don't receiver message error, but When I start the
program all is blocked
This constructor is implemented in this way:

Savan::Savan(Leone &l, Zebra &z){
addLeone(l);
addZebra(z);
}

Another question:
How can I do to add an object to a dynamic array:
I have defined 2 pointer and two int:

Leone *lPtr;
Zebra *zPtr;
int lsize, zsize;

For add an object to lPtr or zPtr, I have defined the method in this
way: void Savan::addLeone(Leone &l){
Leone *tmp;
tmp=lPtr;
lsize=lsize+1;
lPtr=new Leone[lsize];
lPtr=tmp;
lPtr[lsize-1]=l;
delete[] tmp;
}
Is correct what I write?


You lose all the objects that were in the old array, since you don't
copy them to the new location. Besides that, the code is not very
efficient, since it needs to reallocate for every object added. I
suggest to use std::vector instead of arrays. std::vector has all the
functionality for resizing already implemented and is probably a lot
faster than your code, since it does the reallocation and copying in
blocks so that it doesn't need to do them for every object. Just define
them as:

std::vector<Leone> lVector;
std::vector<Zebra> zVector;

And your addLeone funciton would look like:

void Savan::addLeone(Leone &l){
lVector.push_back(l);
}

You can access the object the same way as with an array, using the []
operator.

Jul 22 '05 #2
On Mon, 19 Jan 2004 10:02:19 +0000, Piotre Ugrumov wrote:
Excuse me for the previous post, I have tried to modify the classes of the
Savannah example and now I succeed to compile the classes but I have a
strange problem.

When I declar an object of the class Savan, I cannot see the methdo of the
class:
Savan s();
Surprise! This declares a function s, taking no arguments and returning a
Savan. The rule is, if it can declare a function, it declares a function.
Leone l(........);
s.addLeone(l);
And we cannot use '.' on a function, so the compiler rightly complains.

Change:
Savan s();
to
Savan s;
and it should work.
If I define a constructor like this Savan(Leone &l, Zebra &z), I can view
and I can use all the methods.
//Savan s(l, z);
This cannot be a function declaration, so it must be an variable
definition. That is why this works.
Another question:
How can I do to add an object to a dynamic array:
I have defined 2 pointer and two int:

Leone *lPtr;
Zebra *zPtr;
int lsize, zsize;

For add an object to lPtr or zPtr, I have defined the method in this way:
void Savan::addLeone(Leone &l){
Leone *tmp;
tmp=lPtr;
lsize=lsize+1;
lPtr=new Leone[lsize];
lPtr=tmp;
lPtr[lsize-1]=l;
delete[] tmp;
}
Is correct what I write?
The constructor without arguments inizialize lPtr=0; zPtr=0; lsize=0;
zsize=0;


Uch! No, it's not. You never copy the elements. Why not use a std::vector
or a std::list? They resize automagically.

std::vector<Leone> LeoneVector;

void Savan::addLeone(const Leone &l){
LeoneVector.push_back(l);
}

HTH,
M4

Jul 22 '05 #3
Piotre Ugrumov wrote:

Excuse me for the previous post, I have tried to modify the classes of the
Savannah example and now I succeed to compile the classes but I have a
strange problem.

When I declar an object of the class Savan, I cannot see the methdo of the
class:
Savan s();
This does *not* define an object. It does something completely different:
It declares a function s, which takes no arguments and returns a Savan object.

You want:
Savan s;
Leone l(........);
s.addLeone(l);
the methdo addLeone() isn't view, but all the method of the class Savan
aren't view.
//Savan s();
//s.addLeone(l);
error C2228: the element on the left ".addLeone" must have a type class,
structure or union
the type is "overloaded-function"
Why this behaviour?
Because there is a C++ rule: if something could be a function declaration,
then it *is* a function declaration (a prototype).

Savan s();

looks like a function declaration in the same way as eg.

double MyFunction();

does. Thus the compiler treats it as such.

Another question:
How can I do to add an object to a dynamic array:
I have defined 2 pointer and two int:

Leone *lPtr;
Zebra *zPtr;
int lsize, zsize;

For add an object to lPtr or zPtr, I have defined the method in this way:
void Savan::addLeone(Leone &l){
Leone *tmp;
tmp=lPtr;
lsize=lsize+1;
lPtr=new Leone[lsize];
lPtr=tmp;
lPtr[lsize-1]=l;
delete[] tmp;
}
Is correct what I write?


No.

void Savan::addLeone(Leone &l)
{
Leone* tmp;

tmp = lPtr;

lptr = new Leone[lsize + 1];
for( int i = 0; i < lsize; ++i ) {
lptr[i] = tmp[i];
}
lsize++;

lptr[lsize-1] = l;
delete [] tmp;
}

But even now, there are things that can go wrong (eg. what happens
if the memory allocation failes).

Given your current knowledge, it's best to not do the dynamic memory
management by yourself (there are lots of pitfalls), but instead let
a prebuilt class do it. C++ comes with such a class and it is called vector

#include <string>
#include <vector>
#include <iostream>

class Item
{
public:
Item( const std::string& Name ) : m_Name( Name ) {}
std::string Name() { return m_Name; }

private:
std::string m_Name;
};

class MyTest
{
public:
void Add( const Item& NewItem )
{
m_Items.push_back( NewItem );
}

void PrintAll()
{
for( int i = 0; i < m_Items.size(); ++i )
std::cout << m_Items[i].Name() << '\n';
}

private:
std::vector< Item > m_Items;
};

int main()
{
MyTest TheContainer;
Item Chest( "Chest" );

TheContainer.Add( Chest );
TheContainer.Add( Item( "Chair" ) );
TheContainer.Add( Item( "Lion" ) );

TheContainer.PrintAll();

return 0;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: rong.guo | last post by:
Greetings! Please see my data below, for each account, I would need the lastest balance_date with the corresponding balance. Can anyone help me with the query? Thanks a lot! create table a...
5
by: dkintheuk | last post by:
Set rs = db.OpenRecordset("tblIncident") I'm getting a "Run-time error 13: Type Mismatch" this time. I've got Dim db as database and set db = currentdb DAO reference set up as well. The...
2
by: fhadian1 | last post by:
Excuse me!! Would you stop for a moment?! O...man...Haven't you thought-one day- about yourself ? Who has made it? Have you seen a design which hasn't a designer ?! Have you seen a...
0
by: mossab00019 | last post by:
Excuse me Excuse me!!. Would you stop for a moment?! O...man...Haven't you thought-one day- about yourself ? Who has made it? Have you seen a design which hasn't a designer ?! Have you seen a...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.