【发布时间】:2025-12-19 15:55:09
【问题描述】:
我必须用类和对象为学校制作一个 c++ 问题。
创建“Shaorma”类。
-会员资料:肉、大蒜、盐、胡椒;
-成员函数:隐式构造函数、参数
-构造函数,用于打印的函数
-screen,修改“garlic”成员类型的函数,
-返回肉,析构函数;
我使用 Microsoft Visual Studio 2013,但出现以下错误:
-Error 2 error LNK1120: 1 unresolved externals;
-错误 1 错误 LNK2019:函数 _main 中引用的未解析外部符号“public: __thiscall shaorma::shaorma(void)”(??0shaorma@@QAE@XZ);
#include<iostream>
#include<conio.h>
using namespace std;
class shaorma
{
int salt, peper;
char meat[40];
char garlic[3];
public:
shaorma();
shaorma(int, int, char*, char*);
~shaorma();
void print();
void setgarlic(char*);
char* getmeat();
};
shaorma::shaorma(int s, int p, char *C, char *U)
{
salt = s;
peper = p;
strcpy_s(meat, C);
strcpy_s(garlic, U);
}
shaorma::~shaorma()
{
cout << "The destructor war called.";
}
void shaorma::print()
{
cout << "Shaorma has garlic:" << garlic;
cout << "," << salt << "salt";
cout << peper << "peper";
cout << "meat type:" << meat << endl;
}
void shaorma::setgarlic(char *U)
{
strcpy_s(garlic, U);
}
char* shaorma::getmeat()
{
return meat;
}
void main()
{
shaorma S1, S2(5, 4, "yes", "lamb");
S1.print();
S2.print();
S1.setgarlic("No");
S2.getmeat();
cout << "Meat is :" << S2.getmeat();
_getch();
}
【问题讨论】:
-
默认构造函数的主体在哪里?
-
shaorma S1, S2(5, 4, "yes", "lamb");只有第二个接收这些参数。第一个是使用默认构造函数创建的。你的肉和大蒜参数是否正确?为什么不使用std::string,这样您就可以吃任何名称的肉,并将大蒜设置为bool?