0

I am using DevC++ 5.11 as a compiler.

I am getting the error, "'nullptr' was not declared in scope" when I do believe it is declared in my MyVector.h file. Is this a compiler error or is my code incorrect? Error come in at line 45 of the MyVector.h File:

T *m_data = nullptr;

Main Programming Assignment file:

   /* Sample main
 * Disclaimer: this file is not a unit test!
 * Just because it runs, does NOT mean your assignment is correct.
 * This file is for developing your own tests.
 * We've included vector below to show you how your types should behave.
 * You can use that for debugging.
 * When implementing your list, you are expected to use the same names and declarations as defined by the STL implementation of an array list, the vector.
 * The vector you implement must be templated on the data type it stores.

In main (or multiple mains) you should write your own "unit tests" for each function;
I suggest 1 main per function.
(Multiple Main Methods)
http://www.cplusplus.com/forum/beginner/46664/

The sample input and output are defined in terms of the behavior of the std::vector (which you must test yourself).

Background reading:

General container library
http://en.cppreference.com/w/cpp/container

Vector
http://en.cppreference.com/w/cpp/container/vector
(This wiki the most “officialâ€, but still unofficial, source of C++ knowledge for end-programmers)

http://www.cplusplus.com/reference/vector/vector/
(This may also help, but if these links differ, go with en.cppreference.com instead)

Overloading operator syntax
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
*/

// USE THIS TO TEST, BUT REMOVE LATER
#include <vector>

/// Uncomment this:
#include "MyVector.h"

