2015-12-14 92 views
0

我创建了两个结构,我想我的程序全局在我把它们放在一个头文件中,并将头文件放在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 <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; 

     } 




} 

的功能,我只是想有一个指针指向

#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 

功能到主文件中声明的全局结构变量。但是我是收到错误:

“nasiayam”在此范围内未声明

+1

'rice.quantity = 100;'以下所有行都是无效的。像这样的语句必须发生在函数内部。在你的函数之外,你只允许声明(即引入一个新名字的声明)。 –

+0

没有人知道第二个文件是如何命名的。它是头文件还是源文件? – Blacktempel

+0

@MM这是正常变量检查他们在说什么[这里](http://stackoverflow.com/questions/16054254/c-global-structure) – shuzo

回答

1

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

extern meal nasiayam; 
+0

谢谢@Ruslan它为我工作 – shuzo

+0

@shuzo欢迎您:) –

相关问题