CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Data Structures

  1. #1
    Join Date
    Oct 2011
    Posts
    5

    Data Structures

    I'm a little stuck on my programming. I'm going to post the errors that I keep getting followed by code. If somebody can help me figure out what I'm doing wrong, that would be great.

    quack.cpp

    #include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
    #include "quack.h"

    using namespace std;


    // change the value of this variable to be your own name instead of "I. Forgot"
    const char Quack::YOUR_NAME[] = "I. Forgot";

    Quack::Quack(int capacity)
    {
    items = new item[capacity];
    backPtr = new item;
    frontPtr = new item;
    midPtr = new item;
    current = new item;

    maxSize = capacity;
    back = maxSize-1;
    count = 0;
    top = -1;
    }

    Quack::~Quack(void)
    {
    delete frontPtr;
    delete backPtr;
    delete current;
    delete midPtr;
    delete [] items; //Heap Corruption Debug Error at the end of program.

    items = NULL;
    maxSize = 0;
    back = 0;
    }

    bool Quack:ushFront(const int n)
    {
    int i = 0;

    if ( count == maxSize ) // Then we cant add to it n e more.
    {
    throw runtime_error( "Stack is Full" );// Full Stack
    return false;
    }
    backPtr->n = items[back-1].n;
    while ( i < count ) // Loop less than however many we counted.
    {
    if ( i == top+1 )
    {
    current->n = items[top+1].n;
    items[top+1].n = backPtr->n;
    }

    midPtr->n = items[++i].n;
    items[i].n = current->n;

    if ( i != back-1 )
    {
    current->n = items[++i].n;
    items[i].n = midPtr->n;
    }
    }
    ++count;
    items[top+1].n = n;
    return true;
    //return false;
    }

    bool Quack:ushBack(const int n)
    {
    items[count].n = n;
    count++;
    return true;
    //return false;
    }

    bool Quack:opFront(int& n)
    {
    n = items[top+1].n;
    for ( int i = 0; i < count; i++ )
    {
    items[i] = items[i+1];
    }
    count--; // Remove top element.
    return true;
    //return false;
    }

    bool Quack:opBack(int& n)
    {
    n = items[--count].n;
    return true;
    //return false;
    }

    void Quack::rotate(int r)
    {
    int i = 0;

    while ( r > 0 ) // rotate postively.
    {
    frontPtr->n = items[top+1].n;
    for ( int i = 0; i < back; i++ )
    {
    items[i] = items[i+1];
    }
    items[back-1].n = frontPtr->n;
    r--;
    }
    while ( r < 0 ) // rotate negatively.
    {
    if ( i == top+1 )
    {
    backPtr->n = items[back-1].n;
    current->n = items[top+1].n;
    items[top+1].n = backPtr->n;
    }
    midPtr->n = items[++i].n;
    items[i].n = current->n;

    if ( i == back-1 )
    {
    items[back-1].n = current->n;
    i = 0;
    r++; continue;
    }
    else
    {
    current->n = items[++i].n;
    items[i].n = midPtr->n;
    if ( i == back-1 )
    {
    i = 0;
    r++; continue;
    }
    }
    }
    }

    void Quack::reverse(void)
    {
    int j = 0; // Variable declaration/initialization.

    frontPtr->n = items[top+1].n;
    backPtr->n = items[back-1].n;

    for ( int i = 0; i < count / 2; i++ )
    {
    items[j].n = items[i].n;
    items[i].n = items[ count - i-1 ].n;
    items[ count - i-1 ].n = items->n;
    }

    items[top+1].n = backPtr->n;
    items[back-1].n = frontPtr->n;
    }

    int Quack::itemCount(void)
    {
    return count;
    }

    ostream& operator<<(ostream& out, Quack *q)
    {
    if ( q.count == 0 ) // No elements have been counted.
    out << endl << "quack: empty" << endl;
    else
    {
    out << endl << "quack: ";
    for ( int i = 0; i <
    q.count; i++ )
    {
    if ( i < q.count-1 )
    out << q.items[i].n << ", ";
    else out << q.items[i].n;
    }
    out << endl << endl;
    }
    return out;
    }


    quack.h

    #pragma once

    #include <ostream>

    class Quack
    {
    public:
    static const char YOUR_NAME[]; // used for printing out programmer's name

    Quack(int capacity);
    ~Quack(void);
    bool pushFront(const int n); // push an item onto the front
    bool pushBack(const int n); // push an item onto the back
    bool popFront(int& n); // pop an item off the front
    bool popBack(int& n); // pop an item off the back
    void rotate(int r); // "rotate" the stored items (see note below)
    void reverse(void); // reverse the order of the stored items
    int itemCount(void); // return the current number of stored items

    private:
    int items; // pointer to storage for the circular array.
    // Each item in the array is an int.

    int maxSize; // is for the size of the item stack
    int back; // is for the back or "bottom" of the stack
    int count; // to count the items added to the stack
    int top;

    struct item // Definition of each item stored by the quack.
    {
    int n;

    };

    //item *items; // Pointer to storage for the circular array.
    item *backPtr;
    item *frontPtr;
    item *midPtr;
    item *current; // Each item in the array is an int.
    item q;
    public:
    friend std:stream& operator<<(std:stream& out, Quack *q);
    };

    lab2driver.cpp
    #include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
    #include <iostream>
    #include "quack.h"

    using namespace std;

    const static int QUACK_SIZE = 7;
    static Quack *quack;

    static void push(Quack *quack, bool front, int n)
    {
    bool ok;

    if (front)
    ok = quack->pushFront(n);
    else
    ok = quack->pushBack(n);
    cout << ">>> push" << (front ? "Front " : "Back ") << n << (ok ? " succeeded" : " failed") << endl;
    }

    static void pop(Quack *quack, bool front)
    {
    bool ok;
    int n;

    if (front)
    ok = quack->popFront(n);
    else
    ok = quack->popBack(n);
    cout << ">>> pop" << (front ? "Front " : "Back ");
    if (ok)
    cout << "succeeded: " << n;
    else
    cout << "failed";
    cout << endl;
    }

    int main(int argc, char **argv)
    {
    cout << "CS260 - Lab2 - " << Quack::YOUR_NAME << endl << endl;

    quack = new Quack(QUACK_SIZE);
    cout << quack;
    push(quack, true, 1);
    push(quack, true, 2);
    push(quack, true, 3);
    push(quack, true, 4);
    push(quack, false, 0);
    push(quack, true, 9);
    cout << quack;
    cout << "--- # of items: " << quack->itemCount() << endl << endl;
    pop(quack, true);
    cout << quack;
    pop(quack, true);
    cout << quack;
    push(quack, false, 7);
    cout << quack;
    push(quack, false, 8);
    cout << quack;
    cout << ">>> rotate(2)" << endl;
    quack->rotate(2);
    cout << quack;
    cout << ">>> rotate(-3)" << endl;
    quack->rotate(-3);
    cout << quack;
    cout << ">>> reverse" << endl;
    quack->reverse();
    cout << quack;
    push(quack, true, 6);
    cout << quack;
    cout << ">>> rotate(3)" << endl;
    quack->rotate(3);
    cout << quack;
    cout << ">>> rotate(-4)" << endl;
    quack->rotate(-4);
    cout << quack;
    cout << "--- # of items: " << quack->itemCount() << endl << endl;
    while (quack->itemCount() > 0) {
    pop(quack, false);
    cout << quack;
    }
    cout << "--- # of items: " << quack->itemCount() << endl << endl;

    delete quack;

    // report on memory leaks in the Output Window
    #ifdef _DEBUG
    if (argc == 2) {
    _CrtSetReportMode( _CRT_WARN , _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_WARN , _CRTDBG_FILE_STDERR );
    }
    _CrtDumpMemoryLeaks();
    #endif

    return 0;
    }

    memoryleakdetect.cpp

    #pragma once // include this .h file file only once

    // enable Visual C++ memory leak checking
    #ifdef _DEBUG
    #include <ostream>
    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif

    Here are my errors:
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(13 ): error C2440: '=' : cannot convert from 'Quack::item *' to 'int'
    There is no context in which this conversion is possible
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(31 ): error C2541: 'delete' : cannot delete objects that are not pointers
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(47 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(47 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(52 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(52 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(53 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(53 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(56 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(56 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(57 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(57 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(61 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(61 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(62 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(62 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(66 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(66 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(73 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(73 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(81 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(81 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(84 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(84 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(93 ): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(93 ): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(10 4): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(10 4): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(10 7): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(10 7): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(10 9): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(10 9): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(11 6): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(11 6): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(11 7): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(11 7): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(11 8): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(11 8): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(12 0): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(12 0): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(12 1): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(12 1): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(12 5): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(12 5): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(13 1): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(13 1): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(13 2): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(13 2): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(14 6): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(14 6): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(14 7): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(14 7): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 1): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 1): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 1): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 1): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 2): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 2): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 2): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 2): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 3): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 3): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 3): error C2227: left of '->n' must point to class/struct/union/generic type
    type is 'int'
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 6): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 6): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 7): error C2109: subscript requires array or pointer type
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(15 7): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(16 7): error C2228: left of '.count' must have class/struct/union
    type is 'Quack *'
    did you intend to use '->' instead?
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(17 2): error C2228: left of '.count' must have class/struct/union
    type is 'Quack *'
    did you intend to use '->' instead?
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(17 4): error C2228: left of '.count' must have class/struct/union
    type is 'Quack *'
    did you intend to use '->' instead?
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(17 5): error C2228: left of '.items' must have class/struct/union
    type is 'Quack *'
    did you intend to use '->' instead?
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(17 5): error C2228: left of '.n' must have class/struct/union
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(17 6): error C2228: left of '.items' must have class/struct/union
    type is 'Quack *'
    did you intend to use '->' instead?
    c:\users\brian williams\desktop\260\lab2\quake\quake\quack.cpp(17 6): error C2228: left of '.n' must have class/struct/union

    If you can help me with my problem, I would appreciate it.

  2. #2
    Join Date
    Oct 2011
    Posts
    5

    Post Re: Data Structures

    This is how the code is supposed to look. Sorry about how I posted the code. Oh, and anywhere there is a smiley face with the tongue out, it's supposed to be : p (but together) and just the smiley face, it's supposed to be : 0 (but together)

    quack.cpp

    #include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
    #include "quack.h"

    using namespace std;


    // change the value of this variable to be your own name instead of "I. Forgot"
    const char Quack::YOUR_NAME[] = "I. Forgot";

    Quack::Quack(int capacity)
    {
    items = new item[capacity];
    backPtr = new item;
    frontPtr = new item;
    midPtr = new item;
    current = new item;

    maxSize = capacity;
    back = maxSize-1;
    count = 0;
    top = -1;
    }

    Quack::~Quack(void)
    {
    delete frontPtr;
    delete backPtr;
    delete current;
    delete midPtr;
    delete [] items; //Heap Corruption Debug Error at the end of program.

    items = NULL;
    maxSize = 0;
    back = 0;
    }

    bool Quack:ushFront(const int n)
    {
    int i = 0;

    if ( count == maxSize ) // Then we cant add to it n e more.
    {
    throw runtime_error( "Stack is Full" );// Full Stack
    return false;
    }
    backPtr->n = items[back-1].n;
    while ( i < count ) // Loop less than however many we counted.
    {
    if ( i == top+1 )
    {
    current->n = items[top+1].n;
    items[top+1].n = backPtr->n;
    }

    midPtr->n = items[++i].n;
    items[i].n = current->n;

    if ( i != back-1 )
    {
    current->n = items[++i].n;
    items[i].n = midPtr->n;
    }
    }
    ++count;
    items[top+1].n = n;
    return true;
    //return false;
    }

    bool Quack:ushBack(const int n)
    {
    items[count].n = n;
    count++;
    return true;
    //return false;
    }

    bool Quack:opFront(int& n)
    {
    n = items[top+1].n;
    for ( int i = 0; i < count; i++ )
    {
    items[i] = items[i+1];
    }
    count--; // Remove top element.
    return true;
    //return false;
    }

    bool Quack:opBack(int& n)
    {
    n = items[--count].n;
    return true;
    //return false;
    }

    void Quack::rotate(int r)
    {
    int i = 0;

    while ( r > 0 ) // rotate postively.
    {
    frontPtr->n = items[top+1].n;
    for ( int i = 0; i < back; i++ )
    {
    items[i] = items[i+1];
    }
    items[back-1].n = frontPtr->n;
    r--;
    }
    while ( r < 0 ) // rotate negatively.
    {
    if ( i == top+1 )
    {
    backPtr->n = items[back-1].n;
    current->n = items[top+1].n;
    items[top+1].n = backPtr->n;
    }
    midPtr->n = items[++i].n;
    items[i].n = current->n;

    if ( i == back-1 )
    {
    items[back-1].n = current->n;
    i = 0;
    r++; continue;
    }
    else
    {
    current->n = items[++i].n;
    items[i].n = midPtr->n;
    if ( i == back-1 )
    {
    i = 0;
    r++; continue;
    }
    }
    }
    }

    void Quack::reverse(void)
    {
    int j = 0; // Variable declaration/initialization.

    frontPtr->n = items[top+1].n;
    backPtr->n = items[back-1].n;

    for ( int i = 0; i < count / 2; i++ )
    {
    items[j].n = items[i].n;
    items[i].n = items[ count - i-1 ].n;
    items[ count - i-1 ].n = items->n;
    }

    items[top+1].n = backPtr->n;
    items[back-1].n = frontPtr->n;
    }

    int Quack::itemCount(void)
    {
    return count;
    }

    ostream& operator<<(ostream& out, Quack *q)
    {
    if ( q.count == 0 ) // No elements have been counted.
    out << endl << "quack: empty" << endl;
    else
    {
    out << endl << "quack: ";
    for ( int i = 0; i <
    q.count; i++ )
    {
    if ( i < q.count-1 )
    out << q.items[i].n << ", ";
    else out << q.items[i].n;
    }
    out << endl << endl;
    }
    return out;
    }


    quack.h

    #pragma once

    #include <ostream>

    class Quack
    {
    public:
    static const char YOUR_NAME[]; // used for printing out programmer's name

    Quack(int capacity);
    ~Quack(void);
    bool pushFront(const int n); // push an item onto the front
    bool pushBack(const int n); // push an item onto the back
    bool popFront(int& n); // pop an item off the front
    bool popBack(int& n); // pop an item off the back
    void rotate(int r); // "rotate" the stored items (see note below)
    void reverse(void); // reverse the order of the stored items
    int itemCount(void); // return the current number of stored items

    private:
    int items; // pointer to storage for the circular array.
    // Each item in the array is an int.

    int maxSize; // is for the size of the item stack
    int back; // is for the back or "bottom" of the stack
    int count; // to count the items added to the stack
    int top;

    struct item // Definition of each item stored by the quack.
    {
    int n;

    };

    //item *items; // Pointer to storage for the circular array.
    item *backPtr;
    item *frontPtr;
    item *midPtr;
    item *current; // Each item in the array is an int.
    item q;
    public:
    friend std:stream& operator<<(std:stream& out, Quack *q);
    };

    lab2driver.cpp

    #include "memoryleakdetect.h" // this must be the first #include in each of your .cpp files
    #include <iostream>
    #include "quack.h"

    using namespace std;

    const static int QUACK_SIZE = 7;
    static Quack *quack;

    static void push(Quack *quack, bool front, int n)
    {
    bool ok;

    if (front)
    ok = quack->pushFront(n);
    else
    ok = quack->pushBack(n);
    cout << ">>> push" << (front ? "Front " : "Back ") << n << (ok ? " succeeded" : " failed") << endl;
    }

    static void pop(Quack *quack, bool front)
    {
    bool ok;
    int n;

    if (front)
    ok = quack->popFront(n);
    else
    ok = quack->popBack(n);
    cout << ">>> pop" << (front ? "Front " : "Back ");
    if (ok)
    cout << "succeeded: " << n;
    else
    cout << "failed";
    cout << endl;
    }

    int main(int argc, char **argv)
    {
    cout << "CS260 - Lab2 - " << Quack::YOUR_NAME << endl << endl;

    quack = new Quack(QUACK_SIZE);
    cout << quack;
    push(quack, true, 1);
    push(quack, true, 2);
    push(quack, true, 3);
    push(quack, true, 4);
    push(quack, false, 0);
    push(quack, true, 9);
    cout << quack;
    cout << "--- # of items: " << quack->itemCount() << endl << endl;
    pop(quack, true);
    cout << quack;
    pop(quack, true);
    cout << quack;
    push(quack, false, 7);
    cout << quack;
    push(quack, false, 8);
    cout << quack;
    cout << ">>> rotate(2)" << endl;
    quack->rotate(2);
    cout << quack;
    cout << ">>> rotate(-3)" << endl;
    quack->rotate(-3);
    cout << quack;
    cout << ">>> reverse" << endl;
    quack->reverse();
    cout << quack;
    push(quack, true, 6);
    cout << quack;
    cout << ">>> rotate(3)" << endl;
    quack->rotate(3);
    cout << quack;
    cout << ">>> rotate(-4)" << endl;
    quack->rotate(-4);
    cout << quack;
    cout << "--- # of items: " << quack->itemCount() << endl << endl;
    while (quack->itemCount() > 0) {
    pop(quack, false);
    cout << quack;
    }
    cout << "--- # of items: " << quack->itemCount() << endl << endl;

    delete quack;

    // report on memory leaks in the Output Window
    #ifdef _DEBUG
    if (argc == 2) {
    _CrtSetReportMode( _CRT_WARN , _CRTDBG_MODE_FILE );
    _CrtSetReportFile( _CRT_WARN , _CRTDBG_FILE_STDERR );
    }
    _CrtDumpMemoryLeaks();
    #endif

    return 0;
    }
    memoryleakdetect.cpp

    #pragma once // include this .h file file only once

    // enable Visual C++ memory leak checking
    #ifdef _DEBUG
    #include <ostream>
    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define new DEBUG_NEW
    #endif

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Data Structures

    Without quack.h we point you to your exact errors, but it's safe to assume the compiler is right.

  4. #4
    Join Date
    Oct 2011
    Posts
    97

    Re: Data Structures

    Quote Originally Posted by bwill21706 View Post
    I'm a little stuck on my programming. I'm going to post the errors that I keep getting followed by code. If somebody can help me figure out what I'm doing wrong, that would be great.

    --snip--

    If you can help me with my problem, I would appreciate it.
    First of all, you should really use code tags. And you can't just post code and errors and expect us to go through and fix them. Looking at your errors, you simply don't know how to use pointers. I suggest reading through this tutorial.

    http://www.cplusplus.com/doc/tutorial/pointers/

    Pointers are exactly what they sound like, they point to memory. They hold nothing more than an address to real data. So you can't treat pointers like you would a normal variable, or you will get the errors you have. I suggest reading that tutorial and when you're done, you'll understand what the difference between . and ->, and what the * and & operators do.

  5. #5
    Join Date
    Oct 2011
    Posts
    5

    Re: Data Structures

    Thanks for that Access. So with the pointers, would I initialize them in my .h file?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Data Structures

    Quote Originally Posted by bwill21706 View Post
    Thanks for that Access. So with the pointers, would I initialize them in my .h file?
    So you wrote all of that code without understanding how to use pointers correctly or how to debug such a program? Did you test each function to make sure it does its job, or did you write the code all at once without testing anything, and then only test at the end?

    I'll add by saying you need to take a step back and write a very simple program using pointers so that you are familiar with their usage.
    Code:
    #include <iosteam>
    
    using namespace std;
    
    int main()
    {
        int x = 10;
        int *px;  // px declared, but not initialized
    
        // do not do this, else your program may crash
        // cout << *px;
    
        // Now we will initialize the pointer to point to the "x" integer
        px = &x;  // now px is initialized.  It points to x.
        cout << *px;  // this should print 10
    }
    If you do not understand the code above, then trying to figure out how to use pointers with such a mass of code you wrote isn't going to help you understand pointers any faster or easier.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 30th, 2011 at 10:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured