Another template problem

Hi guys,

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>

using namespace std;
template <class T>
class Number
{
	public:
		Number()
		{
			setInput1(cin);
			setInput2(cin);
		}
		~Number()
		{
		}
		T getInput1()
		{
			return input1;
		}
		T getInput2()
		{
			return input2;
		}
		void setInput1(T input1) {input1 = cin; return;}
		void setInput2(T input2) {input2 = cin; return;}
	private:
		T input1;
		T input2;

};



int main()
{
	Number<T> n1;
	cout << "Please enter first number";

}
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).

EDIT: Fixed syntax error.
Last edited on
@tcs: ¿why are you passing temporaries by non-const reference?

@OP: ¿wtf? ¿are you hitting your keyboard randomly?
Try to do it without the template first.
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
1
2
 Factorial f1(numberInput);
f1.iterativeCalculation(numberInput);


and its worked.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>

using namespace std;
template <class T>
class Number
{
	public:
		Number(float firstInput, float secondInput) : (input1(firstInput)), (input2(secondInput)) 
		{
		}
		~Number()
		{
		}
		T getInput1()
		{
			return input1;
		}
		T getInput2()
		{
			return input2;
		}
		void setInput1(T input1) {input1 = firstInput; return;}
		void setInput2(T input2) {input2 = secondInput; return;}
		void print()
		{
			cout << input1;
			cout << input 2;
			cin.ignore(2);
		}
	private:
		T input1;
		T input2;

};



int main()
{
	Number<float> n1(float firstInput, float SecondInput);
	float firstInput, secondInput;
	cout << "Please enter first number \n";
	cin >> firstInput;
	cout << "please enter second number \n";
	cin >> secondInput;
        n1.print(firstInput,secondInput);
}
@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.
@tcs: void setInput1(const T &input1)

@OP:
Number<float> n1(float firstInput, float SecondInput); is declaring a function.

You call n1.print(firstInput,secondInput);, but you declared void print()

void setInput1(T input1) {input1 = firstInput; return;} basically does nothing.
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
Last edited on
not getting any errors
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
1.0-27.18
.
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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>

using namespace std;
template <class T>
class Number
{
	private:
		T input1;
		T input2;
	public:
		Number(float firstInput, float secondInput) : (input1(firstInput)), (input2(secondInput)) 
		{
		}
		~Number()
		{
		}
		T getInput1()
		{
			return input1;
		}
		T getInput2()
		{
			return input2;
		}
		void setInput1(T input1) {input1 = firstInput; return;}
		void setInput2(T input2) {input2 = secondInput; return;}

		void print(input1, input2)
		{
			cout << input1;
			cout << input2;
			cin.ignore(2);
		}

};



int main()
{
	float firstInput = 0, secondInput= 0;
	cout << "Please enter first number \n";
	cin >> firstInput;
	cout << "please enter second number \n";
	cin >> secondInput;
	Number<float> n1();
	n1.print(firstInput,secondInput);
}
@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.
May be you'll have a second look at http://www.cplusplus.com/doc/tutorial/?

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?

Even if reading a tutorial may be a little bit painful, the http://www.cplusplus.com/doc/tutorial/ uses lots of examples and is easy to understand.


Have fun!
@tcs: that parameter shouldn't be an `output' parameter.
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
2
3
Number(float firstInput, float secondInput) : (input1(firstInput)), (input2(secondInput)) 
		{
		}

my print method is supposed to print out the two member variables which should be set to the two variables the user inputs.

user inputs first number it goes into firstInput then they input second number and that goes into secondInput

then these get transferred into class variables of input1 and input2 which then get printed out by the print function.
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.
Topic archived. No new replies allowed.