gpt4 book ai didi

c++ - 链接器错误 : undefined reference to class: :'methods' in C++

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:55 25 4
gpt4 key购买 nike

我在编译以下源代码时遇到了麻烦[链接器错误] 未定义对“dish::dish()”的引用[链接器错误] 未定义对“dish::~dish()”的引用[链接器错误] 未定义对“dish::ShowResult()”的引用有人能帮我吗?

头文件(dish.h):

#ifndef DISH_H
#define DISH_H

class dish {
public:
dish();
dish(std::string name, std::string variety, float caloric, float price);
~dish();
static int GetN();
void SetN(int N);
static int IncrementN();
std::string GetName() const;
void SetName(std::string name);
std::string GetVariety() const;
void SetVariety(std::string variety);
float GetCaloric() const;
void SetCaloric(float caloric);
float GetPrice() const;
void SetPrice(float price);
void Enter();
void ShowResult();

private:
std::string name;
std::string variety;
float caloric;
float price;
static int N;

};

int dish::N;

#endif

和 dish.cpp:

#include <iostream>
#include <cstring>
#include "dish.h"

dish::dish()
{
dish::Enter();
}

dish::dish(std::string name, std::string variety, float caloric, float price)
{
this->name = name;
this->variety = variety;
this->caloric = caloric;
this->price = price;
}

dish::~dish()
{
}

static int dish::GetN()
{
return N;
}

void dish::SetN(int N)
{
this->N = N;
}

static int dish::IncrementN()
{
N++;
}

std::string dish::GetName() const
{
return name;
}

void dish::SetName(std::string name)
{
dish::name = name;

}

std::string dish::GetVariety() const
{
return variety;
}

void dish::SetVariety(std::string variety)
{
dish::variety = variety;
}

float dish::GetCaloric() const
{
return caloric;
}

void dish::SetCaloric(float caloric)
{
this->caloric = caloric;
}

float dish::GetPrice() const
{
return price;
}

void dish::SetPrice(float price)
{
this->price = price;
}

void dish::Enter()
{
std::cout << "\n \\*_________________________________*\\\n";
std::cout << "\n ENTER THE NAME OF DISH: ";
getline(std::cin, name);
std::cout << " ENTER THE VARIETY: ";
getline(std::cin, variety);
std::cout << " ENTER THE CALORIC CONTENT: ";
(std::cin >> caloric).get();
std::cout << " ENTER THE PRICE: ";
(std::cin >> price).get();
std::cout << "\n \\*_________________________________*\\\n";
dish::IncrementN();
}

void dish::ShowResult()
{
std::cout << "\n \\*________________________*\\\n";
std::cout << "\n THE NAME OF DISH: " << dish::GetName() << std::endl;
std::cout << " THE VARIETY: " << dish::GetVariety() << std::endl;
std::cout << " THE CALORIC CONTENT: " << dish::GetCaloric() << std::endl;
std::cout << " THE PRICE: " << dish::GetPrice() << std::endl;
std::cout << "\n \\*________________________*\\\n";
}

主要实现...

#include <cstring>
using namespace std;
#include "dish.h"


int main() {
dish a;
a.ShowResult();
return 0;
}

最佳答案

您应该将 dish.cpp 链接到您的主要可执行文件,即在您的项目中添加 dish.cpp。

关于c++ - 链接器错误 : undefined reference to class: :'methods' in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com