尝试使用类时遇到标识符错误

时间:2018-07-21 18:04:59

标签: c++ class object identifier

我正在尝试建立一个交互式循环,该循环只会在用户输入EOF时中断。

当我运行程序并在main()中运行代码时,它会吐出错误

  

错误C3861:'startClass':找不到标识符

我已经在公共类中以及在main函数外部的main类中声明了startClass,所以不确定不能找到哪个标识符。谢谢。

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

class ExchangeList {
private:
    double exchRate;

public:
    ExchangeList() {
        exchRate = 0;
    }

    void AddExchange(double x) {
        if (x > 0) {
            exchRate = x;
        }
    }

    double getExchange() {
        return exchRate;
    }

    void startClass(double, double, double, string, string);

};


int main() {
    double value{ 0 };
    double exchange{ 0 };
    double transaction{ 0 };
    string toCurr;
    string fromCurr;
    //ExchangeList N;

    while (!cin.eof()) {
        cout << "Input exchange rate: ";
        cin >> exchange;
        startClass(value, exchange, transaction, toCurr, fromCurr);   
    }
    return EXIT_SUCCESS;
}


void startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) {
    ExchangeList N;

    N.AddExchange(exchange);
    cout << N.getExchange();
    return;

}

1 个答案:

答案 0 :(得分:0)

您在这里遇到一些问题:

cin :: eof()

使用此函数是一个错误,最好找到其他东西来结束while循环。像特定的输入那样表示循环已结束,请尝试以下操作:

cout << "Input exchange rate: ";
cin >> exchange;
while (exchange != -9999) {
    startClass(value, exchange, transaction, toCurr, fromCurr); 
    cout << "Input exchange rate: ";
    cin >> exchange;
}

在主函数上方的功能声明

您创建了一个没有类startClass且没有在main函数上方声明的函数,这导致main函数无法使用此函数。

void startClass(double value, double exchange, double transaction, string toCurr, string fromCurr);

int main() {
...
//use the function...
...
}

void startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) {
        ...
}

或者,在主体上方没有声明-

void startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) {
        ...
}

int main() {
...
//use the function...
...
}

使函数属于类

您试图声明一个类的函数startClass,但是在定义中您没有指定类名,就像这样:

void ExchangeList::startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) {
    AddExchange(exchange);
    cout << getExchange();
    return;
}

关于类的功能

当您调用类的函数时,您没有指定函数的类:

class ExchangeList {
    ...
public:
    void startClass(double, double, double, string, string);
};

void ExchangeList::startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) {
    AddExchange(exchange);
    cout << getExchange();
    return;

}

int main() {
    ...
    ExchangeList N;

    while (exchange != -9999) {
        ...
        N.startClass(value, exchange, transaction, toCurr, fromCurr);   
    }
    return EXIT_SUCCESS;
}

类内部的静态函数

class ExchangeList {
    ...
public:
    static void startClass(double, double, double, string, string);
};

int main() {
    ...    
    while (exchange != -9999) {
        ...
        ExchangeList::startClass(value, exchange, transaction, toCurr, fromCurr);   
    }
    return EXIT_SUCCESS;
}

void ExchangeList::startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) {
    ExchangeList N;

    N.AddExchange(exchange);
    cout << N.getExchange();
    return;

}