Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parking Lot with Alley Classes Using Eclipse

16 views
Skip to first unread message

Denise James

unread,
Jul 15, 2016, 11:51:39 AM7/15/16
to
Hello, I am getting an error in Eclipse, error: expected initializer before 'CAlley', along with warnings such as "will be initialized after [-Wreorder]"

Below is the code: (I get an errror on " CAlley AlleyA, AlleyB; // Create two classes of alley" line)

#include <iostream>
#include <iomanip> // to use setw object#include <iostream>
#include <cctype>

#define MAXSIZE 5
using namespace std; // to use cout and cin objects

class CarNode {
public:
CarNode() : m_pNext(0), m_ticketNum(0) { };
~CarNode();
CarNode(CarNode &):m_pNext(0), m_ticketNum(0) { };

// assign next pointer
void SetNext(CarNode* p){m_pNext=p;}
// assign ticket number
void SetTicketNum(int tN){m_ticketNum=tN;}
// get the next pointer
CarNode *GetNext(void){return(m_pNext);}
// get the ticket number
int GetTicketNum(void){return(m_ticketNum);}
private:
CarNode *m_pNext; // pointer to next node in stack
int m_ticketNum; // ticket number of car
};

class CAlley {
public:
CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZE) { }
~CAlley () {}
CAlley (CAlley &):m_pTop(0), mSize(0), mMaxSize(MAXSIZE) { }

int Park(int); // park a car
void Retrieve(int userTicketNum, CAlley *pB); // retrieve a car
// void Terminate(); // quit the program
void Display(char *); // display contents af alley
void SetTop(CarNode *p){m_pTop=p;} // assign top pointer
// check if stack is empty
bool Empty(){return ((mSize==0) ? true : false);}
// check if stack is full
bool Full() {return ((mSize==MAXSIZE) ? true : false);}
int Push(CarNode *); // push one node onto top of stack
CarNode * Pop(); // pop one node from the top of stack
CarNode *m_pTop; // pointer to top of alley (stack)
private:
int mSize; // number of nodes in alley (stack)
int mMaxSize; //max number of nodes in alley (stack)
};
////////////////////////////////////////////////////////////////
// Function: CAlley::Push
// Purpose: Add a new node to top of stack
// Parameters:
// CarNode * pNewNode- the node to be added to top of stack
// Local Variables:
// status - return 1 if pushed sucessfully
// - return 0 if stack was full
////////////////////////////////////////////////////////////////

int CAlley::Push(CarNode* pNewNode)// type integer function
{
if(Full()){
cout << "PARKING LOT IS FULL";
return 0;
}
else{
m_pTop = pNewNode; // make the top node equal to passed
(*pNewNode).SetNext(m_pTop); // Create the next node
m_pTop = pNewNode; // Assign the next node to the top
return 1;
}
}

/////////////////////////////////////////////////////////////////
// Function: CAlley::Pop
// Purpose: Remove a node to top of alley (stack).
// Parameters:
// CarNode * pNewNode- returns the node removed from top of alley
// is zero if stack is empty
// Local Variables:
// status - return 1 if pushed successfully
// - return 0 if stack was full
/////////////////////////////////////////////////////////////////
CarNode *CAlley::Pop() // type CarNode function
{
if(Empty()){
cout<<"PARKING LOT IS EMPTY";
}
else{
CarNode *pNewNode;
pNewNode = m_pTop;
m_pTop = (*pNewNode).GetNext();
delete m_pTop;
}
}
///////////////////////////////////////////////////////////////
// Function: CAlley::Park ( )
// Purpose: Park a car, if lot is not full. First allocate a
// node, then add it to the top of the stack
// Parameters:
// userTicketNum - the ticket number for the node to be added
// Local Variables:
// CarNode *pNewNode - local pointer to newly allocated node
// int status - 1 if parked sucessfully (lot not full)
// 0 if not parked (lot was full)
///////////////////////////////////////////////////////////////
int CAlley::Park(int userTicketNum)
{
if (Full())
cout << "PARKING LOT FULL" << endl;
else{
userTicketNum++;
cout << "Here is your Ticket no. " << userTicketNum << endl;
CarNode NewNode; // create a node to hold car
NewNode.m_ticketNum = userTicketNum;
NewNode.m_pNext = nullptr; // the next node is empty
Push(&NewNode);
}
}

///////////////////////////////////////////////////////////////
// Function: CAlley:: Retrieve ( int userTicketNum, CAlley *pB)
// Purpose: Retrieve a car from alley A. Search for the car/node
// based on ticket num. by driving a car (popping off top) out of
// A and driving (pushing onto top) into B.
// If the car is found, it is not pushed onto B, but rather,
// it is deleted. Then the cars in B are moved back into A.
//
// Parameters:
// userTicketNum - the ticket number for the node to be added
// pB - pointer to CAlley B
//
// Local Variables:
// CarNode *pCurr - local pointer used as index
// int found - 1 if car is found, 0 if not found
///////////////////////////////////////////////////////////////

