【问题标题】:I dont understand error "definition of implicity-declared 'Clothing::Clothing()'我不明白错误“隐式声明的‘Clothing::Clothing()’的定义
【发布时间】:2019-07-31 14:37:02
【问题描述】:

问题:

为什么会出现以下错误?

隐式声明的'Clothing::Clothing()的定义

上下文:

作为一项任务,我必须在 Clothing 类中执行构造函数、析构函数和方法。当我尝试在 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_ */

服装.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\clothing.cpp:7:21:错误:隐式声明'Clothing::Clothing()'的定义

\src\clothing.cpp:14:37: 错误:隐式声明的'Clothing::Clothing(const Clothing&)'的定义

【问题讨论】:

  • 为什么不在你的标题中定义它?
  • 请提取minimal reproducible example。这可能只是一个文件。作为新用户,也请访问tourHow to Ask
  • 另外,标题末尾是否有#endif
  • 这不是真正的代码。真正的代码没有这些构造函数的声明。
  • 请复制并粘贴您的实际代码。这是不正确的。 #endif 应该在最后

标签: c++ oop constructor


【解决方案1】:

错误是:你声明了一个析构函数但你没有定义它。为析构函数添加定义或将其定义为默认值:

#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_ */

修复此问题后,您的代码 sn-p 有效:tio.run

如果您的代码有更多问题,则问题不在您提供的代码 sn-p 范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2013-03-20
    相关资源
    最近更新 更多