I am having a problem with C++ templates which is actually quite embarrassing for me to admit to since I predict a lot of you will figure my problem out faster than it took me to write this post but I've exhausted my patience for right now. I wrote this short snippet in Linux with GCC.

HelloWorld.h

template<class MessageClass>
	class HelloWorld
	{
		public:
			void MessageOne();
			void MessageTwo();
			void MessageThree();
	};
	
#include "HelloWorld.cc"

HelloWorld.cc

using namespace std;

template<class MessageClass>
void HelloWorld<MessageClass>::MessageOne()
{
	cout << "MessageOne()";
}

I get the following errors:
"HelloWorld.cc:5: error: expected init-declarator before '<' token"
"HelloWorld.cc:5: error: expected ';' before '<' token"

What line does the error refer to, and how are you compiling your program?

It's referring to line that says:

void HelloWorld<MessageClass>::MessageOne()

in the "HelloWorld.cc" file. Here is the output I get

Compiling file: main.cc ...
g++  -I/home/david/Projects/HelloWorld/src -I/home/david/Projects/HelloWorld   -Wall   -g  -c "main.cc" -o "main.o"
In file included from HelloWorld.h:1,
                 from main.cc:2:
HelloWorld.cc:4: error: expected init-declarator before '<' token
HelloWorld.cc:4: error: expected `;' before '<' token
Completed ... unsuccessful
Total time taken: 2 secs

Now it says line 4 (not too sure why) but I assure you that the code is the same as shown above.

Is there any particular reason you're including the .cc file in the .h file instead of the other way around?

If I am not mistaken, that is the way my professor used to do it when it came to templates. I already tried "including" the other way around and all the compiler did was re-compile endlessly a hundred times or so (I hope that made sense). I'm at work right now but I'll try again. Any other problems? Suppose there is some kind of syntax error?

>all the compiler did was re-compile endlessly a hundred times or so
A hundred times or so isn't quite endless. Generally it's not recommended that you include .cc files. The conventional method is like so:

// HelloWorld.h
#ifndef HELLOWORLD
#define HELLOWORLD

template<class MessageClass>
	class HelloWorld
	{
		public:
			void MessageOne();
	};

#endif
#include <iostream>
#include "HelloWorld.h"

using namespace std;

template<class MessageClass>
void HelloWorld<MessageClass>::MessageOne()
{
	cout << "MessageOne()";
}

>Suppose there is some kind of syntax error?
There's nothing wrong with your syntax, otherwise I would have jumped on it straight away. Tested on three compilers (one of those being G++), the code compiles cleanly and runs properly with a test driver. So I have to assume that the source of the problem is elsewhere, or something funky is happening when you include the source file.

Could you post a complete example? Something that I don't have to add to or edit heavily to make legal enough for compilation? ;)

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.