Hey Everyone!
So I'm a super beginner trying to teach myself the ins and outs of stacks (ever so slowly). Specifically, I'm trying to work out a programming challenge in which the objective is to write a function to tell if a string in the format of word1$word2 satisfies the condition of word2 being the reverse of word 1.
I believe that I have a decent enough stack class (I haven't implemented the exceptions properly yet, but that shouldn't be an issue with the test case I'm trying currently), as well as a function to perform the operations on the strings. I'm including both of these files in a main.cpp file.
The issue I am having is that, when I got to compile this program, I am hit with an error saying "multiple definition of 'stringcomp(std::string)'". Seeing as how I only declare the function stringcomp(string) in the file where I define it, I don't see how this is possible.
Does anyone have any input as to what I did wrong in this case? (files included below) I am a beginner, so I'm sorry if it seems painfully obvious or stupid.
stackA.h:
#include <string>
using namespace std;
typedef char stacktype;
class Stack{
private:
int top;
int size;
stacktype *arr;
public:
Stack(int size) {
if (size <= 0) {
throw string("Invalid stack size.");
}
arr = new stacktype[size];
top = -1;
}//end copy constructor
void push(stacktype value) {
if(top==size) {
throw string("Stack storage overflow");
}
top++;
arr[top]=value;
}//end push
stacktype peek() {
if(top == -1) {
throw string("Stack empty.");
}
return arr[top];
}//end peek
void pop() {
if(top == -1) {
throw string("Stack empty");
}
top--;
}//end pop
bool isEmpty() {
return (top == -1);
}//end isEmpty
int getTop() {
return top;
}//end getTop
~Stack() {
delete[] arr;
}//end destructor
};//end stack class
stringrec.cpp (contains the stringcomp() function)
#include <iostream>
#include <string>
#include "stackA.h"
using namespace std;
void stringcomp(string str) {
int i;
bool inlang;//tells if the string is in the language
Stack mystack(str.size());
while(str[i] != '$')
{
mystack.push(str[i]);
i++;
}//end while loop
//increment i to skip $
i++;
//match reverse of w as stipulated
inlang = true;
while(inlang && (i<str.size()))
{
// try
// {
mystack.pop();
if(mystack.getTop() == str[i])
{
i++;
}
else {
inlang = false;
}
//}//end try
/* catch StackException
{
inlang=false;
}//end catch
*/
}//end while
if(inlang && mystack.isEmpty()) {
cout << "The string is in the language." << endl;
}
else {
cout << "The string is not in the language." << endl;
}
}//end function
main.cpp:
#include <iostream>
#include <string>
#include "stringrec.cpp"
using namespace std;
int main() {
string mystring;
mystring = "ABC$CBA";
stringcomp(mystring);
return 0;
}