C++ c++;使用for循环调用其他类方法

C++ c++;使用for循环调用其他类方法,c++,class,loops,for-loop,implementation,C++,Class,Loops,For Loop,Implementation,我试图使用[i].display()调用另一个类方法;功能 获取i的错误如下:没有运算符“[]”与这些操作数匹配 这是我的相关代码: Main.cpp: for (int i = 0; i < n; i++){ char date_description[7]; double high = 0.0, low = 0.0; cout << "Enter Date :"; cin.getline(date_description, 80, '\n'

我试图使用[i].display()调用另一个类方法;功能 获取i的错误如下:
没有运算符“[]”与这些操作数匹配

这是我的相关代码: Main.cpp:

 for (int i = 0; i < n; i++){
    char date_description[7];
    double high = 0.0, low = 0.0;
    cout << "Enter Date :";
    cin.getline(date_description, 80, '\n');
    cout << "Enter High :";
    cin >> high;
    cout << "Enter Low :";
    cin >> low;
    cin.ignore();
    weather.set(date_description, low, high);

  }
  cout << endl;
  cout << "Weather report:\n";
  cout << "======================" << endl;

  for (int i = 0; i < n; i++){
      weather[i].display();
  }
知道我的天气为什么吗[i].display(); 抛出错误吗?
我还没有编写display()实现的代码。

您的问题中缺少一些代码,但我假设您已经找到了一些代码

 for (int i = 0; i < n; i++){
       weather[i].display();  }
Weather weather(/*constructor args*/);
或者只是

Weather weather;
您得到的错误是编译器说“嘿,我试着在“Weather”类型的东西上做[],但是类不知道如何做”

看起来(从
weather[i].display()
和两个for循环)您希望有一个天气对象列表(向量或数组),并尝试依次调用每个对象的display

方法是从

Weather weather[/*some integer*/];

std::矢量天气;

并更改行
weather.set(日期描述,低,高)
也依次应用于每个项目(例如
天气[i]。设置(日期描述,低,高);

您如何定义
天气,尤其是
[]
操作符的重载?您的意思是什么?对不起,我是新来的语言/术语,我在顶部用这个定义:天气;你认为
[i].display()
应该做什么?如果
weather[i]
意味着什么,那么
weather[i].display()
将调用对象上的display(),该对象是
weather[i]
的结果。但是,由于运算符[]没有为对象weather定义,所以它没有任何意义。@andirew,“你是什么意思?”--实际上,他的意思是,是什么让你认为表达式
weather[i]
应该有意义?很抱歉,输入错误,我问的是
weather
(类型),而不是
weather
(变量).我可以使用Weather Weather[n]n作为用户输入的变量如果要动态分配,请使用Weather*Weather;天气=新天气;
Weather weather;
Weather weather[/*some integer*/];
std::vector<Weather> weather;