C ++“错误:非静态成员引用必须相对于特定对象”

时间:2014-09-16 15:41:17

标签: c++

我试图在课程中加入experiencecalculator但我得到 Error: a nonstatic member reference must be relative to a specific object

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <windows.h>
#include "ExpCalc.h"
#include <algorithm>
#include <string>
#include <cctype>

using namespace std;

int main() {
    cout << "Pick which calculator you would like to use by typing the correct "
            "number.\n";
    cout << "1. Experience Calculator" << endl;
    // cout << "" Insert other calculators and there number here.
    // cout << ""
    int choice;
    cin >> choice;

    if (choice == 1) {
        ExpCalc::ExperienceCalculator;
    }
}

我从中获取的课程是:

ExpCalc.h

class ExpCalc
{
public:
    ExpCalc();
    int ExperienceCalculator;
};

ExpCalc.cpp

#include "stdafx.h"
#include "ExpCalc.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <windows.h>
#include <algorithm>
#include <string>
#include <cctype>

using namespace std;

ExpCalc::ExpCalc() {}
int ExperienceCalculator() {
    double timetotal;
    double timeperinv;
    double xptotal;
    double xpitem;
    double amount;
    double perinv;
    double totalinv;
    double costper;
    double costtotal;

    SetConsoleTitle(TEXT("Runescape Skill Calculator"));

    cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=Runescape Skill "
            "Calculator=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;
    cout << endl;

    cout << "How much experience do you want to get?" << endl;
    cin >> xptotal;
    cout << endl;

    cout << "How much does it cost per item?" << endl;
    cin >> costper;
    cout << endl;

    cout << "How much experience do you get per item?" << endl;
    cin >> xpitem;
    cout << endl;

    cout << "How many items can you process in one inventory?" << endl;
    cin >> perinv;
    cout << endl;

    cout << "How long does it take to process one inventory of items?" << endl;
    cin >> timeperinv;

    system("CLS");

    amount = xptotal / xpitem;
    totalinv = amount / perinv;
    timetotal = totalinv * timeperinv;
    costtotal = amount * costper;

    cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=Runescape Skill "
            "Calculator=+=+=+=+=+=+=+=+=+=+=+=+=+=" << endl;

    cout << endl;
    std::cout << std::setprecision(1) << fixed;
    cout << "The amount of items that you will need to process is: \n" << amount
         << endl;
    cout << endl;

    cout << "The total amount of inventories to process all items is: \n"
         << totalinv << endl;
    cout << endl;

    cout << "The total time it will take to complete processing all items is:\n"
         << timetotal << endl;
    cout << endl;

    cout << "The total cost of the items will be: \n" << totalinv << endl;
    cout << endl;

    cout << "The total amount of inventories to process is: \n" << totalinv
         << endl;
    cout << endl;

    cout << "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+==+=+==+=+==+=+==+=+==+=+=+=+=+=+=+="
            "+=+=+=+=+=+=+=" << endl;

    system("PAUSE");

    return 0;
};

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您的H文件将ExperienceCalculator描述为int字段。您的CPP文件将ExperienceCalculator描述为自由函数(甚至不是ExpCalc的方法)。所以我怀疑你必须做以下修正:

  • H档案:

    int ExperienceCalculator(); // parenthesis to be added
    
  • CPP文件:

    int ExpCalc::ExperienceCalculator() { // class name ExpCalc to be added
    
  • 主文件:

    if (choice == 1) {
        ExpCalc exp_calc; // instantiate the class
        exp_calc.ExperienceCalculator(); // make a call to non-static method
    }
    

或者,您可以将方法设为静态,但让我们一步一步。快乐的编码!