gpt4 book ai didi

c++ - 错误 : Undefined symbols for architecture x86_64 with classes

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:07 24 4
gpt4 key购买 nike

我是一名初学者,正在编写一个帮助我节食的有趣程序。该程序尚未完成,但在我编写时正在编译。我不断收到标题中提到的错误:

Undefined symbols for architecture x86_64 with classes

我看过类似的问题,但它们都与模板和继承类有关,这与我的情况不同。我只是在宣布一个类(class),没什么特别的。我认为这与未正确定义类有关,但我无法弄清楚它是什么。这可能是我想念的愚蠢的东西,但我仍然被困住了。谢谢。

#include <iostream>
#include <string>
using namespace std;

class Meal
{
private:
string name;
int protein;
int carbs;
int fat;
int calories;

public:
Meal(string name, int calories, int protein, int carbs, int fat);

};



int main()
{
int calories = 0;
int rest_or_lift;
int create_or_not;
cout << "Enter 1 if it is a workout day, enter 2 if it is a rest day./n";
cin >> rest_or_lift;
if (rest_or_lift == 1)
{
calories = 2554;
}
else if (rest_or_lift == 2)
{
calories = 1703;
}

cout << "Enter 1 to input existing foods, enter 2 to create new foods./n";
cin >> create_or_not;
if (create_or_not == 1)
{
cout << "This aspect has not yet been created /n"; //need to fix this part
}
else if (create_or_not == 2)
{
do
{
string name;
int protein;
int carbs;
int fat;
int calories;
cout << "Enter the name of the food./n";
cin >> name;
cout << "Enter how many calories the food has. /n";
cin >> calories;
cout << "Enter how many grams of protein the food has /n";
cin >> protein;
cout << "Enter how many grams of carbs the food has /n";
cin >> carbs;
cout << "Enter how many grams of fats the food has /n";
cin >> fat;
Meal(name, calories, protein, carbs, fat);
cout << "Enter another food? Enter 1 to exit, 2 to continue.";
cin >> create_or_not;
} while (create_or_not == 2);

}



return 0;
}

最佳答案

Meal 类没有构造函数。你可以解决它,例如像这样:

class Meal
{
private:
string m_name;
int m_protein;
int m_carbs;
int m_fat;
int m_calories;

public:
Meal(string name, int calories, int protein, int carbs, int fat)
: m_name(name), m_protein(protein),
m_carbs(carbs), m_fat(fat), m_calories(calories)
{
}
};

关于c++ - 错误 : Undefined symbols for architecture x86_64 with classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13509046/

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