0

Well, basically for a class I need to make a simple program that used templates, in which I receive data from the user that I dont really know the type of, until I receive it.

template <typename T>
int main()
{
    Calculator<T> calc;
    bool flag = true;
    int punto = 0;
    string entry, op;
    T a, b, r;
    entry = ' ';
    while (flag) {
        try {
            cin << op;
            entry += op;
            for (int i = 0; i < entry.length(); i++) {
                if (entry[i] == '+' || entry[i] == '-' || entry[i] == '*' || entry[i] == '/') {
                    for (int j = 0; j < i; j++) {
                        if (entry[j].isdigit) {
                            a += entry[j];
                        }
                        else if (entry[j] == '.') {
                             a += entry[j];
                             punto++;
                             if (punto > 1) {
                                 throw '.';
                             }
                         }
                        else{
                            throw "l";
                        }

                    }
                    if (punto = 1) {
                        a = stof(a);
                    }
                    else {
                        a = stoi(a);
                    }

I know the problem is the template part at the start, but sice I need to change the type a couple types I dont really know what to do. Obviusly because of the template it doesnt detect my main() fuction as main and is giving me the "Header Errors LNK2019"

1 Answers1

0

This does not work because the linker fails to find main. Also, it doesn't make sense because you can only run main once, and you ought to know which type T has at any given invocation. :-)

If you just want to use T as a placeholder, you use a typedef declaration (or an equivalent using declaration) like this:

int main() {
    using T = int;
    Calculator<T> calc;
    // rest of code here
    // ...
}