0

I have to modify the myqueue and mystack templates and make a new class called myerror in order to make
the main.cpp work, im getting countless errors tho in the main.cpp
Here are my errors

main.cpp: In function ‘int main()’:
main.cpp:52: error: no matching function for call to ‘MyStack<double>::MyStack(int)’
mystack.h:21: note: candidates are: MyStack<T>::MyStack() [with T = double]
mystack.h:5: note:                 MyStack<double>::MyStack(const MyStack<double>&)
main.cpp:53: error: no matching function for call to ‘MyStack<char>::MyStack(int)’
mystack.h:21: note: candidates are: MyStack<T>::MyStack() [with T = char]
mystack.h:5: note:                 MyStack<char>::MyStack(const MyStack<char>&)
main.cpp:54: error: no matching function for call to ‘MyQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::MyQueue(int)’
myqueue.h:37: note: candidates are: MyQueue<T>::MyQueue() [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
myqueue.h:20: note:                 MyQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::MyQueue(const MyQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)
main.cpp:114: error: no matching function for call to ‘MyStack<int>::MyStack(int)’
mystack.h:21: note: candidates are: MyStack<T>::MyStack() [with T = int]
mystack.h:5: note:                 MyStack<int>::MyStack(const MyStack<int>&)
main.cpp:115: error: no matching function for call to ‘MyQueue<int>::MyQueue(int)’
myqueue.h:37: note: candidates are: MyQueue<T>::MyQueue() [with T = int]
myqueue.h:20: note:                 MyQueue<int>::MyQueue(const MyQueue<int>&)
main.cpp:122: error: expected type-specifier before ‘MyError’
main.cpp:122: error: expected `)' before ‘&’ token
main.cpp:122: error: expected `{' before ‘&’ token
main.cpp:122: error: ‘e’ was not declared in this scope
main.cpp:122: error: expected `;' before ‘)’ token
main.cpp:132: error: expected type-specifier before ‘MyError’
main.cpp:132: error: expected `)' before ‘&’ token
main.cpp:132: error: expected `{' before ‘&’ token
main.cpp:132: error: expected `;' before ‘)’ token
main.cpp:142: error: expected type-specifier before ‘MyError’
main.cpp:142: error: expected `)' before ‘&’ token
main.cpp:142: error: expected `{' before ‘&’ token
main.cpp:142: error: expected `;' before ‘)’ token
main.cpp:152: error: expected type-specifier before ‘MyError’
main.cpp:152: error: expected `)' before ‘&’ token
main.cpp:152: error: expected `{' before ‘&’ token
main.cpp:152: error: expected `;' before ‘)’ token
myerror.cpp: In member function ‘int myError::what()’:
myerror.cpp:14: error: expected type-specifier before ‘)’ token
#include <iostream>
#include <vector>
#include <string>
using namespace std;


#ifndef MY_QUEUE_H 
#define MY_QUEUE_H 

#include<iostream> 
#include<string> 
using namespace std; 




template<typename T> 
class MyQueue 
{ 
public: 
    MyQueue(); 
    T pop_front(); 
    void push_back(T item); 
    bool isEmpty() const; 
    void print() const; 
	
private:
	
    T queue[100]; 
    int front; 
    int back; 
	
};//End  
//Create an empty queue 
template<typename T> 
MyQueue<T>::MyQueue() 
{ 
    front = 0; 
    back = 0; 
} 
//remove and return the first item in the queue 
template<typename T> 
T MyQueue<T>:: pop_front() 
{ 
    T temp = queue[front]; 
    front++; 
    front = front % 100; 
    return temp; 
} 
//add item to the end of queue 
template<typename T> 
void MyQueue<T>::push_back(T item) 
{ 
	
    queue[back] = item; 
    back++; 
    back = back % 100; 
	
} 
//returns true if the queue is empty 
template<typename T> 
bool MyQueue<T>::isEmpty() const 
{ 
    if(front==back) 
		return true; 
    else 
		return false; 
	
} 
//print put the elements of the queue from front to back speratied by one space 
template<typename T> 
void MyQueue<T>::print()const 
{ 
    for(int i = front; i != back; i = (i + 1) % 100) 
		cout << queue[i]<<endl; 
	
}//end print() 
#endif
template <typename T>
class MyStack
{
public:
    MyStack();
	T pop(); 
    void push(T item);
    bool isEmpty() const;
private:
	int top;
	T* items;
    int MAX;
	T data[100];
	
	
};

