指向全局结构变量

时间:2015-12-14 06:31:20

标签: c++ pointers scope structure

我在程序中创建了两个我希望作为全局的结构,我将它们放在标题中,并将标题放在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;

         }




}

在函数中我只想指向指向主文件中声明的全局结构变量的指针。但是我收到错误:

&#39; nasiayam&#39;未在此范围内声明

1 个答案:

答案 0 :(得分:1)

您必须在第二个源文件中声明'nasiayam'(以及其他全局变量)具有外部链接,其中定义了public class Alarm extends AppCompatActivity { TextView _item, _title; HashMap<String,List<String>> _itemDisplay; List<String> _list; ExpandableListView _expLV; expLVAdapter _expAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm); //Adding font to notification title--------------------------------------------------------- _title = (TextView) findViewById(R.id.textViewNotificationTitle); Typeface amaticTF = Typeface.createFromAsset(getAssets(), "Amatic-Bold.ttf"); _title.setTypeface(amaticTF); // End of notification title---------------------------------------------------------------- _expLV = (ExpandableListView) findViewById(R.id.expListView); ArrayList<String> _displayItem = getIntent().getStringArrayListExtra("Text1"); _itemDisplay = getInfo(_displayItem); _list = new ArrayList<String>(_itemDisplay.keySet()); _expAdapter = new expLVAdapter(this,_itemDisplay,_list); _expLV.setAdapter(_expAdapter); } //Adding values to the expandable list view public static HashMap<String,List<String>> getInfo(ArrayList<String> _displayItem){ HashMap<String, List<String>> _itemList = new HashMap<String,List<String>>(); int items = _displayItem.size(); for(int i = 0; i < items; i++) { String _itemName = _displayItem.get(i); List<String> itemName = new ArrayList<>(); itemName.add("some text"); _itemList.put(_itemName,itemName); } return _itemList; }} 函数。 例如:

getorder