指向全局结构变量

pointing to a global structure variable

我在我的程序中创建了两个我想要作为全局的结构,我把它们放在 header 中,我把 header 放在 main.cpp 和文件源中使用这些结构的函数。

我在我的主文件中声明了一些具有这些结构类型的全局变量,我初始化了所有这些,这样我就没有任何问题了。

结构文件:

#ifndef STRUCT_HPP_INCLUDED
#define STRUCT_HPP_INCLUDED

struct item {
int quantity = 0;
};

struct meal {

item rice;
item chicken;
item onion;
item orange;
item apple;
item ice;
item sugar;
double price;

};



#endif // STRUCT_HPP_INCLUDED

全局变量声明和初始化:

#include "struct.hpp"

using namespace std;

item rice,chicken,onion,orange,apple,ice,sugar;

meal nasiayam,kabsa,nasikun,appleju,orangeju,apploreng,kueh,ricapl;
meal* order(NULL);

//item initialization
rice.quantity = 100;
chicken.quantity = 70;
onion.quantity = 500;
orange.quantity = 500;
apple.quantity = 500;
ice.quantity = 1000;
sugar.quantity = 1000;
//end


//meal initialization
nasiayam.rice.quantity = 1;
nasiayam.chicken.quantity = 1;
nasiayam.onion.quantity = 0;
nasiayam.orange.quantity = 0;
nasiayam.apple.quantity = 0;
nasiayam.ice.quantity = 0;
nasiayam.sugar.quantity = 0;
nasiayam.price = 25;

kabsa.rice.quantity = 1;
kabsa.chicken.quantity = 1;
kabsa.onion.quantity = 1;
kabsa.orange.quantity = 0;
kabsa.apple.quantity = 0;
kabsa.ice.quantity = 0;
kabsa.sugar.quantity = 0;
kabsa.price = 35;

nasikun.rice.quantity = 1;
nasikun.chicken.quantity = 1;
nasikun.onion.quantity = 1;
nasikun.orange.quantity = 0;
nasikun.apple.quantity = 0;
nasikun.ice.quantity = 0;
nasikun.sugar.quantity = 0;
nasikun.price = 20;

appleju.rice.quantity = 0;
appleju.chicken.quantity = 0;
appleju.onion.quantity = 0;
appleju.orange.quantity = 0;
appleju.apple.quantity = 2;
appleju.ice.quantity = 1;
appleju.sugar.quantity = 1;
appleju.price = 10;

orangeju.rice.quantity = 0;
orangeju.chicken.quantity = 0;
orangeju.onion.quantity = 0;
orangeju.orange.quantity = 2;
orangeju.apple.quantity = 0;
orangeju.ice.quantity = 1;
orangeju.sugar.quantity = 1;
orangeju.price = 10;

apploreng.rice.quantity = 0;
apploreng.chicken.quantity = 0;
apploreng.onion.quantity = 0;
apploreng.orange.quantity = 2;
apploreng.apple.quantity = 0;
apploreng.ice.quantity = 1;
apploreng.sugar.quantity = 1;
apploreng.price = 10;

kueh.rice.quantity = 1;
kueh.chicken.quantity = 0;
kueh.onion.quantity = 0;
kueh.orange.quantity = 0;
kueh.apple.quantity = 0;
kueh.ice.quantity = 0;
kueh.sugar.quantity = 1;
kueh.price = 15;

ricapl.rice.quantity = 1;
ricapl.chicken.quantity = 0;
ricapl.onion.quantity = 0;
ricapl.orange.quantity = 0;
ricapl.apple.quantity = 1;
ricapl.ice.quantity = 0;
ricapl.sugar.quantity = 1;
ricapl.price = 25;
//end

使用结构

的函数
#include <iostream>
#include "checktyping.hpp"
#include "getorder.hpp"
#include "struct.hpp"


void getorder (meal *order)
{
    int choice(0);

    std::cout << std::endl << std::endl
         << "\t\t              {{{{{{{{{{}}}}}}}}}" << std::endl
         << "\t\t   {{{{{{{{      today's menu   }}}}}}}" << std::endl
         << "\t\t              {{{{{{{{{{}}}}}}}}}" << std::endl;

    std::cout << std::endl << std::endl
         << "\t\t   ========== main dishes =========" << std::endl << std::endl
         << "1)Nasi Ayam" << std::endl
         << "2)Kabsa" << std::endl
         << "3)Nasi Kunyit" << std::endl;
    std::cout << std::endl << std::endl
         << "\t\t   ========== juices =========" << std::endl << std::endl
         << "4)orange juice" << std::endl
         << "5)apple juice" << std::endl
         << "6)mixed apple-orange" << std::endl;
    std::cout << std::endl << std::endl
         << "\t\t   ========== desserts =========" << std::endl << std::endl
         << "7)Kueh Melaka" << std::endl
         << "8)rice apple pie" << std::endl;
    std::cout << std::endl << std::endl
         << "please enter your choice (1-8): ";

         switch (choice)
         {
         case 1:
             order = &nasiayam;//problem here!!
            break;
         case 2:
             order = &kabsa;//and problem here!!
            break;
         case 3:
             order = &nasikun;//and problem here!!
            break;
         case 4:
             order = &orangeju;//and problem here!!
            break;
         case 5:
             order = &appleju;//and problem here!!
            break;
         case 6:
             order = &apploreng;//and problem here!!
            break;
         case 7:
             order = &kueh;//and problem here!!
            break;
         case 8:
             order = &ricapl;//and problem here!!
            break;

         }




}

在函数中我只想有一个指针指向在主函数中声明的全局结构变量file.but我收到错误:

'nasiayam' 未在此范围内声明

您必须在第二个源文件中声明 'nasiayam'(和其他全局变量)具有外部链接,其中定义了 getorder 函数。 例如:

extern meal nasiayam;