954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Function calling problems

I'm writing up a program for a class I'm taking where I need to make a linked list stack & queue. Right now I'm not quite done, but I'm having problems calling the functions from the .h file.

What's happening is in the while loop, I'm trying to get data loaded from a file to load the specific function, then send the numbers after it from that file into the function. VS 2010 is telling me that the identifier for each of the four functions is undefined. Do I just need to add int before each of them? I'm thinking I wouldn't because that would be declaring it right? which I've already done.

Here's an example of the data that's going to be loaded from a file:
push 10
push 35
push 50
append 100
pop
push 15
pop
serve
serve
append 200


It has been a little while since I've programmed so it may just be a few basic thing that I've forgotten, but if I could get help on this it'd be great. ^.^

Here's my .cpp

#include "LinkedLists.h"
using namespace std;


void main()
{
	Stack();
	Queue();
	char reload = 'y';
	cout<<"Student: Matthew Law"<<endl<<"Professor: Reza Sanati Mehrizy"<<endl<<"CS 2420"<<endl<<"Project 1"<<endl;
	while(reload=='y'||reload=='Y')
	{
		string function;
		string File;

		ifstream fLocation;
		cout<<endl<<"What is the file you would like to load?  ";
		cin>> File;
		fLocation.open(File);
		while(fLocation.good())
		{
			cin>>"function";
			int num = 0;
			if(function=="push"||function=="Push")
			{
				cin>>num;
				push(num);
			}
			else if(function=="pop"||function=="Pop")
			{
				pop();
			}
			else if(function=="append"||function=="Append")
			{
				cin>>num;
				append(num);
			}
			else if(function=="serve"||function=="Serve")
			{
				serve();
			}
			else
				cout<<"\nAn incorrect function was defined in the file.\n";

		}
		cout<<"Would you like to run the program again? y/n: ";
		cin>>reload;
	}
}

and my .h

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <streambuf>

enum Error_code
{
	success,
	overflow,
	underflow
};
#define NodeEntry int
#define Stack_entry int
#define Queue_entry int

struct Node
{
	NodeEntry entry;
	Node *next;

	Node();
	Node(NodeEntry item, Node* addOn);

};

class Stack
{
public:
	Stack();
	bool empty() const;
	Error_code push(const Stack_entry &item);
	Error_code pop();
	~Stack();
protected:
	Node *top_node;

};

class Queue
{
public:
	Queue();
	Error_code append(const Queue_entry &item);
	Error_code serve();
	~Queue();

protected:
	Node *front_node, *back_node;

};

//-----------------Node----------------

Node::Node()
{
	next = NULL;
}

Node::Node(NodeEntry item, Node* addOn)
{
	entry = item;
	next = addOn;
}

//-----------------Stack---------------
Error_code Stack::push(const Stack_entry &item)
{
	Node *new_top = new Node(item, top_node);
	if (new_top == NULL)
		return overflow;
	top_node = new_top;
	return success;
}

Error_code Stack::pop()
{
	Node *old_top = top_node;
	if (top_node == NULL)
		return underflow;
	top_node = old_top->next;
	delete old_top;
	return success;
}



//-----------------Queue-------------

Error_code Queue::append(const Queue_entry &item)
{

}

Error_code Queue::serve()
{

}


also if you happen to notice any glaring errors, please let me know. ^^ Thanks~

coolboym99
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

void main is a glaring error.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

ok, what would be better to put there then? =/

coolboym99
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
cin>>"function";			
int num = 0;


what do you mean by this ==>cin>>"function";

muze
Junior Poster in Training
62 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

oops, the "" aren't supposed to be there. But does that work for getting the data from the file, or is a getline required?

coolboym99
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

main() should have return type int.

In main() you want to read the file with the ifstream you declared, not with cin. Therefore replace cin with the ifstream in lines 26-35.

In addition push, pop and serve are member functions of a class and can only be called using an object of the desired class so someplace you are going to have to declare objects of the appropriate class(es) to call the appropriate methods/functions.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

First thing first
1) cin>>function is the right way to use, taking input in some variable
2) This cant be used to get data from the file.
3) As pointed out by Lerner, most of the functions are member functions and you did not call them using appropriate objects.

muze
Junior Poster in Training
62 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

how would I appropriately call them using objects then? I don't remember how to do so.

coolboym99
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
how would I appropriately call them using objects then? I don't remember how to do so.

You need a good book to start with. Try to go directly on OOP basics, it will most probably be 6th or 7th chapter of any random book.

Lets suppose there is a class of name Shape, further suppose it has a function names Square like this.

class Shape{
public:
      Shape();  //constructor
      Square();
}


Now in the main(), you call it by the object of Shape like this

Shape sh;
sh.Square();

This is how you use an object to call its member function. If this is not a copied assignment, leave this code and refer to your book as soon as possible.

muze
Junior Poster in Training
62 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

Ok, Thank you, and I'll make sure to go through my book more. ^^

coolboym99
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: