【发布时间】: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。这可能只是一个文件。作为新用户,也请访问tour和How to Ask。
-
另外,标题末尾是否有
#endif? -
这不是真正的代码。真正的代码没有这些构造函数的声明。
-
请复制并粘贴您的实际代码。这是不正确的。
#endif应该在最后
标签: c++ oop constructor