c++ - 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;
}
解决方案
你#include "SStack.cpp"
的头文件中有一个。
不要那样做。
源文件(.cpp 文件)包括头文件。永远不要反过来。
推荐阅读
- java - 无法获取 log4j-api-java9
- eclipse - 运行 Mavenized ::Project Maven Studio 目标发布错误
- javascript - 使用角度访问全局对象
- typescript - 将大型 Typescript 项目转换为 ES6 模块
- ms-access - 单击搜索时即使数据库表中的字段为空,如何显示记录
- css - 在与复选框标签相同的行中输入复选框
- node.js - 无法连接到数据库MongoParseError: Unescaped at-sign in authority section
- java - 在文件夹之间移动创建的文件会产生奇怪的“与我共享”行为
- javascript - 如何在不使用request.post的情况下将ajax数据的所有数据导入django
- c++ - 分配二维可变大小的数组