int main()
{
    // Uncomment this
    MyVector<int> v;
    //std::vector<int> v;

    cout << v.size() << endl;
    v.push_back(4000);
    cout << v.at(0) << endl;
    cout << v.capacity() << endl;
    v.reserve(10);
    cout << v.capacity() << endl;
    v.push_back(200);
    v.push_back(100);
    v.insert(v.begin(), 3);
    v.insert(v.end(), 20);

    // These below are tricky.
    // They look like iterators, but are really just integers.
    // We are not making you re-implement iterators themselves.
    int j = 0;

    for(auto i = v.begin(); i < v.end(); i++)
    {
        cout << v.at(j) << endl;
        j++;
    }

    j = 0;

    for(auto i = 0; i < v.size(); i++)
    {
        cout << v[j] << endl;
        j++;
    }

    return 0;

MyVector.h File:

/*
 * Simplified version of the std::vector
 * This is your h file. Do not edit it!
 * If you edit it, you risk getting a 0.
 * To see the function specifications in more detail, check en.cppreference!
 * Make sure to test your vector with multiple types: (e.g., int, char, long, etc.)
 */

#ifndef MYVECTOR_H
#define MYVECTOR_H

#include <iostream>
#include <stdexcept>
#include <string>

using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::string;

/*
* the your (the student author’s) Campus Username
*/
void get_identity(string &my_id);


// Purpose: Swaps two elements (including a vector).
// Parameters: a, b: the list elements themselves to be swapped
// Note: a and b can be any parameters, and just happen to be list this time.
// e.g., swap(list_obj[2], list_obj[1]);
// This one is similar to std::swap (you can check it's behavior).
// This is not a list function, but a general one, that swaps any elements, including array elements.
// On all assignments after this one, you can use std::swap (but not in this one).
// After this assignment (e.g., on pa05, you can use std::swap()
// https://en.cppreference.com/w/cpp/algorithm/swap
template <typename T>
void MySwap(T &a, T &b);


template <typename T>
class MyVector
{
    private:
        T *m_data = nullptr;
        int reserved_size;
        int data_size;

    public:
        // Should default array pointer to nullptr, and do all the stuff a constructor does.
        // Just a simple default constructor.
        MyVector();

        ~MyVector();

        // Make sure this does a deep copy.
        MyVector<T> & operator=(const MyVector<T> &source);

        // Make sure this does a deep copy
        MyVector(const MyVector<T> &source);

        // Returns the element of the array by reference, so it can be read or written.
        T & operator[](int i);

        // Purpose: Returns the element stored at the index by reference
        // Parameters: index - The location of the element to be returned.
        // This one should throw an std::out_of_range exception when out-of-bounds
        T & at(int index);

        // Purpose: Returns the element stored at the front of the vector
        T & front();

        // Purpose: Returns the element stored at the back of the vector
        T & back();

        // Purpose: Returns the maximum number of elements that can be stored in the current Vector
        int capacity();

        // Purpose: Ensures that there is enough room for a specific number of elements
        // Parameters: new_cap - The amount of space to be reserved in the array.
        // Postconditions: Increases max_size to new_cap if new_cap > max_size
        // Other functions should check that their operations are not going beyond the size limit,
        // and call this one if so
        // By default, you should double the size of the current array when it fills up.
        // NOTE: reserve does not do the checking of whether it's full; another function should do that, and then call reserve, which just adjusts the size to whatever it is called with, only if it is greater than current.
        // Inital size is 0, so a special case is needed to grow to 1, then 2, 4, etc., which is handled in another function (like push_back for example).
        void reserve(int new_cap);

        // If your array hits the point at which it is less than ( not <= ) 1/4 full, it should call this function.
        // It should shrink such that the array is 2x as big as the data.
        // NOTE: shrink_to_fit does not do the checking of 1/4 full; another function should do that, and then call shrink_to_fit.
        void shrink_to_fit();

        // Fills the array with count of T value (e.g., 5 a would be [a, a, a, a, a])
        // This replaces the current array.
        void assign(int count, const T &value);

        // Purpose: Clears the MyVector
        // Postconditions: current size set to 0, storage size to default (0).
        //                 elements are deallocated and data is set to nullptr
        void clear();

        // Purpose: appends the value x to the end of an MyVector
        // Parameters: x is value to be added to MyVector
        // Postconditions: current size is incremented by 1
        //     If max size is reached, the storage array is grown.
        void push_back(const T &x);

        // Removes the last element of the vector
        void pop_back();

        // Purpose: inserts the value x at the position i 
        //     (before the value currently at i) in the  MyVector
        // Parameters: x is value to be added to MyVector
        //             i is the position to insert x at
        // Postconditions: current size is incremented by 1
        //     If max size is reached, the storage array is grown.
        void insert(int i, const T &x);

        // Purpose: Removes the element at index i in the array
        // Parameters: i, the index of the element to remove
        // Postconditions: if the size of the list is greater than 0
        //     size is decremented by one.
        //     if the size of the list less than 1/4 the max size,
        //     the storage array is shrunk.
        void erase(int i);

        // Returns the size of the actual data stored in the array list
        // Remember, with indexing at 0, this is 1 more than the last elements position index
        int size();

        // This is designed to partially mimic the behavior of iterators.
        int begin()
        {
            return 0;
        }

        int end()
        {
            return size();
        }
};


// We're giving you this one.
// This relies on you having implemented the .at() and .size() member functions first.
// It may be a good idea to get those working, so you can test with this.
template <typename T>
std::ostream & operator<<(std::ostream &out, MyVector<T> &my_list)
{
    out << "[ ";

    for(int i = 0; i < my_list.size(); i++)
    {
        out << my_list.at(i) << ", ";
    }

    out << "]";

    return out;
}


#include "MyVector.hpp"

#endif
tbertra1
  • 1
  • 2
  • 2
    What version of C++ are you compiling this with? – Carcigenicate Mar 02 '20 at 02:44
  • 2
    `nullptr` is a keyword starting from C++11. Looks like you are using a very old compiler that doesn't recognize it. – Igor Tandetnik Mar 02 '20 at 02:44
  • Please add what compiler (including version) you are compiling with and how you are compiling (e.g. what compiler options are you passing). You probably need to either update to a more recent compiler or add the `-std=c++11`/`-std=c++0x` flag (for older GCC and Clang versions). – walnut Mar 02 '20 at 02:48
  • DevC++ 5.11 compiler, TDM-GCC 4.9.2 64-bit Release – tbertra1 Mar 02 '20 at 02:50
  • 1
    Does this answer your question? [How to change mode from c++98 mode in Dev-C++ to a mode that supports C++0x (range based for)?](https://stackoverflow.com/questions/16951376/how-to-change-mode-from-c98-mode-in-dev-c-to-a-mode-that-supports-c0x-ran) and [Does Dev-Cpp 5.11 support C++ 11?](https://stackoverflow.com/questions/50220237/does-dev-cpp-5-11-support-c-11) and [How can I compile C++11 code with Orwell Dev-C++?](https://stackoverflow.com/questions/13613295/how-can-i-compile-c11-code-with-orwell-dev-c) Also, side note: "DevC++" is the IDE, "GCC" is the compiler. – walnut Mar 02 '20 at 03:02
  • Solved! Changed compiler settings to GNU C++11. Thank you :) – tbertra1 Mar 02 '20 at 03:23

0 Answers0