C++ "Error: a nonstatic member reference must be relative to a specific object"



我试图从类中包含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;
};

任何帮助都将不胜感激!

您的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
    }
    

或者,您可以将该方法设置为静态方法,但每次只执行一步。编码快乐!

相关内容

最新更新