void CAlley::Retrieve(int userTicketNum, CAlley * pB)
{
// This makes function to be called at Alley A location
int topTicket;
bool found = false;
CarNode* pHoldNode; // create a node to hold the node examined
while(!Empty() && !found)//while alley A is not empty
// and ticket not found yet
{
pHoldNode = Pop();
topTicket = (*pHoldNode).GetTicketNum();// access the ticket number
if (topTicket == userTicketNum) // if matches exit while loop
found = true;
else
(*pB).Push(pHoldNode);
}// end of while
if (!found)
cout << "CAR NOT PARKED IN MY LOT\n";
while (!(*pB).Empty())
{
pHoldNode = (*pB).Pop();
Push(pHoldNode);
}//end of while
}

void CAlley::Display(char *)
{
cout << "Alley A: ";
CarNode * pCurr = m_pTop;
while (pCurr != nullptr)
{
cout << pCurr->GetTicketNum();
if(pCurr->GetNext()!=nullptr)
cout << " ";
pCurr=pCurr->GetNext();
}
cout << endl;
}
void Terminate()
{
}

int main()
{
int ticketStub = 123; //ticket stub is the pointer
char input ; //User inputs whether to Park, Display or Retrieve Car(s) //Determines row of matrix
CAlley AlleyA, AlleyB; // Create two classes of alley
cout << "Enter: " << endl;
cout << "D to Display the location of the cars" << endl;
cout << "P to Park a car " << endl;
cout << "R to Retreive a car" << endl;
cout << "Q to Quit" << endl;

// create five node linked list or stack

while (cin >> input)
{
// If input = Q or q, then exit program.
if ((input == 'q') || (input == 'Q'))
break;
// Validity check for input: If not equal either D,P,R,Q,d,p,r,q
// then wait for the next input
if ((input != 'D') || (input != 'P') || (input != 'R')
|| (input != 'd') || (input != 'p') || (input != 'r'))
continue;
else
{
switch(input)
{
case 'P':
AlleyA.Park(ticketStub);
break;
case 'R':
int inputTicketStub;
cout << "Type in the ticket number: ";
cin >> inputTicketStub;
AlleyA.Retrieve(inputTicketStub, &AlleyB);
break;
case 'D':
AlleyA.Display();
break;
default:
;
}//end of switch
}// end of else
}//end of while input
cout << endl << "Adios amigo!" << endl;
}// end of main

Scott Lurndal

unread,
Jul 15, 2016, 2:20:38 PM7/15/16
to
Denise James <deni...@gmail.com> writes:
>Hello, I am getting an error in Eclipse, error: expected initializer before 'CAlley', along with warnings such as "will be initialized after [-Wreorder]"
>
>Below is the code: (I get an errror on " CAlley AlleyA, AlleyB; // Create two classes of alley" line)

You should probably take this to your Instructor or their T.A.

In the future, post the exact output from the compiler.

Alf P. Steinbach

unread,
Jul 15, 2016, 5:50:13 PM7/15/16
to
On 15.07.2016 17:51, Denise James wrote:
> Hello, I am getting an error in Eclipse, error: expected initializer
> before 'CAlley', along with warnings such as "will be initialized
> after [-Wreorder]"
>

Here are the errors reported by Visual C++ 2015:

> original.cpp(114): error C2248: 'CarNode::m_ticketNum': cannot access private member declared in class 'CarNode'
> original.cpp(24): note: see declaration of 'CarNode::m_ticketNum'
> original.cpp(8): note: see declaration of 'CarNode'
> original.cpp(115): error C2248: 'CarNode::m_pNext': cannot access private member declared in class 'CarNode'
> original.cpp(23): note: see declaration of 'CarNode::m_pNext'
> original.cpp(8): note: see declaration of 'CarNode'
> original.cpp(216): error C2660: 'CAlley::Display': function does not take 0 arguments

I used the following default options, in environment variable CL:

> CL=/nologo /EHsc /GR /W4 /FI "iso646.h"

Here are the errors reported by MinGW g++ 5.1.0:

> original.cpp: In member function 'CarNode* CAlley::Pop()':
> original.cpp:94:1: warning: no return statement in function returning non-void [-Wreturn-type]
> }
> ^
> original.cpp: In member function 'int CAlley::Park(int)':
> original.cpp:24:6: error: 'int CarNode::m_ticketNum' is private
> int m_ticketNum; // ticket number of car
> ^
> original.cpp:114:19: error: within this context
> NewNode.m_ticketNum = userTicketNum;
> ^
> original.cpp:23:11: error: 'CarNode* CarNode::m_pNext' is private
> CarNode *m_pNext; // pointer to next node in stack
> ^
> original.cpp:115:19: error: within this context
> NewNode.m_pNext = nullptr; // the next node is empty
> ^
> original.cpp:118:1: warning: no return statement in function returning non-void [-Wreturn-type]
> }
> ^
> original.cpp: In function 'int main()':
> original.cpp:216:28: error: no matching function for call to 'CAlley::Display()'
> AlleyA.Display();
> ^
> original.cpp:162:6: note: candidate: void CAlley::Display(char*)
> void CAlley::Display(char *)

I used the following default options, in command alias g++ (via doskey):

> g++=g++ -std=c++14 -pedantic-errors -Wall -Wextra -fexec-charset=cp1252 $*

Mostly the options tell each compiler to be a bit more
standard-conforming than it is by default, and to produce more warnings,
please.

It's generally a good idea to use at least two compilers. When one
compiler's error messages are incomprehensible, the other compiler might
be able to shed some light on the problem. Also, it helps to produce
more portable code.


Cheers & hth.,

- Alf

0 new messages