I am trying to use push_back() with a vector of type Section--i.e., vector<Section> sections. I am trying to use push_back() to add a copy from the copy constructor into the copied version. Here is my constructor:
1 2 3 4 5 6 7
Faculty::Faculty (const Faculty& f)
: theName( f.theName )
{
for (int i = 0; i < f.sections.size(); ++i) {
f.sections.push_back ( sections.theName );
}
}
What am I doing wrong here? I am getting the error: "No matching member function call to 'push_back'. I don't understand why push_back() is giving me trouble, because I was able to successfully use other vector-specific functions such as size().
sections.size() works, but sections.push_back() and sections.reserve() do not.
Are you saying try sections.push_back ( f.theName ); ?
I tried it, but that doesn't work. Basically, all this is trying to do is say, "if there is room, add a new section into the copy--i.e., f. I'm not sure if I should be doing f.sections.push_back or just sections.push_back. Any ideas?
Note that if Faculty has no other members, this copy constructor is equivalent to the one generated by default by the compiler if you don't define it explicitly.
Thanks for the suggestion, Helios. sections.push_back ( f.sections[i] ) fixed the compilation error. Now, however, I'm getting a segmentation fault, and I'm not sure where my code is running into a problem. Any ideas? Here is my faculty.h:
std::vector::max_size() returns the maximum size the vector could ever possibly reach due to system limitations, not the capacity. std::vector::capacity() returns the maximum number of elements the vector can hold in its current internal array, before performing a reallocation.
1 2 3 4 5 6 7 8 9
//By definition, this condition is always true, no matter what.
assert ( sections.size() < sections.max_size() );
//...
//Remove this code. std::vector does this automatically:
if ( sections.size() == sections.max_size () ) {
sections.reserve ( 2 * sections.max_size() + 1 );
}
I realized that between the original comment and when you responded, so I took it out. However, I still have a segmentation fault 11. Perhaps it has to do with the
1 2 3
sections[sections.size()] = s;
for ( vector<Section>::size_type i = 0; i < sections.size(); ++i ) {
sections.push_back ( s );
Is it correct to be using push_back() on s?
The new addSection function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Faculty::addSection( Section s )
{
//assert ( sections.size() < sections.max_size() );
sections[ sections.size() ]= s;
//for each section, push_back the new section
for ( vector<Section>::size_type i = 0; i < sections.size(); ++i ) {
sections.push_back ( s );
}
//If the sections are full, reserve twice the max_size.
//Copy the new, reserved size into size()
//if ( sections.size() == sections.max_size () ) {
//sections.reserve ( 2 * sections.max_size() + 1 );
//}
}
So, you're saying change the addSection function to:
1 2 3 4
void Faculty::addSection( Section s ) {
assert ( sections.size() < sections.max_size() );
sections.push_back ( s );
}
?
Unfortunately, the segmentation fault is still present. Could this loop, in both the copy constructor and the assignment operator be a problem for the same reason?
1 2
for ( int i = 0; i < f.sections.max_size(); ++i )
sections.push_back ( f.sections[i] );