I'm just learning how to use templates so I don't really have any experience with them, and I can't seem to understand what I'm supposed to do ... there's apparently an issue with the showWeaponSpecs:

error C2783: 'WS AssaultRifle::showWeaponSpecs(void)' : could not deduce template argument for 'WS' : see declaration of 'AssaultRifle::showWeaponSpecs'

AssaultRifle2.h

#include <iostream>
#include <string>
using namespace std;

class AssaultRifle
{
	private:
		string Name;
		string FurnitureType;
		string Caliber;
		int MagazineSize;
		int MaxCarryingCapacity;
		
	public: 
		template <typename N>
		N setName(N n)
		{  Name = n;  }

		template <typename FT>
		FT setFurnitureType (FT ft)
		{  FurnitureType = ft;  }

		template <typename C>
		C setCaliber (C c)
		{  Caliber = c;  }

		template <typename MS>
		MS setMagazineSize (MS ms)
		{  MagazineSize = ms;  }

		template <typename MCC>
		MCC setMaxCarryingCapacity (MCC mcc)
		{  MaxCarryingCapacity = mcc;  }

		template <typename WS>
		WS showWeaponSpecs()
		{
			cout << "Assault Rifle: " << Name << endl;
			cout << "Furniture Type: " << FurnitureType << endl;
			cout << "Caliber: " << Caliber << endl;
			cout << "Magazine Size: " << MagazineSize << endl;
			cout << "Max Carrying Capacity: " << MaxCarryingCapacity << endl << endl;
		} 
};

WeaponCreator2.cpp

#include <iostream>
#include "AssaultRifle2.h"
using namespace std;

int main()
{
	AssaultRifle M16A4;

	M16A4.setName("M16A4");
	M16A4.setFurnitureType("Black Polymer");
	M16A4.setCaliber("5.56 x 45mm NATO");
	M16A4.setMagazineSize(30); 
	M16A4.setMaxCarryingCapacity(300); 
 
        M16A4.showWeaponSpecs();
 
	return 0;
}

Sorry, forgot to make the changes to the AssaultRifle header

AssaultRifle2.h

#include <iostream>
#include <string>
using namespace std;

class AssaultRifle
{
	private:
		string Name;
		string FurnitureType;
		string Caliber;
		int MagazineSize;
		int MaxCarryingCapacity;
		
	public: 
		template <typename N>
		N setName(N n)
		{  
		   Name = n;
		   return n; 
		}

		template <typename FT>
		FT setFurnitureType (FT ft)
		{  
                   FurnitureType = ft;
		   return ft; 
		}

		template <typename C>
		C setCaliber (C c)
		{  
			Caliber = c;
			return c;
		}

		template <typename MS>
		MS setMagazineSize (MS ms)
		{  
		   MagazineSize = ms;
		   return ms;
		}

		template <typename MCC>
		MCC setMaxCarryingCapacity (MCC mcc)
		{  
			MaxCarryingCapacity = mcc;
			return mcc;
		}

		template <typename WS>
		WS showWeaponSpecs()
		{
			cout << "Assault Rifle: " << Name << endl;
			cout << "Furniture Type: " << FurnitureType << endl;
			cout << "Caliber: " << Caliber << endl;
			cout << "Magazine Size: " << MagazineSize << endl;
			cout << "Max Carrying Capacity: " << MaxCarryingCapacity << endl << endl;
		} 
};

I dont think you fully understand what templates are for, let's look at the name setter for example:

template <typename N>
N setName(N n)
{  
   Name = n;
   return n; 
}

Why do you need a template function here? Name is always a string.
So this would do just as well:

void setName(string n)
{  
   Name = n;
   //return n;  // Why do you need to return n? The caller already knows it
}

But now, every time you call setName( someString ); a copy is made of 'someString', so even better:

void setName( const string& n )
{
    Name = n;
}

Even this can be further 'optimized' if you call setName like: setName( "My New name" );
But let's not get too much into optimization 'details'

Read up on when and how to use templates ;)

They're used so you can have different return types be supported by one function so you can use int, float, double, etc vs making several of the same function with different return types. Like I said I'm experimenting with templates, I've actually already written a version with just regular functions and classes

As said earlier, you aren't using the template correctly. In your "SetName", if a template instantiation is made of type double, then you will have a compiler error, because you can't assign a double to a string.

different return types be supported by one function so you can use int, float, double, etc vs making several of the same function with different return types.

Your definition of the use of templates is completely wrong (or at least extremely reductionist). First of all, what you are describing is "function overloading" (you can use function templates to have the same effect but it is usually worse than overloading, certainly not in the code you posted). And having different return types for the functions is not possible in neither function overloading nor function templates. The C++ compiler cannot resolve which function to use based only on the return type. This is why you cannot have function overloads or function templates that only differ by the return type. This is exactly the error that your compiler is giving you for the function showWeaponSpecs() because it does not have a parameter whose type depends on the template argument. You can fix it by either not using function templates for the above code and use a void return type (highly recommended!), or by using a dummy parameter, or by instantiating the function call explicitly "M16A4.showWeaponSpecs<int>();".

I suggest to read up seriously on templates because they are so much more powerful than what you are using them for (in fact, what you are using them for is a travesty! IMO). And as firstPerson already pointed out, if you call the function with the wrong type, you will get a compile-time error inside your function. You are shifting the error occurrence from the caller context to the callee context (this is just horrible! It's the first time I see templated code being much worse than non-templated code).

Well thanks, I appreciate everybody's efforts to make me look like I'm an idiot... other than that, thanks for the advice. I was just trying to understand how to implement them, i wasn't going so much for "am i using it right?" sheesh.

Well thanks, I appreciate everybody's efforts to make me look like I'm an idiot... other than that, thanks for the advice. I was just trying to understand how to implement them, i wasn't going so much for "am i using it right?" sheesh.

Take the criticism as a help instead of brushing it off your shoulder. Every time someone points what I'm doing wrong, I take that as an advice from someone who knows more and learn and become better from it. And if you want to be a good programmer, I suggest you do the same.

Alright fine. But saying my code is a piece of crap is a little harsh, that was just uncalled for

Alright fine. But saying my code is a piece of crap is a little harsh, that was just uncalled for

That should motivate you even more to learn and write better code. But I see your ego is in the way. If it helps, your code isn't bad. The problem was just the way you used templates.

I'm sorry if I was harsh. It's just that it is sometimes frustrating to see people who seem to think that because their code uses templates that it automatically makes their code "better". The thinking should go the other way around. If you want to write really good code (or use a fairly clever design paradigm), you will often end up _needing_ templates to realise it. What you have posted (or at least what your code is doing) is very simple and does not require templates and thus, making use of templates is not a good thing, and it does not add any value or quality to the code (in fact it makes it worse in this case).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.