C++ STL vector Question

D

Drew

Hello:

I am having trouble getting a vector of a class to work from another class

//********************
class CaClass{

int idata;

CaClass(){idata =1;}
}
//********************
//********************
class CanotherClass{

vector <CaClass> aClass;

int iotherdata;
}
//********************

main()
{
CanotherClass anotherClass;
anotherClass->aClass.push_back(CaClass); //compiles fine but this
fails
}
//*********************************************************

Any ideas?
Know of any example that demo a vector used from within another class?

Thank you.

Drew
 
B

Buster

Drew said:
Hello:

I am having trouble getting a vector of a class to work from another class

//********************
class CaClass{

int idata;

CaClass(){idata =1;}
}
//********************
//********************
class CanotherClass{

vector <CaClass> aClass;

int iotherdata;
}
//********************

main()
{
CanotherClass anotherClass;
anotherClass->aClass.push_back(CaClass); //compiles fine but this
fails

It shouldn't compile. CaClass is a type (or class) name. You must
provide a value (or object) to push_back. Besides, aClass is private in
CanotherClass. And main always returns int, always.

}
//*********************************************************

Any ideas?
Know of any example that demo a vector used from within another class?

Thank you.

Drew

Correcting your syntax and getting rid of those stupid confusing names:

#include <vector>

class X
{
int i;
X () : i (1) { }
};

class Y
{
public:
std::vector <X> v;
int i;
};

int main ()
{
Y y;
y->v.push_back (X ());
}

Easy enough. "X ()" means "a temporary, default-constructed instance of X".

Regards,
Buster.
 
C

Chris \( Val \)

| Hello:
|
| I am having trouble getting a vector of a class to work from another class
|
| //********************
| class CaClass{
|
| int idata;
|
| CaClass(){idata =1;}
| }
| //********************
| //********************
| class CanotherClass{
|
| vector <CaClass> aClass;
|
| int iotherdata;
| }
| //********************
|
| main()
| {
| CanotherClass anotherClass;
| anotherClass->aClass.push_back(CaClass); //compiles fine but this
| fails
| }
| //*********************************************************
|
| Any ideas?
| Know of any example that demo a vector used from within another class?


You have many syntax errors in your code.

Do you have an appropriate book to learn from ?

Study the following:

class CaClass
{
int idata;
public:
CaClass( int n ) : idata( n ) {}
};

class CanotherClass
{
public:
std::vector<CaClass> aClass;
};

int main()
{
CanotherClass anotherClass;
anotherClass.aClass.push_back( CaClass( 10 ) );

// Note that main() must have an return type
// specification of 'int'.

return 0;
}

Cheers.
Chris Val
 
L

Leor Zolman

Hello:

I am having trouble getting a vector of a class to work from another class

Hi,
It would help a lot if you cut-and-pasted the actual code, because this is
chock-full of syntax errors that keep it miles from compiling.

Let's add headers:

#include <iostream>
//********************
class CaClass{

int idata;
public:

CaClass(){idata =1;}
}

Missing semicolon after the }

And the most common style is to put the public first, then the private.
Note that the default in a class is /private/, so you had /everything/ be
private, in both classes, which was one of your major issues.

//********************
//********************
class CanotherClass{

vector <CaClass> aClass;

int iotherdata;
}
//********************

Another missing semi, and everything is private. Actually, making all your
data private is /good/, but you then need some public functions to provide
an interface to your clients, and your code below would have to be
modified accordingly.

int main()
{
CanotherClass anotherClass;
anotherClass->aClass.push_back(CaClass); //compiles fine but this
fails

First of all, anotherClass is not a pointer, and CaClass is not an object.
So if everything were public, you'd have to say:

anotherClass.aClass.push_back(CaClass());

To instantiate an anonymous CaClass object and add it to the vector.


Add:
return 0;
}
//*********************************************************

Any ideas? Heh.

Know of any example that demo a vector used from within another class?

Yours, once fixed.
Thank you.
You're welcome,
-leor

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
A

AngleWyrm

// Note that main() must have an return type
// specification of 'int'.

return 0;

main() is required to return an int value, but that int value defaults
to zero, and thus need not be specified. Here is a quote from Bjarne
Stroustrup, author of "The C++ Programming Language", page 46:

" The int value returned by main(), if any, is the program's return
value to "the system." If no value is returned, the system will
receive a value indicating successful completion. A nonzero value from
main() indicates failure. "

That quote is immediately followed by a hello, world example:
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
}

Thus, in the case of the requisite function main(), there is an
implicit "return 0;" at the closing brace.
 
M

Mike Wahler

AngleWyrm said:
main() is required to return an int value, but that int value defaults
to zero, and thus need not be specified.

Please read again what Chris wrote. He did *not* say that
the return statement is required. He was talking about
'main()'s return type. I.e. it must be

int main()

rather than

main()

Here is a quote from Bjarne
Stroustrup, author of "The C++ Programming Language", page 46:

" The int value returned by main(), if any, is the program's return
value to "the system." If no value is returned, the system will
receive a value indicating successful completion. A nonzero value from
main() indicates failure. "

That quote is immediately followed by a hello, world example:
#include <iostream>
int main()
{
std::cout << "Hello, world!\n";
}

Thus, in the case of the requisite function main(), there is an
implicit "return 0;" at the closing brace.

Many (including myself, and apparently Chris as well) feel that
although it's not mandatory, an explicit return is 'good style'.
YMMV.

-Mike
 
L

Leor Zolman

Many (including myself, and apparently Chris as well) feel that
although it's not mandatory, an explicit return is 'good style'.
YMMV.

Me three. That return 0 at the end is like a comment saying, "If we reach
this point, everything is under control.". OTOH, if I don't see any return
statement, my immediate thought is: "I have no idea whether or not things
have settled down in this program; Have any un-recoverable errors
happened? If so, has some reasonable action been taken? I'd better go read
through all the code to find out, because I'd hate to have it fall off the
end of main() with things in a .5-donkeyed state of some kind only to have
it return 0 by default and fake out the system..."

With the return 0 there, I can infer the programmer at least /thought/
his/her i's are dotted and t's crossed when execution reaches that point...
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
C

Chris \( Val \)

|
| | > | >
| > > // Note that main() must have an return type
| > > // specification of 'int'.
| > >
| > > return 0;
| >
| > main() is required to return an int value, but that int value defaults
| > to zero, and thus need not be specified.
|
| Please read again what Chris wrote. He did *not* say that
| the return statement is required. He was talking about
| 'main()'s return type. I.e. it must be
|
| int main()
|
| rather than
|
| main()

[snip]

Indeed - That is what I meant :).

| Many (including myself, and apparently Chris as well) feel that
| although it's not mandatory, an explicit return is 'good style'.
| YMMV.

Spot on :).

Thanks Mike.
Chris Val
 

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
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top