c ++ stucts无法在if语句之外访问

时间:2015-04-24 09:26:10

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

目标是让用户输入两部电影的一些电影信息 然后两个功能输出电影数据
问题是当我尝试使用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;
}

4 个答案:

答案 0 :(得分:0)

您有范围问题 当你写:

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

        } // Aaand he's dead.

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

另一件事,因为它是c++,试着查看class的工作原理,我认为在你对问题的总体看法class会做得比struct

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

哦,你不需要2个打印功能,因为他们做同样的工作。

尝试举例:

printMovie1(movie1);
printMovie1(movie2);

看?它将是相同的输出。保持工作,你接近一件好事!

答案 1 :(得分:0)

它与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

答案 2 :(得分:0)

您的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

答案 3 :(得分:-2)

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

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