【问题标题】:pointing to a global structure variable指向全局结构变量
【发布时间】:2015-12-14 06:31:20
【问题描述】:

我创建了两个我想要在我的程序中作为全局的结构,我将它们放在头文件中,并将头文件放在 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; 和以下所有行均无效。像这样的语句必须出现在函数内部。在你的函数之外,你只允许声明(即引入新名称的语句)。
  • 没有人知道第二个文件是如何命名的。是头文件还是源文件?
  • @M.M 用于普通变量检查他们在说什么here
  • @Blacktempel 函数文件是一个源文件,它有一个头文件,我也将它包含在主文件中

标签: c++ pointers scope structure


【解决方案1】:

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

extern meal nasiayam;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多