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

C++ STL vector Question

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
Jul 22 '05 #1
7 1280
Drew wrote:
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.
Jul 22 '05 #2

"Drew" <pe*****@bellsouth.net> wrote in message
news:pS****************@bignews4.bellsouth.net...
| 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
Jul 22 '05 #3
On Tue, 2 Mar 2004 08:33:27 -0600, "Drew" <pe*****@bellsouth.net> wrote:
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>
#include <vector>
using namespace std; // for testing, usually bad at file scope

//********************
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.

main()
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

Drew


Leor Zolman
BD Software
le**@bdsoft.com
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
Jul 22 '05 #4
"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:c2*************@ID-110726.news.uni-berlin.de...
// 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.
Jul 22 '05 #5

"AngleWyrm" <no***************@hotmail.com> wrote in message
news:Kaf1c.27997$ko6.287431@attbi_s02...
"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:c2*************@ID-110726.news.uni-berlin.de...
// 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()

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
Jul 22 '05 #6
On Wed, 03 Mar 2004 07:20:10 GMT, "Mike Wahler" <mk******@mkwahler.net>
wrote:

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.


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

-Mike


Leor Zolman
BD Software
le**@bdsoft.com
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
Jul 22 '05 #7

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:KG******************@newsread1.news.pas.earth link.net...
|
| "AngleWyrm" <no***************@hotmail.com> wrote in message
| news:Kaf1c.27997$ko6.287431@attbi_s02...
| > "Chris ( Val )" <ch******@bigpond.com.au> wrote in message
| > news:c2*************@ID-110726.news.uni-berlin.de...
| >
| > > // 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
Jul 22 '05 #8

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

Similar topics

4
by: Jessica | last post by:
Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
12
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.