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;
}

First: Its never a good idea to include a c/cpp file.

This is better:-

say
file.h is the header. The corresponding C++ file will be file.cpp

So, you include the header file and the C++ file is included in the project.

The files are as follows:

1)main.cpp

#include <iostream>
#include <string>
#include "st.h"
using namespace std;

int main() {

	string mystring;
	mystring = "ABC$CBA";
	stringcomp(mystring);

	return 0;
}

2)st.h

#ifndef STH
#define STH

#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

/////////////////////////////////////////////////////
void stringcomp(string str);// Prototype added here.
////////////////////////////////////////////////////

// sorry for making it shabby. just wanted to draw your attention to that :)

#endif

3)st.cpp

#include <iostream>
#include <string>
#include "st.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

try putting void stringcomp(string str); right before "int main()".
Also I hope you got a "stringrec.h" that has the prototype of the functions defined in "stringrec.cpp"

Thanks guys. I made changes to reflect what you guys said and it appears that I did something incorrectly either with that or somewhere else, as it now tells me "error: 'stringcomp' was not declared in this scope" at the call inside the main function.

Looks like I've found the issues! Thanks again, I would have been really lost without you guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.