Daniel Nguyen
Daniel Nguyen

Reputation: 41

Passing vector to member function of class

I am trying to pass a vector to a member function, but keep coming up with these errors:

27 28 [Error] 'double equip::calcmass' is not a static member of 'class equip'

13 19 [Error] invalid use of non-static data member 'equip::time'

27 24 [Error] from this location

28 1 [Error] expected unqualified-id before '{' token

How can I correct this?

#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

class equip
{
    public:
        vector <double> time;
        vector <double> mass;
        vector <double> velocity;
        vector <double> height;
        double calcmass();
        double calcvelocity();
        double calcheight();
        double calctmax(); 
    private:
        double T = 7000;
        double g = 32.2;
        double K = 0.008;
};

double equip::calcmass(time);
{
    int i = 0;
    for(i=0; i<time.size(); i++)
    {
        return mass[i] = (3000 - 40 * time[i]) / g;
    }
}

int main()
{
    int i = 0;

    ifstream infile;
    string filename;
    cout<<"Enter input file name for time (t): ";
    cin>>filename;
    infile.open(filename.c_str());

    while(infile.fail())
    {
        cerr<<"Error opening file. \n";
        cout<<"Enter file name: ";
        cin>>filename;
        infile.open(filename.c_str());
    }

    for(i=0; i<time.size(); i++)
    {
        infile>>time[i];
    }

Upvotes: 0

Views: 186

Answers (1)

Aramakus
Aramakus

Reputation: 1920

Your code is not working because you defined a a class equip, but never created an instance of it in main, and then you try to read a file content into a member of class time. Also, your function calcmass in the class definition has no arguments, but later you declare it with argument of undetermined type time. Remove the argument of a function, it will see time anyway as they are both members of the same class.

Upvotes: 1

Related Questions