0

I have an error where "function already has a body" for my constructors and member functions when I have not repeated any of the bodies. The error code is C2084:

void Func(int);
void Func(int) {}   // define function
void Func(int) {}   // C2084 second definition

I have not created duplicate functions similar to what is shown on the visual studios error page. here is the code below if anybody knows a solution to this error I would greatly appreciate it.

Here is the Stack.h:

//CONTENTS: Declares Class SStack, with data members, contructors and member function prototypes
//If you want, you can make minor changes to this header file

#ifndef _StackClass_
#define  _StackClass_



#include <cstdlib>
#include <string>
#include <iostream>


using namespace std;

class SStack
{

        
        public:
                // Constructor
                SStack( int cap);
                // Copy Constructor
                SStack( const SStack& s );
                ~SStack( ); //destructor
                
        // The member function push: Precondition:  the stack is not full.
                void push ( const std::string s);
                
        // The member function pop: Precondition:  the stack is not empty.
                void pop ();

        // The member function top: Precondition:  the stack is not empty.
                string top () const;
                
                bool IsEmpty () const;

        //printing all the elements in the stack
        void print() const;

        int size() const;

        int getCapacity() const;
        

        private:
                int capacity; // Capacity is the maximum number of items that a stack can hold
                std::string* DynamicStack; 
                int used; // How many items are stored in the stack
};


        
#include "SStack.cpp"
#endif


Here is the SStack.cpp:

#include <iostream>
#include "SStack.h"

SStack::SStack(int cap)
{
    DynamicStack = new string[cap];
    this->capacity = cap;
    this->used = -1;
}

SStack::SStack(const SStack& s)
{
    capacity = s.capacity;
    DynamicStack = new string[capacity];
    used = s.used;

    for (int i = 0; i < used; i++) {
        DynamicStack[i] = s.DynamicStack[i];
    }
}

SStack::~SStack()
{

}

void SStack::push(const std::string s)
{
    if (used >= capacity - 1) {
        cout << "Stack overflow" << endl;
    }
    else {
        this->used++;
        DynamicStack[used] = s;
        cout << s << "pushed onto the stack" << endl;
    }
}

void SStack::pop()
{
    if (used < 0) {
        cout << "stack underflow" << endl;
    }
    else {
        string s = DynamicStack[used];
        this->used--;
    }
}

string SStack::top() const
{
    if (used < 0) {
        cout << "stack is empty" << endl;
        return 0;
    }
    else {
        string s = DynamicStack[used];
        return s;
    }
}

bool SStack::IsEmpty() const
{
    if (used < 0) {
        return true;
    }
    else {
        return false;
    }
}

void SStack::print() const
{
    for (int i = used; i >= 0; i--) {
        cout << DynamicStack[used] << endl;
    }
}

int SStack::size() const
{
    return used;
}

int SStack::getCapacity() const
{
    return capacity;
}


  • 2
    The `#include "SStack.cpp"` in the header file is giving you problems, I'm sure. When it compiles `SStack.cpp`, it'll be seeing two copies of the contents of SStack.cpp. – Nathan Pierson Oct 13 '20 at 19:25
  • @Nathan's hit the nail on the head. You should almost never be including `.cpp` files. What was that intended to do? – scohe001 Oct 13 '20 at 19:26
  • My professor told me to include the #include "SStack.cpp" at the bottom of my header file. When I remove the include i am met with the LNK2005 error which I had before i added the #include "SStack.cpp". I do not know how to resolve this error either. – sam schafer Oct 13 '20 at 19:33

1 Answers1

3

You have an #include "SStack.cpp" in your header file.

Don't do that.

Source files (.cpp files) include header files. Never the other way around.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Thank you for your response, my professor told me to include the "SStack.cpp" at the bottom of my header file. When i remove the #include ""SStack.cpp" from the header then i am met with the LNK2005 error message. I am not sure how to resolve that either. – sam schafer Oct 13 '20 at 19:30
  • How are you compiling it? – hookenz Oct 13 '20 at 19:32
  • It compiles but does not build. – sam schafer Oct 13 '20 at 19:44
  • @samschafer I don't know what OS and compiler you are using, but if it was on Unix you would compile it like `c++ -std=c++11 -W -Wall SStack.cpp main.cpp -o SStack` – Zan Lynx Oct 14 '20 at 15:39
  • @samschafer I noticed that your SStack.cpp has no main function so I assume you also have some file maybe named main.cpp. – Zan Lynx Oct 14 '20 at 15:40