【问题标题】:c++ stucts cant be accessed outside the if statementc++ 结构不能在 if 语句之外访问
【发布时间】:2015-07-02 19:57:13
【问题描述】:

目标是让用户为两部电影输入一些电影信息。
然后两个函数输出电影数据
问题是当我尝试使用 if 语句之外的函数时,它们不能再被访问了。

#include <iostream>
#include <string>
using namespace std;

struct MovieData // the stuct
{
    string Title,  // required varibles
        Director;
    int yearReleased,
        runTime;

    MovieData(string TITLE, string  DIRECTOR, int YearReleased, int RunTime)  // constructors 
    {
        Title = TITLE;
        Director = DIRECTOR;
        yearReleased = YearReleased;
        runTime = RunTime;
    }
    string getTitle() // getters
    { return Title; }
    string getDirector()
    { return Director; }
    int getYear()
    { return yearReleased; }
    int getLength()
    { return runTime; }
};

void printMovie1(MovieData movieprint1) // fucntion that outputs movie 1
{
    cout << "Title of movie " << movieprint1.getTitle() << endl;
    cout << "Director is " << movieprint1.getDirector()<< endl;
    cout << "Movie release year " << movieprint1.getYear()<< endl;
    cout << "Movie length " << movieprint1.getLength() << endl;
}

void printMovie2(MovieData movieprint2) // function that outputs movie 2
{
    cout << "Title of movie " << movieprint2.getTitle() << endl;
    cout << "Director is " << movieprint2.getDirector() << endl;
    cout << "Movie release year " << movieprint2.getYear() << endl;
    cout << "Movie length " << movieprint2.getLength() << endl;
}

int main()
{
    string Title, // local varibles
        Director;
    int yearReleased,
        runTime;

    for (int i = 0; i < 2; i++) // forward loop to enter movie info
    {
        cout << "Please enter the movie " << (i + 1) << " title ";
        getline(cin, Title);

        cout << "Now the director ";
        getline(cin, Director);

        cout << "What year was " << Title << " releasted ";
        cin >> yearReleased;

        cout << "Lastly the length ";
        cin >> runTime;
        cin.ignore();

        if (i == 0)
        {
            MovieData movie1(Title, Director, yearReleased, runTime);
            // I know function works when placed here
            // printMovie1(movie1); 
        }
        else if (i == 1)
        {
          MovieData  movie2(Title, Director, yearReleased, runTime);
         // I know function works when placed here
          // printMovie2(movie2);
        }
    }
    // these functions now dont work
    enter code hereprintMovie1(movie1);
    printMovie2(movie2);
    return 0;
}

【问题讨论】:

  • movie1movie2 变量在 if 块的范围内声明。如果要在外面使用,请在外面声明。

标签: c++ function if-statement for-loop struct


【解决方案1】:

您遇到了范围问题 当你写:

  if (i == 0)
        {
            MovieData movie1(Title, Director, yearReleased, runTime);
            // movie1 starts living

        } // Aaand he's dead.

您的 movie1 仅存在于这些括号内。这正是您必须在要打印它们的同一范围内声明 movie1 的原因。

另一件事,因为它是c++,请尝试查找class 的工作方式,我认为在您对问题的总体处理中class 会比struct 做得更好。

顺便说一句,为什么不把函数 printMovie1() 放在你的结构中呢?

哦,您不需要 2 个打印功能,因为它们执行完全相同的工作。

尝试例如调用:

printMovie1(movie1);
printMovie1(movie2);

看到了吗?它将是相同的输出。继续努力,你正在接近一件好事!

【讨论】:

  • 我同意class 会更适合它(只是从概念上来说),但实际上classstruct 之间没有任何区别,除了前者默认其成员为private,后者默认其成员为public
  • @Kvothe 我这么说只是因为他确实向他的结构声明了吸气剂,因此我的反思。但是,是的,感谢您明确了 class 和 struct 之间的范围差异!
  • 如果您认为我的回答有需要改进的地方,请不要忘记在投反对票时发表评论
【解决方案2】:

它与对象的scope 有关。您已经在它们的 if 语句中声明了这两个对象。简而言之,一个对象总是只存在于声明它的直接块内(想想{})。

在你的情况下:

if (i == 0)
    {
        MovieData movie1(Title, Director, yearReleased, runTime);
        //movie1 exists only inside this if statement.
    }
  //no movie1 from here on

所以你必须做类似的事情:

MovieData movie1;

if (i == 0)
    {
       movie1 = MovieData(Title, Director, yearReleased, runTime);

    }
//movie1 is accessible here. yay

【讨论】:

    【解决方案3】:

    您的 MovieData 仅存在于括号范围内。退出外部范围后,您将无法再访问它。 你可以修复它,在范围之外声明你的变量。

        MovieData  movie; //default construction
        if (i == 0)
        {
            movie = MovieData(Title, Director, yearReleased, runTime);
            // I know function works when placed here
            // printMovie1(movie1); 
        }
        else if (i == 1)
        {
          movie= MovieData(Title, Director, yearReleased, runTime);
         // I know function works when placed here
          // printMovie2(movie2);
    
        }
        //you can now use movie
    

    【讨论】:

      【解决方案4】:

      C/C++ 使用块局部变量。出于兴趣,这在编程语言中并不通用; R 和 JavaScript 是使用函数局部变量的著名语言。换句话说,在 R 和 JavaScript 中,当您声明(在 JavaScript 中)或分配(在 R 中)一个局部变量时,它成为函数的“激活对象”(在 JavaScript 中)或“评估环境”(在 R 中)的一部分。随后可以从函数内部的任何位置访问它,甚至在创建它的声明/赋值范围之外。

      由于您使用的是 C++,因此您必须小心您的块。您不能在块内声明变量,然后在块外访问它。这是由编译器强制执行的。因此,要解决您的问题,您必须将 movie1movie2 的声明从重度嵌套的块中移出,并移到将访问这些变量的最外层块中。您将必须定义一个空构造函数和赋值方法以允许它工作(因为您必须在赋值上分支),或者您可以在 if 分支中动态分配(即使用new),在在这种情况下你会调用构造函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 2018-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多