未定义的引用 c++ 丢失

Undefined Reference c++ lost

#include "assert.h"; // for some reason assert wouldn't work on my compiler without this
#include <iostream>
#include <string>
#include <limits>   // This is helpful for inputting values. Otherwise, funny stuff happens


using namespace std;


class Product
{
public:

    Product();
    Product(string the_name, int the_price, int number_of);

    string return_name();
    void reduce_amount();
    void print_data() const;

private:
    string prod_name; // name of your product
    int price_in_cents; // it's price in cents
    int amount; // the number of the product that you have
};

Product::Product()
{

    prod_name = "NULL_NAME: NEED DATA";
    price_in_cents = 0;
}

Product::Product(string the_name, int the_price, int number_of)
{
    assert(the_price>0);
    assert(number_of>0);
    assert(number_of<21);
    assert(prod_name !="NULL_NAME: NEED DATA");
    prod_name = the_name;
    price_in_cents = the_price;
    amount = number_of;
}

void Product::print_data() const
{
    cout<<prod_name << endl;
    cout<<"The price in cents is: " <<price_in_cents<< endl;
    cout<< "Amount left: " << " " << amount << endl;
}

void Product::reduce_amount()
{
    amount = amount -1;
}


string Product::return_name()
{
    return prod_name;
}

class Vending_Machine
{
public:

    Vending_Machine();
    void empty_coins();
    void print_vend_stats();
    void add_product();
    Product buy_product();
private:
    int income_in_cents;

    Product product1();
    Product product2();
    Product product3();
    Product product4();
    Product product5();
};

void Vending_Machine::empty_coins()
{
    cout << "The total amount of money earned today is " << income_in_cents << " cents" << endl;
    income_in_cents = 0;
    cout << "All the coins have been withdrawn. The balance is now zero." <<     endl;
}

void Vending_Machine::print_vend_stats()
{

    cout<< "Total income thus far: " << income_in_cents << endl;

    if (product1().return_name() != "NULL_NAME: NEED DATA")
    {
        //stuff happens
    }
}

int main()
{
    return 0;
}

所以,我不确定我是否正确地完成了所有识别,但我在自动售货机 print_vend_stats() 函数中遇到布尔语句问题。它是说我正在对 product1() 进行未定义的引用。这是什么意思?

申报时

Product product1();

你声明一个成员函数,括号是使它成为一个函数的原因。

如果去掉括号

Product product1;

你声明了一个成员变量Productclass.

的实际实例

再举个例子,你不会这样写

int income_in_cents();

现在要将 income_in_cents 声明为变量吗?

无论类型是像 int 这样的原始类型,还是像 Product 这样的 class 都没有关系,成员变量的声明方式与在其他任何地方所做的普通变量一样.