0

I try to implement a function which received parameter type int and string. So I though of template.

Header file

template <typename T>
class SymbolTable {
public:
   void run(string filename);
   void insert(T value);
}

Implementation (.cpp) file

template <typename T>
void SymbolTable::run(string filename)
{
   cout << "success" << endl;
}
// Haven't implement "insert" yet!

Compiler report error:

name followed by '::' must be a class or namespace name

If I remove template, It works fine. Any suggestion...?

Tan Nguyen
  • 323
  • 2
  • 14

1 Answers1

3

This is just a basic syntax error. Your method definition should be:

template <typename T>
void SymbolTable<T>::run(string filename)
{
   cout << "success" << endl;
}

Should you need it, here are a few syntactically correct examples on how to use different variants of templates in c++.

LNiederha
  • 911
  • 4
  • 18
  • 1
    cplusplus.com is not "the reference" but a (frequently pretty awful) site written by "enthusiasts". It's of little use unless you already know enough to tell the good bits from the bad bits. – molbdnilo Sep 09 '21 at 10:00
  • @molbdnilo Good call, thanks for the correction. I agree my phrasing was misleading... IMO your comment on that website is completely valid if not a little harsh. It is true is asks for a lot of technical knowledge to use it in full, but they have a lot of syntactically correct examples which is nice. But the reliability of sources is a whole other opinion-based discussion... – LNiederha Sep 09 '21 at 10:24
  • 1
    Reliability is not a matter of opinion, it is a matter of fact, and the issues are usually with semantics, not syntax. – molbdnilo Sep 09 '21 at 14:27
  • @molbdnilo Yeah your completely right... re-read my comment and the last sentence is pretty dumb... thanks for pointing all of that out! – LNiederha Sep 09 '21 at 14:35