You are using a version without Ads of this website. Please, consider donating:

Basically we're taking our custom int vector class and making it into an adaptable vector class. However I'm running into these particular errors. Here's my code first:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229`` ``````// File Prologue #include #include #pragma once using namespace std; template class MyVector { // Note that you program should not have any memory leaks. private: T vSize; T vCapacity; T* vectorData; // overloaded << operator - a nonmember // designate as a friend so that it can utilize the private data members of the MyVector class friend ostream& operator<< (ostream&, const MyVector&); public: // MyVector(Default) // Purpose: A default constructor that creates an vector with a default capacity of 2 // Parameters: None // Returns: None MyVector(void); // MyVector(Parameterized) // Purpose: A parameterized constructor that creates a vector of capacity n // Parameters: integer value of n for capacity // Returns: None MyVector(const T&); // MyVector(Copy) // Purpose: A parameterized constructor that takes an object(designated as "b") and copies it to another object // Parameters: Object vector designated as "b"; Must be a constant // Returns: Copy of object "b" MyVector(const MyVector&); // MyVector(Destructor) // Purpose: A destructor that deletes any dynamically allocated storage // Parameters: None // Returns: None ~MyVector(void); // size() // Purpose: A function, size( ), that returns the size of your vector. // Parameters: None // Returns: Size of the vector T size() const; // capacity() // Purpose: A function, capacity( ), that returns the capacity of the vector. // Parameters: None // Returns: Capacity of Vector T capacity() const ; // clear() // Purpose: A function, clear( ), that deletes all of the elements from the vector and resets its size and capacity to zero. // Parameters: None // Returns: None void clear(); // push_back() // Purpose: A function push_back(int n) that adds the integer value n to the end of the vector. // Parameters: None // Returns: None void push_back(T&); // at(int n) // Purpose: locate the value of the element at position i in the vector. // Parameters: If the index i is greater than the size( ) of the vector, this function should throw an exception. // Returns: returns the value of the element at position i in the vector. T at(T&) const; // If the vector is not large enough to hold this additional value, you must make the vector grow. // Your grow algorithm should double the current capacity of the vector. // Don't forget to consider the case where the initial capacity of the vector is zero. void allocNew(); // Overload Assignment Operator() // Purpose: Overload the assignment operator // Parameters: const object that is designated as "b" // Returns: ? MyVector& MyVector::operator=(const MyVector&); }; // MyVector Class Code // Default Constructor template MyVector::MyVector(void) { vCapacity = 2; vectorData = new T [vCapacity]; vSize = 0; } // Parameterized Constructor template MyVector::MyVector(const T& n) { vCapacity = n; vectorData = new [vCapacity]; vSize = 0; } // Destructor template MyVector::~MyVector(void) { if ( vectorData != nullptr) { delete [ ] vectorData; vectorData = nullptr; } } // MyVector Copy Constructor template MyVector::MyVector(const MyVector& b) { vCapacity = b.vCapacity; vSize = b.vSize; delete [] vectorData; vectorData = new T [vSize]; for(int i = 0; i < b.vSize;i++) { vectorData[i] = b.vectorData[i]; } } // Operator Overload template MyVector::MyVector& operator=(const MyVector& b) { // Test for self-copy if(this != &b) { // If not a self-copy, implement the following vSize = b.vSize; vCapacity = b.vCapacity; delete [] vectorData; vectorData = new [vSize]; for(int i = 0; i < b.vSize;i++) { vectorData[i] = b.vectorData[i]; } } return *this; } // Size Function template T MyVector::size() const { return vSize; } // Push_Back Function template void MyVector::push_back(T& n) { // Allocate new memory space if the size of the vector is greater than the capacity of the vector if(vSize+1 > vCapacity) { allocNew(); } vectorData[vSize] = n; vSize++; } // Capacity Function template T MyVector::capacity() const { return vCapacity; } // Clear Function template void MyVector::clear() { delete []vectorData; // If vectorData is not NULL then reset size and capacity to respective values of 0 and 2. Delete []vectorData and set vectorData to new array vSize = 0; vCapacity = 2; vectorData = new [vCapacity]; } // At Function template T MyVector::at(T& n) const { if(n < vSize) { return vectorData[n]; } // Throw an exception if n is greater than vector size throw 10; } // allocNew Function template void MyVector::allocNew() { // Increase vector capacity by doubling the size of the vector by 2 vCapacity = vSize * 2; // Declare new array T* temp = new T[vCapacity]; // Copy data from old array into new array for(int i = 0; i < vSize; i++) { temp[i] = vectorData[i]; } // delete old array and have new array take the place of old array delete [] vectorData; vectorData = temp; } template ostream& operator<<(ostream& out, const MyVector& b) { for(int i = 0; i <(b.vSize); i++) { out << b.at(i) << " "; } return out; }``````

and here are the errors/warnings that visual studio 2010 is giving me:
 error C1903: unable to recover from previous error(s); stopping compilation myvector.h 144 error C2143: syntax error : missing ';' before '&' myvector.h 144 error C2350: 'MyVector::{ctor}' is not a static member myvector.h 144 IntelliSense: initial value of reference to non-const must be an lvalue driver.cpp 42 warning C4346: 'MyVector::{ctor}' : dependent name is not a type myvector.h 144

Can someone help me make sense of these errors please? Thanks in advance.
Here's the driver for reference and it's been modified for this template assignment:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````// File Prologue // -------------------------------------------------------- // DO NOT CHANGE ANY CODE PAST THIS POINT //--------------------------------------------------------- #include #include "MyVector.h" using namespace std; // the printV function // used to test the copy constructor // parameter: a MyVector object template void printV(MyVector); int main( ) { cout << "\nCreating a vector Sam of size 4."; MyVector sam( 4 ); cout << "\nPush 12 values into the vector."; for (int i = 0; i < 12; i++) sam.push_back(i); cout << "\nHere is sam: "; cout << sam; cout << "\n---------------\n"; cout << "\nCreating a vector Joe of size 4."; MyVector joe( 4 ); cout << "\nPush 6 values into the vector."; for (int i = 0; i < 6; i++) joe.push_back(i * 3); cout << "\nHere is joe: "; cout << joe; cout << "\n---------------\n"; cout << "\nTest the overloaded assignment operator \"joe = sam\": "; joe = sam; cout << "\nHere is sam: "; cout << sam; cout << "\n---------------\n"; cout << "\nHere is joe: "; cout << joe; cout << "\n---------------\n"; // pass a copy of sam by value printV(sam); cout << endl; system("PAUSE"); return 0; } void printV(MyVector v) { cout << "\n--------------------\n"; cout << "Printing a copy of a vector\n"; cout << v; }``````
Making liberal use of Google (since my class hasn't even started making linked lists yet), you need to fix lines 18, 67, 103, 134, 143, 161, 187, and maybe 84 of your header file.
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: