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

C++ c++;无法在if语句之外访问stucts,c++,function,if-statement,for-loop,struct,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,

目标是让用户为两部电影输入一些电影信息。
然后两个函数输出电影数据
问题是当我尝试使用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;
}
#包括
#包括
使用名称空间std;
struct MovieData//stuct
{
字符串标题,//必需的变量
经理
国际年发布,
运行时间;
MovieData(字符串标题、字符串控制器、int YearReleased、int RunTime)//构造函数
{
头衔=头衔;
董事=董事;
yearReleased=yearReleased;
运行时=运行时;
}
字符串getTitle()//getters
{返回标题;}
字符串getDirector()
{返回控制器;}
整年
{return yearReleased;}
int getLength()
{返回运行时;}
};
void printMovie1(MovieData movieprint1)//输出电影1的功能
{

您可能有一个范围问题 当你写作时:

  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);

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

你有一个范围问题 当你写作时:

  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);

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

这与对象的
范围有关。你已经在它们的
if
语句中声明了这两个对象。简言之,对象总是只存在于立即块中(想想
{/code>和
}
)它是在一份声明中宣布的

就你而言:

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

它与对象的
作用域
有关。您已在其
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

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

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

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


<> P>因为你使用C++,你必须小心你的块。你不能在块中声明变量,然后在块外访问它。这是编译器强制执行的。因此,为了解决问题,你必须把你的代码< >代码> MOVIE1 和<代码> MVIE2从繁琐嵌套的块中移入OUTE。rmost块,其中将访问这些变量。您必须定义一个空构造函数和赋值方法以允许此操作(因为您必须在赋值上进行分支),或者您可以动态分配(即使用
新建
)在if分支内部,在这种情况下,您将在那时调用构造函数。

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


<> P>因为你使用C++,你必须小心你的块。你不能在块中声明变量,然后在块外访问它。这是编译器强制执行的。因此,为了解决问题,你必须把你的代码< >代码> MOVIE1 和<代码> MVIE2从繁琐嵌套的块中移入OUTE。rmost块,在其中将访问这些变量。您必须定义一个空构造函数和赋值方法以允许此操作(因为您必须在赋值上进行分支),或者您也可以在if分支内动态分配(即使用
new
),其中