Я создал две структуры, которые я хочу как глобальные в своей программе, я поместил их в заголовок, и я поместил заголовок в 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;
}
}
в функции я просто хочу, чтобы иметь указатель, указывающий к глобальной структурной переменной, которая была объявлена в основном файле. Но я получать сообщение об ошибке:
«nasiayam» не был объявлен в этой области
'rice.quantity = 100;' и все следующие строки недействительны. Подобные утверждения должны выполняться внутри функций. Вне вашей функции вы можете разрешать объявления (т. Е. Заявления, которые вводят новое имя). –
Никто не знает, как назван второй файл. Это заголовок или исходный файл? – Blacktempel
@MM, что для обычных переменных, проверьте, что они говорят [здесь] (http://stackoverflow.com/questions/16054254/c-global-structure) – shuzo