Template problem! expected initializer before '<' token (Solved)

I am trying to make a calculator program and I am getting an error when I try to define a function from a class template

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>     //Basic I/O
#include <sstream>      //to handle bad input
#include <windows.h>    //For future key state use
#include <cmath>        //Extended math functions from C
#include <stdexcept>    //To handle exceptions
#include <exception>    //To handle exceptions

#include "Calculator.h"

#define pi 3.141592654

using namespace std;

int main()
{
    //This file has nothing, so it is useless for this thread
    return 0;
}


Calculator.cpp
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
  #include <iostream>

#include "Calculator.h"

template <typename T> class Calculator final
{   //This class template will handle the main functionality of the calculator. the generic type T will hold either an int or a double and the result will be either a
    //double or int. In the future two generic types will be provided to use operands of different types
   public:
       Calculator() : operand1(NULL), operand2(NULL), result(operand1 + operand2), counter(1) {}
       //This sets the operands and the result to a null state to avoid accidental operations on them.
       void set_operand_one(T op1);
       T get_operand_one();
       void set_operand_two(T op2);
       T get_operand_two() ;
       T get_result();
       //The following functions are responsible for calculations: Addition(), Subtraction(), Multiplication(), Division().
       T Addition();
       T Subtraction();
       T Multiplication();
       T Division();
       vector <char*> operation; //This will hold the entire operation to print to the screen
       void tick();
       size_t get_count();
   private:
    T mOperand1;
    T mOperand2;
    T mResult; //+, -, *, /, =
    T mOperator;
    static size_t counter; //This tells whether a calculation function has been called before, to calculate result
};


Calculator.h
1
2
3
4
5
6
7
8
9
10
11
12
  #pragma once
#include <iostream>     //Basic I/O
#include <sstream>      //to handle bad input
#include <windows.h>    //For future key state use
#include <cmath>        //Extended math functions from C
#include <stdexcept>    //To handle exceptions
#include <exception>    //To handle exceptions

using namespace std;

template <typename T> void Calculator<T>::set_operand_one() {cout << "hi";}


I get an error here:

template <typename T> void Calculator<T>::set_operand_one() {cout << "hi";}

it says error: expected initializer before '<' token
Last edited on
set_operand_one function prototype accepts a template variable. You didnt give it one in the function definition.
Thanks, but I'm still getting the same error. So how do I fix that?
I think there might be something wrong with codeblocks :( Idk if this is the case, but if it is, I am going to switch to visual studio
You have variables mOperand1 etc but the constructor has operand1 etc - the spelling difference is part of what the compiler is complaining about.

You'll need this include for vector.
#include <vector>


Edit:
See this earlier answer about initializing a static variable.
http://www.cplusplus.com/forum/beginner/100701/
Last edited on
I don't think anyone is understanding...

The problem is not in the constructor, it is here:

void Calculator<T>::set_operand_one(T op1) {cout << "hi";}

it gives an error: expected initializer before '<' token

can anyone help
I copied and pasted your code into the compiler on this site and got those errors.

The class declaration is usually in the .h file. The class function definitions are usually in the .cpp file. You seem to have reversed these above.
The definition of the class must precede the definition of it's methods.
EDIT:

Thank you everyone for your replies, but I finally figured out the problem...

I accidentally put the declaration of the class in the .cpp file and the definition in the .h file, when it should be the reverse of that! I apologize for making this stupid mistake. I will mark this thread as solved for others that make the same mistake :)
I accidentally put the declaration of the class in the .cpp file and the definition in the .h file, when it should be the reverse of that!

For templates, both belong in the header.
For templates, both belong in the header.


Why is that?

It works, though.
Why is that?

It works, though.

Does it work? Have you tried exercising an object of type Calculator in main?
Oh that's right - there is a difference with templates. It may be working so far if you don't have the main function built yet that would be trying to use the template class?

http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file

A way to keep separate files that I learned (and is mentioned in that linked SO thread) is to put an include to the .cpp file at the bottom of the .h file.
no no, I added the main function and also added a Calculator<int> ob; in it and it works perfectly
Have you tried exercising an object of type Calculator in main?


All of the code required for creation of a default constructed Calculator object is present in the header. Code for parameterized methods is not generated unless the methods are used.
Oh.. now I get an error. Fixing now, thanks :)
Last edited on
Topic archived. No new replies allowed.