template <typename T>
MyStack<T>::MyStack()
{
	int size=100;
	MAX=size;
	top=-1;
	items=new T[MAX];
	throw "out of memory";
	
}
template <typename T>
T MyStack<T>::pop()

{ 
    if(top > 0) 
	{ 
		throw "";
        T item = data[--top]; 
        return item; 
	} 
    else 
        top--; 
}
template <typename T>
void MyStack<T>::push(T item)
{
	int size=100; 
    if(top <= size - 1) 
	{ 
		throw "out of memory";
        data[top++] = item; 
	} 
}

template <typename T>
bool MyStack<T>::isEmpty() const
{ 
    return top == -1;
}
#include "myerror.h"

myError::myError(int errors)
{
	errors=errs;
}
int myError::what()
{
	try {
		int what;
	}
	catch () {
	throw "";	
	}
}


#ifndef MYERROR_H
#define MYERROR_H

#include <iostream>
#include <vector>
#include <string>
using namespace std;


class myError
{
	public:
	myError(int errs=0);
	int what();
	
private:
	int errs;
};
#endif

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

#include "mystack.h"
#include "myqueue.h"
#include "myerror.h"

#define INFILE "input.txt"

static void add(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 + op2);
}

static void subtract(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 - op2);
}

static void multiply(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 * op2);
}

static void divide(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 / op2);
}

int main()
{
	ifstream inFile;
	inFile.open(INFILE);
	if(!inFile.is_open())
	{
		cout << "\nCould not open file " << INFILE << endl;
		cout << "Place file " << INFILE << " in same directory as a.out\n" << endl;
		return 1;
	}
	
	MyStack<double> stack(100);
	MyStack<char> charStack(100);
	MyQueue<string> input(100);
	
	cout << "Input is: ";
	string token;
	inFile >> token;
	
	while(token != "EOF")
	{
		cout << token << " ";
		input.push_back(token);
		for(int ii = 0; ii < token.length(); ++ii) charStack.push(token[ii]);
		inFile >> token;
	}
	
	inFile.close();
	cout << endl;
	
	cout << "Contents of input queue: ";
	input.print();
	
	while(!input.isEmpty())
	{
		token = input.pop_front();
		if(token == "+")
		{
			add(stack);
		}
		else if(token == "-")
		{
			subtract(stack);
		}
		else if(token == "*")
		{
			multiply(stack);
		}
		else if(token == "/")
		{
			divide(stack);
		}
		else
		{
			const char *n = token.data();
		    char *endPtr = NULL;
			double value = strtod(n, &endPtr);
		    stack.push(value);
		}
	} // end while reading input queue
	
	double result = stack.pop();
	
	cout << "Result is " << result << endl;
	
	cout << "Input reversed is: ";
	while(!charStack.isEmpty())
	{
		cout << charStack.pop() << " ";
	}
	cout << endl;
	
	// Now cause some exceptions
	MyStack<int> s1(4);
	MyQueue<int> q1(5);
	
	try
	{
		s1.pop();
		cout << "Empty stack pop not reported" << endl;
	}
	catch(MyError &e)
	{
		cout << "Error thrown: " << e.what() << endl;
	}
	
	try
	{
		q1.pop_front();
		cout << "Empty queue pop not reported" << endl;
	}
	catch(MyError &e)
	{
		cout << "Error thrown: " << e.what() << endl;
	}
	
	try
	{
		for(int ii = 0; ii < 100; ++ii) s1.push(ii);
		cout << "Full stack push not reported" << endl;
	}
	catch(MyError &e)
	{
		cout << "Error thrown: " << e.what() << endl;
	}
	
	try
	{
		for(int ii = 0; ii < 100; ++ii) q1.push_back(ii);
		cout << "Full queue push not reported" << endl;
	}
	catch(MyError &e)
	{
		cout << "Error thrown: " << e.what() << endl;
	}
	return 0;
	
} // end main()
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by jonsca
0

The first few errors are due to the fact that you don't have a constructor that takes an argument.
In addition to what you have:
You need a constructor that takes an int argument.
(in your class declaration): MyStack(int sz); and defined as:

MyStack<T>::MyStack(int sz)
{	
    top=-1;
    items=new T[sz];	//and whatever else you deem fit to put in
}

For the last few errors note that myError is not the same as MyError .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.