So i'm trying to make a class template and i have a couple of problems firstly is there anyway to pass an input from a user straight into a function.
i googled and it said using getline(cin, ); would work but i've tried getline(cin, n1.setInput1); but that dosn't work and i tried putting them both in the constructor and doing getline(cin, n1.Number()); but that dosn't work either
Secondly im having a few errors pop up when i try to use my class in my main.
errors:
Error 1 error C2955: 'Number' : use of class template requires template argument list
Error 2 error C2133: 'n1' : unknown size
Error 3 error C2512: 'Number' : no appropriate default constructor available
Error 4 IntelliSense: identifier "T" is undefined
all on line 36
i've tried Number<T> n1 and Number n1 but neither work
Line 36: When instantiating a template you've to pass the "real" type as argument which you want the class to use. F.e. Number<int> n1; instantiates an object called n1 having two private attributes of type int.
Lines 10, 11: Number<T>::setInput expects type T as input. In my example above this would be an int not an istream.
Lines 24, 25: Will work. But you'll get a value returned only if the template was parametrized with a reference of T as f.e. in Number<int&> n1;. But then you may have to initialize Number<T>::input1 and Number<T>::input2 on construction to reference to any variable. F.e. Number(T& i1, T& i2) : input1(i1), input2(i2). Another way may be declaring both setter methods like void setInput1(T &input1).
hi i've rejigged my code so that instead of passing straight from the input it instead gets stored as a float because i only really need it to work for ints and floats but now i can't seem to call a function from my template class, i made a temp test function which just prints out the variables but i can't seem to call it.
not getting any errors but im getting a warning Warning 1 warning C4930: 'Number<T> n1(float,float)': prototyped function not called (was a variable definition intended?) 40
and when i hover over the code when trying to do n1.print(firstInput, secondInput); it says expression must have class type. i don't understand whats wrong because in previous work i've just put something like
@ne555: Where do I pass temporaries? May be that I'd overlooked something. I know, there are more problems: F.e. The assignments in lines 24, 25. But I didn't really understand the intention of this little class to fix this problems.
i have to make a program that takes in two inputs passes them into a template class, sets them as variables in the class the uses them to compare with each other.
i've already done the using templates to compare different data types so i'll be able to easily redo that bit but im struggling with passing things into this template class now.
oh sorry it also comes up with this error when i try n1.print(firstInput, secondInput);
Error 1 error C2228: left of '.print' must have class/struct/union 46
Really?
Line 27 won't compile.
Lines 40, 41: Won't compile. Swap lines 40 and 41. Rewrite the number instantiation without those argument type specifiers like Number<float> n1(firstInput, SecondInput);. You may also want to initialize both variables first.
Another way would be to use some float constants on n1 creation, f.e. like Number<float> n1(1.0, -27.18);.
Line 46: Will print
thanks for the advice btw guys, right i swapped the lines 40 and 41 around and removed the arguments and also initialized them both first but i can't use constants as the numbers have to come from the user inputting them.
i also have 3 errors now.
Error 1 error C2061: syntax error : identifier 'input1' 28
Error 2 error C2228: left of '.print' must have class/struct/union 47
Error3 IntelliSense: expression must have class type 47
@ne555: void setInput1(T &input1) has nothing to do with passing temporaries. It's just a classical way to define an output parameter. Regardless of the methods assignments type incompatibility, the assignment wouldn't work when defining the output parameter as const.
Line 28: If a methods takes any parameter its type has to be declared in its signature.
Line 46: You didn't define a default constructor.
Lines 28ff.: Your print method doesn't refer to Number's private attributes. Is it your intention?
i've read through quite a few different tutorials to try to figure this out,
by signature do you mean i have to put void print(T input1, T input2)
also iwas led to believe you didn't need a default constructor as one is made by the program but in any case i made a constructor which should work it think.
1. A default constructor will only be implicitly defined if no one else was defined by the programmer of the class. So your statement at line 46 wouldn't compile.
2. Add both float variables as construction arguments in line 46. Then you'll not need a default constructor.
3. Why does your print method has any parameters? An object is allowed to access all its attributes - even those one declared private.
4. Compilation/linking will also fail due to unknown symbols firstInput and secondInput in your setters body. Name the setters parameter with this symbols.
Another, more elegant way, to get stream input into An object of your class will be by adding istream &operator>>(istream &input). It's body may contain statements to fill both attributes from the given istream.
If you also define a function istream &operator>>(istream &input, Number<T> &n); you may assign values to an object n1 of your class as cin >> n1; Don't forget to define it as a function template.
thank you so much tcs, i know my stupidity was probably annoying but thank you so much for sticking with me on it, its finally working. now ill make a back up and add in the other functions then review to make sure i understand it fully.