-4

I am having some errors appear in my code and i can't figure them out. I reduced my code down to the very basic simple functions/class calls but still have issues with this.

#include <iostream>
using namespace std;

template <class T> 

class FC
{
private: 
    double netprofit, costofinvest;
    double curras, invent, curliab;
public: 

    void ROI(double np, double ci)
    {
     netprofit = np; costofinvest = ci;
    }

    double getROI()
    {
     return (netprofit - costofinvest) / costofinvest;
    }

    void ATR(double ca, double inv, double cl)
    {
     curras = ca; invent = inv; curliab = cl;
    }

    double getATR()
    {
    return (curras - invent) / curliab;
    }

};


int main()
{
 FC ROI, AcidTestRatio; 

 ROI.ROI(27, 288);
 cout << ROI.getROI() << endl;

 AcidTestRatio.ATR(77, 2l, 344);
 cout << AcidTestRatio.getATR() << endl;


return 0;
}

The errors I get look like this:

In function 'int main()':
39:22: error: missing template arguments before 'ROI'
41:2: error: 'ROI' was not declared in this scope
44:2: error: 'AcidTestRatio' was not declared in this scope
YelizavetaYR
  • 1,611
  • 6
  • 21
  • 37
  • 3
    since you're not using `T`, you can just delete the `template` line – alain Apr 22 '15 at 18:54
  • modifying the code to look like this `void ROI(T np, T ci)` and `void ATR(T ca, T inv, T cl)` doesn't fix the errors that appear to be coming through in main. – YelizavetaYR Apr 22 '15 at 18:58
  • @YelizavetaYR There's other dependent code you're not showing here. – πάντα ῥεῖ Apr 22 '15 at 19:04
  • @πάνταῥεῖ there are many other functions, but all are semi-independent. The assignment is to create a series of template classes that can use any passed data type. which is what i'm trying to do with the ROI and ATR functions only – YelizavetaYR Apr 22 '15 at 19:05

1 Answers1

4

you need to give a template argument ´T´:

FC<float> ROI, AcidTestRatio; 

but as alain points out, you are not using T yet, so you may as well instead remove template <class T> from the beginning of your code instead.

Alternatively, you may want to use T as the type of the members instead of double:

template <class T> 
class FC
{
private: 
    T netprofit, costofinvest;
    T curras, invent, curliab;
public: 

    void ROI(T np, T ci)
    {
     netprofit = np; costofinvest = ci;
    }

    T getROI()
    {
     return (netprofit - costofinvest) / costofinvest;
    }

    void ATR(T ca, T inv, T cl)
    {
     curras = ca; invent = inv; curliab = cl;
    }

    T getATR()
    {
    return (curras - invent) / curliab;
    }
};

Update: Full example here

Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • Can you please explain what this means - as I understand in order to template a the data types you need to add this line `template ` of code and then use T, with or without the T definitions the code doesn't work. – YelizavetaYR Apr 22 '15 at 19:03
  • 1
    The point of templates are to create versions of a class (or function) with only slight variations (for example a storage class that can store integers or floats). start with reading the book "tour of C++", http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Johan Lundberg Apr 22 '15 at 19:04
  • Correct, but your line of code suggests its a float, we want to set it up as a generic type it can be anything depending on run time. (the link takes me to a book,and I've got books, and I've got reading material) – YelizavetaYR Apr 22 '15 at 19:07
  • with the suggested changes - even if I have everything as T (which i did at some point and then changed back) I still get the same exact error messages. All coming from main. (your suggestion does fix it) but i'm not sure i understand why I need that there. – YelizavetaYR Apr 22 '15 at 19:13
  • 1
    "a generic type it can be anything depending on run time":. Then templates is not what you are after... Templates are completely determined or specified at compile time. You can have FC or FC but not FC. – Johan Lundberg Apr 22 '15 at 19:13
  • yes, i'm aware that a template value/how its used is determined at run time. what confuses me is then `FC ` what does this mean and why do we need to specify it. Isn't the `template ` enough to state that its a template class? – YelizavetaYR Apr 22 '15 at 19:24
  • It is enough to state that it is a template class. but when you *use* it you need to specify what T shall be. As I said, it is not done at run-time. Template *functions* can *deduce* T based on arguments, but that is another story. This is well described in the first chapters of any good C++ book. – Johan Lundberg Apr 22 '15 at 19:29
  • Thank you - but I thought the function call will decide what is done at run time. In other words if our function is `sum(3,5)` vs `sum(2.5,4.7)` that is when its decided. So we don't need to code in advance. It gets decided based on user input. – YelizavetaYR Apr 22 '15 at 19:30