尝试使用类时出现标识符错误

时间:2020-12-22 21:50:29

I am attempting to make an interactive loop that will only break when the user inputs EOF.

我试图创建一个交互式循环,只有在用户输入EOF时才会中断。

When I run the program and it runs the code in main() it spits out the error

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

error C3861: 'startClass': identifier not found

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

I've declared startClass in the public class as well as outside the main in its own function so not sure what identifier cannot be found. Thanks.

我已经在公共类中声明了startClass,并在其自己的函数中声明了main,因此不确定无法找到哪个标识符。谢谢。

#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 个解决方案

#1


0  

You have some problems here:

你有一些问题:

cin::eof()

It is a mistake to use this function, it will be better to find something else to end the while loop. Somthing like a specific input that will indicate that the loop is over, try something like this:

使用此函数是错误的,最好找到其他东西来结束while循环。像一个特定的输入,表明循环结束,尝试这样的事情:

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

Declaration of the function, above the main

You created a function without class startClass, without declaration above the main function, what cause that the main function can't use this function.

你创建了一个没有类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) {
        ...
}

Or, withou declaration- definition above the main:

或者,在主要上面的声明定义:

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

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

Make the function belong to the class

You tried to declare a class's function startClass, but in the definition you didn't specified the class name, like this:

你试图声明一个类的函数startClass,但是在定义中你没有指定类名,如下所示:

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

In case of class's function

When you call the function of the class, you didn't specified the class of the function:

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

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

Static function inside the class

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;

}

#1


0  

You have some problems here:

你有一些问题:

cin::eof()

It is a mistake to use this function, it will be better to find something else to end the while loop. Somthing like a specific input that will indicate that the loop is over, try something like this:

使用此函数是错误的,最好找到其他东西来结束while循环。像一个特定的输入,表明循环结束,尝试这样的事情:

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

Declaration of the function, above the main

You created a function without class startClass, without declaration above the main function, what cause that the main function can't use this function.

你创建了一个没有类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) {
        ...
}

Or, withou declaration- definition above the main:

或者,在主要上面的声明定义:

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

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

Make the function belong to the class

You tried to declare a class's function startClass, but in the definition you didn't specified the class name, like this:

你试图声明一个类的函数startClass,但是在定义中你没有指定类名,如下所示:

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

In case of class's function

When you call the function of the class, you didn't specified the class of the function:

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

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

Static function inside the class

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;

}