-1

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

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

error C3861: 'startClass': identifier not found

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.

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

}
CD'A
  • 41
  • 5
  • You declared `startClass` as a member of `ExchangeList` but are trying to use it as a free function – UnholySheep Jul 21 '18 at 18:07
  • See [why `while (!cin.eof())` is wrong](https://stackoverflow.com/q/5605125/9254539). – BessieTheCookie Jul 21 '18 at 18:10
  • `startClass()` is a non-static member of `ExchangeList`, so it can only be called on an object, not as a freestanding function. You might want to review how class member functions work in C++. – BessieTheCookie Jul 21 '18 at 18:17

1 Answers1

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:

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.

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:

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;

}
CoralK
  • 3,341
  • 2
  • 8
  • 21
  • I doubt that `void ExchangeList::startClass(double value, double exchange, double transaction, string toCurr, string fromCurr) { ExchangeList N;` is something that the OP is looking for. Seems more like OP'd like to make that a friend function. – Surojit Jul 21 '18 at 18:17
  • There are so many ways to solve this problem, I am just trying to mention most of them in this comment, so the publisher can use the one that he feel more comfortable with. – CoralK Jul 21 '18 at 18:21
  • What you call a "pre-declaration" is simply called a "declaration", and the implementation is called a "definition". `this.some_member` doesn't compile, and there is no point in writing `this->` every time you access a member. There's also much nicer ways to read to the end of the file other than using some special value. – BessieTheCookie Jul 21 '18 at 22:09