4

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

带有类的体系结构 x86_64 的未定义符号

我看过类似的问题,但它们都与模板和继承的类有关,这与我的场景不同。我只是宣布一个类,没什么特别的。我认为这与没有正确定义类有关,但我不知道它是什么。这可能是我想念的愚蠢的东西,但我仍然被困住了。谢谢。

#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;
}
4

1 回答 1

3

class 没有构造函数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)
  {
  }
};
于 2012-11-22T09:19:32.297 回答