首页 > 解决方案 > C++ 函数已经有一个主体

问题描述

当我没有重复任何主体时,我的构造函数和成员函数出现“函数已经有主体”的错误。错误代码为 C2084:

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

我没有创建类似于视觉工作室错误页面上显示的重复功能。如果有人知道此错误的解决方案,这是下面的代码,我将不胜感激。

这是 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


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


标签: c++

解决方案


#include "SStack.cpp"的头文件中有一个。

不要那样做。

源文件(.cpp 文件)包括头文件。永远不要反过来。


推荐阅读