c++ - Call to non-static member function without an object argument compiler error -
i working on project intro c++ class build program calculate various statistics. have calculations down, our professor wants use std::istream
collect input file. program keep collecting information file until reaches end of file marker. unfamiliar way std::istream
works , keep encountering error when try compile.
main.cpp:5:10: error: call non-static member function without object argument stats::getinput(std::cin);
here stats.cpp file:
#include "stats.h" #include <vector> #include <cstdlib> #include <iostream> stats::stats(){ } std::vector <double> stats::getinput(std::istream& input_stream){ std::vector <double> stream; double x; while(input_stream){ input_stream >> x; // std::cout << "your list of numbers is: " << x << std::endl; if(input_stream){ stream.push_back(x); } } return stream; }
here header file:
#ifndef _stats_ #define _stats_ #include <vector> #include <cstdlib> class stats{ public: stats(); std::vector <double> getinput(std::istream& input_stream); private: }; #endif
and here main.cpp file:
#include "stats.h" #include <iostream> int main(){ stats::getinput(std::cin); }
like said, beginner in c++ answer simple, c++ vastly different python. have seen similar questions, none of them have helped me figure out.
thanks
the error message compiler clear.
getinput
non-static
member function of class.
you need object of class able use member function.
instead of
stats::getinput(std::cin);
use
stats obj; obj.getinput(std::cin);
another solution.
since class not have member variables, may change getinput
static
member functions.
class stats { public: stats(); static std::vector <double> getinput(std::istream& input_stream); private: };
if that, may use:
stats::getinput(std::cin);
also, loop read data can simplified to:
while (input_stream >> x){ stream.push_back(x); }
Comments
Post a Comment