c++ Calling a different class method with a for loop -


im trying call different class method using [i].display(); function error im getting follows: no operator "[]" matches these operands

here relevant code: main.cpp:

 (int = 0; < 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;    (int = 0; < n; i++){       weather[i].display();   } 

weather.cpp

void weather::set(const char* date_description, double low , double high) {         strcpy(date, date_description);         hightemp = high;         lowtemp = low;      }      void weather::display() const {      } 

any idea why weather[i].display(); throwing error? didnt code display() implementation yet.

we're missing bit of code question, i'm assuming somewhere you've got

weather weather(/*constructor args*/); 

or just

weather weather; 

the error getting compiler saying "hey, tried [] on of type "weather", class doesn't know how that".

it looks (from weather[i].display() , 2 for-loops) you're expecting have list (either vector or array) of weather objects, , trying call display on each in turn.

the approach instead start with

weather weather[/*some integer*/]; 

or

std::vector<weather> weather; 

and change line weather.set(date_description, low, high); apply each item in turn (e.g. weather[i].set(date_description, low, high);)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -