我不明白错误"隐含声明的定义'Clothing::Clothing()'

I dont understand error "definition of implicity-declared 'Clothing::Clothing()'

本文关键字:Clothing 定义 错误 明白 声明      更新时间:2023-10-16

问题:

为什么会发生以下错误?

暗示'服装的定义:: clotsing()

上下文:

作为一项任务,我必须在班级服装中做构造函数,攻击者和方法。当我尝试在 clothing.cpp 中定义构造函数时,我有问题。我读到问题是因为我没有在 clothing.h 中声明构造函数,但我认为我是怎么做到的。我不知道问题所在。

我的代码:

服装。H:

#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
    int gender;
    int size;
    string name;
public:
    Clothing();
    Clothing(const Clothing &t);
    Clothing(int gender, int size, string name);
    ~Clothing();
    int getGender();
    int getSize();
    string getName();
    void setGender(int gender1);
    void setSize(int size1);
    void setName(string name1);
    void print();
    void toString();
};
#endif /* CLOTHING_H_ */

clothing.cpp:

#include <iostream>
#include "clothing.h"
#include <string>
#include <sstream>
using namespace std;
Clothing::Clothing() :
        gender(1), 
        size(1), 
        name("outofstock") {
}
Clothing::Clothing(const Clothing& t) :
        gender(t.gender), 
        size(t.size), 
        name(t.name) {
}
Clothing::Clothing(int gender, int size, string name) {
}
int Clothing::getGender() {
    return gender;
}
int Clothing::getSize() {
    return size;
}
string Clothing::getName() {
    return name;
}
void Clothing::setGender(int gender1) {
    gender = gender1;
}
void Clothing::setSize(int size1) {
    size = size1;
}
void Clothing::setName(string name1) {
    name = name1;
}
void Clothing::print() {
    cout << name << "  " << gender << "  " << size << endl;
}
void Clothing::toString() {
    stringstream ss;
    ss << name << "  " << gender << "  " << size;
    cout << ss.str();
}

错误: src clotsing.cpp:7:21:错误:隐式宣布的定义'clotsing :: clotsing()'

src clothing.cpp:14:37:错误:隐式宣布的'clotsing :: Clotsing(Const Clothing&amp;)'

错误是:您声明了驱动器,但没有定义它。添加destructor的定义或将其定义为默认值:

#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
    int gender;
    int size;
    string name;
public:
    Clothing();
    Clothing(const Clothing &t);
    Clothing(int gender, int size, string name);
    ~Clothing() = default; // <-- add a default destructor
    int getGender();
    int getSize();
    string getName();
    void setGender(int gender1);
    void setSize(int size1);
    void setName(string name1);
    void print();
    void toString();
};
#endif /* CLOTHING_H_ */

修复此操作后,您的代码片段工作:tio.run

如果您的代码有更多问题,则问题不在提供的代码段。