Help With Template

closed account (1vD3vCM9)
I just learned it, it kinda confuses me, and i have an error: Entry point must be defined ( using visual studio 2015 community )

Give me suggestions and please help.

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
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <string>
#include <conio.h>

template<class T>

class System {
private:
	T _Username;
	T _Password;
public:
	class Errors {
	public:
		void fatalError(T errorMsg) {
			std::cout << errorMsg << std::endl;
			std::cout << errorMsg.c_str() << std::endl;
		}
	};
	T set_values(T pass, T user);
	T printInfo();
	T checkForEmptyValues();
};
template<class T>

T System<T>::set_values(T pass, T user) 
{
	pass = _Password;
	user = _Username;
}
template<class T>

T System<T>::printInfo() {
	checkForEmptyValues();
	std::cout << "Username: " << _Username << std::endl;
	std::cout << "Password: " << _Password << std::endl;
}
template<class T>

T System<T>::checkForEmptyValues() {
	System::Errors er;
	
	if (_Username == NULL || _Username.empty()) {
	er.fatalError("_Username is NULL! No Values Are Set!");	
	}
	if (_Password == NULL || _Password.empty()) {
		er.fatalError("Password is NULL! No Values Are Set!");
	}
}
template<class T>

int main(int argc, char** argv) {
	System<int> sys; // confused what to put here..
	
	sys.set_values(155, "Mr_Oren");
	_getch();
	return 0;
}


Thanks.
Last edited on
Visual Studio carries various types of "project". Some of them don't recognise the "main" function in your code.

When you created your "project", did you definitely create it as an ordinary console project? Not a Win32 Windows project or other such whacky thing.
closed account (1vD3vCM9)
It's An Empty Project
Try starting again and explicitly selecting a Console project.


(As an aside, this is why I always advise against newcomers starting with an IDE. People end up having to learn how to use the IDE and what all the buttons do and all the whacky Microsoft oddities, instead of being able to just concentrate on programming.)
The problem is in line 49: template<class T> -- This just doesn't belong there:

Also in line 54: sys.set_values(155, "Mr_Oren"); since T is an int you need to supply two integers,

Also in line 25:
1
2
3
4
5
6
7
template<class T>
T System<T>::set_values(T pass, T user) 
{
	pass = _Password;
	user = _Username;
}

sys.set_values must return a value;

closed account (1vD3vCM9)
Thomas1965
I Need the _Username to be type of string, and the pass to be int.
how do i do that?
You need then a second type in your template.

 
  template<class T, class S>
Hi,

Thomas1965's advice applies throughout your program, you need two types.


The following aren't related to your problem:

Was there a particular need to nest the Error class? You could achieve the same thing with namespaces. Ideally ones own code should go in it's own namespace, rather than the global namespace.

Templates really need to be in header file/s.

Also consider using constructor/s, rather than a set_values function.

Scott Meyers advises to always use typename instead of class, for the template parameters. They are supposed to be equivalent there, but apparently there are situations where they are not.
Topic archived. No new replies allowed.