【问题标题】:Passing vector to member function of class将向量传递给类的成员函数
【发布时间】:2020-09-06 04:26:38
【问题描述】:

我正在尝试将向量传递给成员函数,但不断出现以下错误:

27 28 [错误]“双重装备::calcmass”不是“类装备”的静态成员

13 19 [错误] 非静态数据成员 'equip::time' 的使用无效

27 24 [错误]来自这个位置

28 1 [错误] '{' 标记之前的预期不合格 ID

我该如何纠正这个问题?

#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];
    }

【问题讨论】:

  • 您在calcmass 的定义中缺少time 的类型说明符。你还有一个额外的;
  • 另外,main函数中的time是什么?
  • 循环中的无条件返回使循环几乎无用。

标签: c++ function vector


【解决方案1】:

您的代码不起作用,因为您定义了一个类equip,但从未在main 中创建它的实例,然后您尝试将文件内容读入类time 的成员中。此外,类定义中的函数calcmass 没有参数,但稍后您使用未确定类型time 的参数声明它。删除函数的参数,它会看到time,因为它们都是同一个类的成员。

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2017-05-16
    • 1970-01-01
    • 2017-06-25
    • 2017-01-19
    • 2019-09-27
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多